-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
67 lines (56 loc) · 1.55 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
const fs = require('fs');
const path = require('path');
const { promisify } = require('util');
const chokidar = require('chokidar');
const merge = require('merge');
const { name } = require('./package.json');
require('colors');
const readFileAsync = promisify(fs.readFile);
const writeFileAsync = promisify(fs.writeFile);
const createDirIfNotExist = to => {
const dirs = [];
let dir = path.dirname(to);
while (dir !== path.dirname(dir)) {
dirs.unshift(dir);
dir = path.dirname(dir);
}
dirs.forEach(dir => {
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir);
}
});
};
module.exports = ({ input, output, watch = false, verbose = false, recursive = false, prettify = false }) => {
const run = async () => {
try {
const files = await Promise.all(input.map(i => readFileAsync(i)));
const mergeFn = recursive ? merge.recursive : merge;
createDirIfNotExist(output);
await writeFileAsync(output, JSON.stringify(mergeFn(...files.map(i => JSON.parse(i))), null, prettify ? ' ' : ''));
if (verbose) {
console.log('[MERGE][COMPLETE]'.yellow, output);
}
} catch (e) {
console.log('[MERGE][ERROR]'.red, output);
console.error(e);
}
};
let once = true;
return {
name,
buildStart() {
if (once) {
once = false;
if (watch) {
chokidar.watch(input)
.on('add', run)
.on('change', run)
.on('unlink', run)
.on('error', e => console.error(e));
} else {
run();
}
}
}
};
};