-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
executable file
·124 lines (106 loc) · 3.15 KB
/
Copy pathindex.js
File metadata and controls
executable file
·124 lines (106 loc) · 3.15 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#!/usr/bin/env node
const fs = require('fs');
const chalk = require('chalk');
// Parse command line arguments
const args = process.argv.slice(2);
let minify = false;
let indent = 2;
let validateOnly = false;
let useColor = true;
let inputFile = null;
for (let i = 0; i < args.length; i++) {
const arg = args[i];
if (arg === '-m' || arg === '--minify') {
minify = true;
} else if (arg === '-i' || arg === '--indent') {
indent = parseInt(args[++i], 10);
} else if (arg === '-v' || arg === '--validate') {
validateOnly = true;
} else if (arg === '--no-color') {
useColor = false;
} else if (arg === '-h' || arg === '--help') {
showHelp();
process.exit(0);
} else if (arg === '--version') {
const pkg = require('./package.json');
console.log(pkg.version);
process.exit(0);
} else if (!arg.startsWith('-')) {
inputFile = arg;
}
}
function showHelp() {
console.log(`
JSON Formatter CLI - Format, validate, and minify JSON
Usage: json-fmt [options] <file>
Options:
-m, --minify Minify JSON (remove whitespace)
-i, --indent <number> Set indentation spaces (default: 2)
-v, --validate Validate JSON without formatting
--no-color Disable syntax highlighting
-h, --help Show help
--version Show version
Examples:
json-fmt input.json Format JSON file
echo '{"a":1}' | json-fmt Format from stdin
json-fmt --minify input.json Minify JSON
json-fmt --indent 4 input.json Use 4-space indentation
curl api.com/data | json-fmt Format API response
`);
}
function colorizeJSON(jsonString) {
if (!useColor) return jsonString;
return jsonString
.replace(/"([^"]+)":/g, chalk.blue('"$1"') + ':')
.replace(/: "([^"]*)"/g, ': ' + chalk.green('"$1"'))
.replace(/: (\d+\.?\d*)/g, ': ' + chalk.yellow('$1'))
.replace(/: (true|false)/g, ': ' + chalk.magenta('$1'))
.replace(/: null/g, ': ' + chalk.gray('null'));
}
function processJSON(input) {
try {
const parsed = JSON.parse(input);
if (validateOnly) {
console.log(chalk.green('✓ Valid JSON'));
process.exit(0);
}
let output;
if (minify) {
output = JSON.stringify(parsed);
} else {
output = JSON.stringify(parsed, null, indent);
}
console.log(colorizeJSON(output));
process.exit(0);
} catch (error) {
console.error(chalk.red('✗ Invalid JSON:'), error.message);
process.exit(1);
}
}
// Read input
if (inputFile) {
if (!fs.existsSync(inputFile)) {
console.error(chalk.red(`Error: File not found: ${inputFile}`));
process.exit(1);
}
const input = fs.readFileSync(inputFile, 'utf8');
processJSON(input);
} else {
// Read from stdin
let input = '';
process.stdin.setEncoding('utf8');
process.stdin.on('readable', () => {
let chunk;
while ((chunk = process.stdin.read()) !== null) {
input += chunk;
}
});
process.stdin.on('end', () => {
if (!input.trim()) {
console.error(chalk.red('Error: No input provided'));
showHelp();
process.exit(1);
}
processJSON(input);
});
}