Skip to content

Instantly share code, notes, and snippets.

@dtothefp
Created August 24, 2016 16:18
Show Gist options
  • Save dtothefp/07a37838f9891b3f5f64e6151aaef4da to your computer and use it in GitHub Desktop.
Save dtothefp/07a37838f9891b3f5f64e6151aaef4da to your computer and use it in GitHub Desktop.
import path from 'path';
import glob from 'globby';
import {readJson, outputJson} from 'fs-extra';
const cwd = process.cwd();
const read = (fp) => {
return new Promise((res, rej) => {
readJson(fp, (err, data) => {
if (err) return rej(err);
res(data);
});
});
};
const write = (data) => {
return new Promise((res, rej) => {
const dest = path.join(cwd, 'dist', 'combined.json');
outputJson(dest, data, (err) => {
if (err) return rej(err);
res();
});
});
};
glob('mocks/*.json', {cwd}).then(fps => {
const proms = fps.reduce((list, fp) => ([
...list,
read(fp)
]), []);
return Promise.all(proms);
}).then(data => {
const combined = data.reduce((acc, json) => ({
...acc,
...json
}), {});
return write(combined);
}).catch(console.log.bind(console, '**ERR**'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment