-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add executable for npx. See #59
Usage example: `npx merge ./foo.json ./bar.json`.
- Loading branch information
1 parent
9356e2f
commit 37eed9f
Showing
2 changed files
with
64 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
#!/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'], | ||
}); | ||
|
||
console.log(args); | ||
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); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters