-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathmerge-cli.mjs
executable file
·57 lines (48 loc) · 1.16 KB
/
merge-cli.mjs
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
#!/usr/bin/env node
import mri from 'mri';
import fs from 'fs';
import { merge, recursive } from "./lib/index.js";
const args = mri(process.argv.slice(2), {
alias: {
s: 'shallow',
h: 'help',
},
boolean: ['shallow', 'help'],
});
const inputFiles = args._;
/*
Why to avoid `process.exit()`:
see https://stackoverflow.com/a/37592669/521957
or https://nodejs.org/api/process.html#processexitcode.
*/
if (args.help) {
if (inputFiles.length) {
process.exitCode = 1;
console.error("You can't provide `--help` or `-h` flags and input files at the same time.")
} else {
const help = `\
npx merge
[--shallow or -s] # If merge is shallow then recursion won't be used.
[--help or -h]
[file1.json file2.json ...]
`;
process.stdout.write(help);
}
} else if (!inputFiles.length) {
console.log({});
} else {
const objects = inputFiles.map(
(path) => {
const json = fs.readFileSync(path, 'utf-8');
return JSON.parse(json);
}
);
const ifToClone = false;
let merged;
if (args.shallow) {
merged = merge(ifToClone, ...objects);
} else {
merged = recursive(ifToClone, ...objects);
}
console.log(merged);
}