Skip to content

Commit

Permalink
feat: add executable for npx. See swordev#59
Browse files Browse the repository at this point in the history
Usage example: `npx merge ./foo.json ./bar.json`.
  • Loading branch information
ilyaigpetrov committed Feb 6, 2024
1 parent 9356e2f commit 84cbd06
Show file tree
Hide file tree
Showing 3 changed files with 5,072 additions and 0 deletions.
49 changes: 49 additions & 0 deletions merge-cli.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#!/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'],
});

const inputFiles = args._;

// Why to avoid `process.exit()`: https://stackoverflow.com/a/37592669/521957.
if (args.help) {

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);

}

0 comments on commit 84cbd06

Please sign in to comment.