-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.js
More file actions
58 lines (50 loc) · 1.73 KB
/
Copy pathcli.js
File metadata and controls
58 lines (50 loc) · 1.73 KB
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
#!/usr/bin/env node
import { compareKeys } from './../src/index.js';
const STYLES = Object.freeze({
reset: '\x1b[0m',
bold: '\x1b[1m',
red: '\x1b[31m',
green: '\x1b[32m',
yellow: '\x1b[33m',
blue: '\x1b[34m'
});
const files = process.argv.slice(2);
if (files.length < 2) {
console.log(`\n${STYLES.bold}Usage:${STYLES.reset} jsonkdiff <file1.json> <file2.json> ...`);
process.exit(0);
}
async function main() {
try {
const report = await compareKeys(files);
console.log(`\n${STYLES.blue}${STYLES.bold}--- JSON Key Diff Report ---${STYLES.reset}\n`);
let hasIssues = false;
for (const [fileName, missing] of Object.entries(report)) {
if (missing.length > 0) {
hasIssues = true;
console.log(`${STYLES.yellow}File: ${fileName}${STYLES.reset}`);
missing.forEach(key => {
console.log(` ${STYLES.red}× Missing key:${STYLES.reset} ${key}`);
});
console.log('');
}
}
if (!hasIssues) {
console.log(`${STYLES.green}✔ All files share the exact same keys!${STYLES.reset}\n`);
process.exit(0);
} else {
process.exit(1);
}
} catch (err) {
if (err.code === 'ENOENT') {
console.error(`${STYLES.red}${STYLES.bold}Error:${STYLES.reset} Could not find file "${err.path}"`);
} else if (err instanceof SyntaxError) {
console.error(`${STYLES.red}${STYLES.bold}Error:${STYLES.reset} Invalid JSON in one of the files.`);
} else if (err.code === 'EISDIR') {
console.error(`${STYLES.red}${STYLES.bold}Error:${STYLES.reset} One of the given paths is a directory, not a file.`);
} else {
console.error(`${STYLES.red}${STYLES.bold}Error:${STYLES.reset} ${err.message}`);
}
process.exit(1);
}
}
main();