devkits-json-formatter 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +45 -0
- package/index.js +93 -0
- package/package.json +40 -0
package/README.md
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
# @devkits/json-formatter
|
|
2
|
+
|
|
3
|
+
Format and validate JSON from CLI or stdin. Part of [@devkits/tools](https://devkits-tools.surge.sh).
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install -g @devkits/json-formatter
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
### Format JSON (pretty print)
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
# From stdin
|
|
17
|
+
echo '{"name":"DevKits","tools":84}' | jsonfmt
|
|
18
|
+
|
|
19
|
+
# From file
|
|
20
|
+
jsonfmt package.json
|
|
21
|
+
|
|
22
|
+
# From JSON string
|
|
23
|
+
jsonfmt '{"test":true}'
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
### Minify JSON
|
|
27
|
+
|
|
28
|
+
```bash
|
|
29
|
+
jsonfmt package.json --minify
|
|
30
|
+
jsonfmt '{"test":true}' -m
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
### Web Version
|
|
34
|
+
|
|
35
|
+
Use the web version at: https://devkits-tools.surge.sh
|
|
36
|
+
|
|
37
|
+
### Pro Features
|
|
38
|
+
|
|
39
|
+
Unlock batch processing, API access, and cloud sync: https://devkits-tools.surge.sh/pro
|
|
40
|
+
|
|
41
|
+
Use code `EARLYBIRD-2026` for 20% off ($7.20 instead of $9).
|
|
42
|
+
|
|
43
|
+
## License
|
|
44
|
+
|
|
45
|
+
MIT ā DevKits Team
|
package/index.js
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @devkits/json-formatter ā Format and validate JSON
|
|
5
|
+
*
|
|
6
|
+
* Usage:
|
|
7
|
+
* jsonfmt <file|json> [--minify]
|
|
8
|
+
* echo '{"foo":"bar"}' | jsonfmt
|
|
9
|
+
* jsonfmt package.json
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
const fs = require('fs');
|
|
13
|
+
|
|
14
|
+
const PRO_UPGRADE = '\nš Want more? Upgrade to Pro: https://devkits-tools.surge.sh/pro\n Use code EARLYBIRD-2026 for 20% off\n';
|
|
15
|
+
|
|
16
|
+
function showHelp() {
|
|
17
|
+
console.log(`
|
|
18
|
+
@devkits/json-formatter ā Format and validate JSON
|
|
19
|
+
|
|
20
|
+
Usage:
|
|
21
|
+
jsonfmt <file|json> [--minify]
|
|
22
|
+
jsonfmt package.json
|
|
23
|
+
jsonfmt '{"foo":"bar"}'
|
|
24
|
+
echo '{"test":true}' | jsonfmt
|
|
25
|
+
|
|
26
|
+
Options:
|
|
27
|
+
--minify, -m Minify JSON (remove whitespace)
|
|
28
|
+
--help, -h Show this help
|
|
29
|
+
|
|
30
|
+
Examples:
|
|
31
|
+
jsonfmt package.json
|
|
32
|
+
jsonfmt '{"foo":"bar"}' --minify
|
|
33
|
+
cat data.json | jsonfmt
|
|
34
|
+
|
|
35
|
+
Web Version: https://devkits-tools.surge.sh
|
|
36
|
+
Pro Features: https://devkits-tools.surge.sh/pro
|
|
37
|
+
`);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function formatJSON(input, minify = false) {
|
|
41
|
+
try {
|
|
42
|
+
let jsonData;
|
|
43
|
+
|
|
44
|
+
// Check if input is a file path
|
|
45
|
+
if (fs.existsSync(input)) {
|
|
46
|
+
input = fs.readFileSync(input, 'utf-8');
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
jsonData = JSON.parse(input);
|
|
50
|
+
|
|
51
|
+
if (minify) {
|
|
52
|
+
console.log(JSON.stringify(jsonData));
|
|
53
|
+
} else {
|
|
54
|
+
console.log(JSON.stringify(jsonData, null, 2));
|
|
55
|
+
console.log(`\nā
Valid JSON`);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
} catch (err) {
|
|
59
|
+
console.error(`ā Invalid JSON: ${err.message}`);
|
|
60
|
+
process.exit(1);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
// Main
|
|
65
|
+
const args = process.argv.slice(2);
|
|
66
|
+
|
|
67
|
+
// Check for help
|
|
68
|
+
if (args.includes('--help') || args.includes('-h')) {
|
|
69
|
+
showHelp();
|
|
70
|
+
process.exit(0);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
const minify = args.includes('--minify') || args.includes('-m');
|
|
74
|
+
const input = args.filter(a => !a.startsWith('-')).join(' ');
|
|
75
|
+
|
|
76
|
+
if (!input) {
|
|
77
|
+
// Read from stdin
|
|
78
|
+
try {
|
|
79
|
+
const stdin = fs.readFileSync(0, 'utf-8').trim();
|
|
80
|
+
if (!stdin) {
|
|
81
|
+
showHelp();
|
|
82
|
+
process.exit(0);
|
|
83
|
+
}
|
|
84
|
+
formatJSON(stdin, minify);
|
|
85
|
+
} catch (err) {
|
|
86
|
+
console.error('Error reading stdin: ' + err.message);
|
|
87
|
+
process.exit(1);
|
|
88
|
+
}
|
|
89
|
+
} else {
|
|
90
|
+
formatJSON(input, minify);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
console.log(PRO_UPGRADE);
|
package/package.json
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "devkits-json-formatter",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Format and validate JSON from CLI or stdin ā pretty print, minify, and validate JSON files",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"bin": {
|
|
7
|
+
"jsonfmt": "./index.js",
|
|
8
|
+
"json-formatter": "./index.js"
|
|
9
|
+
},
|
|
10
|
+
"scripts": {
|
|
11
|
+
"test": "node test.js"
|
|
12
|
+
},
|
|
13
|
+
"keywords": [
|
|
14
|
+
"json",
|
|
15
|
+
"formatter",
|
|
16
|
+
"validator",
|
|
17
|
+
"cli",
|
|
18
|
+
"pretty-print",
|
|
19
|
+
"minify",
|
|
20
|
+
"developer-tools"
|
|
21
|
+
],
|
|
22
|
+
"author": "DevKits Team <devkits-auto@protonmail.com>",
|
|
23
|
+
"license": "MIT",
|
|
24
|
+
"repository": {
|
|
25
|
+
"type": "git",
|
|
26
|
+
"url": "https://github.com/devkits/tools"
|
|
27
|
+
},
|
|
28
|
+
"homepage": "https://devkits-tools.surge.sh",
|
|
29
|
+
"bugs": {
|
|
30
|
+
"url": "https://github.com/devkits/tools/issues"
|
|
31
|
+
},
|
|
32
|
+
"engines": {
|
|
33
|
+
"node": ">=14.0.0"
|
|
34
|
+
},
|
|
35
|
+
"files": [
|
|
36
|
+
"index.js",
|
|
37
|
+
"README.md"
|
|
38
|
+
],
|
|
39
|
+
"dependencies": {}
|
|
40
|
+
}
|