jfmt-cli 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.
Files changed (4) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +67 -0
  3. package/index.js +128 -0
  4. package/package.json +24 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Arephan
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,67 @@
1
+ # jfmt-cli
2
+
3
+ Pretty-print JSON from the command line. With colors.
4
+
5
+ ## Install
6
+
7
+ ```bash
8
+ npm install -g jfmt-cli
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ ```bash
14
+ # From a string
15
+ json-fmt '{"name":"test","count":42}'
16
+
17
+ # From a file
18
+ json-fmt package.json
19
+
20
+ # From a pipe
21
+ echo '{"a":1}' | json-fmt
22
+ curl -s api.github.com/users/octocat | json-fmt
23
+
24
+ # Shorthand alias
25
+ echo '{"a":1}' | jf
26
+ ```
27
+
28
+ ## Options
29
+
30
+ ```
31
+ -i, --indent <n> Indentation spaces (default: 2)
32
+ -c, --compact Compact output (no indentation)
33
+ -m, --minify Minify (same as --compact)
34
+ --no-color Disable colors
35
+ -h, --help Show help
36
+ ```
37
+
38
+ ## Examples
39
+
40
+ ```bash
41
+ # Custom indentation
42
+ json-fmt data.json --indent 4
43
+
44
+ # Minify
45
+ json-fmt data.json --minify
46
+
47
+ # Pipe from curl
48
+ curl -s https://api.github.com/users/octocat | json-fmt
49
+
50
+ # Combine with other tools
51
+ cat large.json | json-fmt | head -50
52
+ ```
53
+
54
+ ## Colors
55
+
56
+ Output is colorized when writing to a terminal:
57
+ - Keys: cyan
58
+ - Strings: green
59
+ - Numbers: yellow
60
+ - Booleans: magenta
61
+ - Null: gray
62
+
63
+ Use `--no-color` to disable.
64
+
65
+ ## License
66
+
67
+ MIT
package/index.js ADDED
@@ -0,0 +1,128 @@
1
+ #!/usr/bin/env node
2
+
3
+ /**
4
+ * json-fmt - Pretty-print JSON from CLI or pipe
5
+ * Usage:
6
+ * echo '{"a":1}' | json-fmt
7
+ * json-fmt '{"a":1}'
8
+ * json-fmt file.json
9
+ * curl api.example.com | json-fmt
10
+ */
11
+
12
+ const fs = require('fs');
13
+ const path = require('path');
14
+
15
+ const colors = {
16
+ key: '\x1b[36m', // cyan
17
+ string: '\x1b[32m', // green
18
+ number: '\x1b[33m', // yellow
19
+ boolean: '\x1b[35m', // magenta
20
+ null: '\x1b[90m', // gray
21
+ reset: '\x1b[0m',
22
+ };
23
+
24
+ function colorize(json, useColor) {
25
+ if (!useColor) return json;
26
+
27
+ return json
28
+ .replace(/"([^"]+)":/g, `${colors.key}"$1"${colors.reset}:`)
29
+ .replace(/: "([^"]*)"/g, `: ${colors.string}"$1"${colors.reset}`)
30
+ .replace(/: (-?\d+\.?\d*)/g, `: ${colors.number}$1${colors.reset}`)
31
+ .replace(/: (true|false)/g, `: ${colors.boolean}$1${colors.reset}`)
32
+ .replace(/: (null)/g, `: ${colors.null}$1${colors.reset}`);
33
+ }
34
+
35
+ function formatJSON(input, indent = 2, useColor = true) {
36
+ try {
37
+ const obj = JSON.parse(input);
38
+ const formatted = JSON.stringify(obj, null, indent);
39
+ return colorize(formatted, useColor);
40
+ } catch (err) {
41
+ console.error('Error: Invalid JSON');
42
+ console.error(err.message);
43
+ process.exit(1);
44
+ }
45
+ }
46
+
47
+ function showHelp() {
48
+ console.log(`
49
+ json-fmt - Pretty-print JSON
50
+
51
+ Usage:
52
+ json-fmt [options] [input]
53
+
54
+ Input:
55
+ <json> JSON string to format
56
+ <file> Path to JSON file
57
+ (stdin) Pipe JSON from another command
58
+
59
+ Options:
60
+ -i, --indent <n> Indentation spaces (default: 2)
61
+ -c, --compact Compact output (no indentation)
62
+ -m, --minify Minify (same as --compact)
63
+ --no-color Disable colors
64
+ -h, --help Show this help
65
+
66
+ Examples:
67
+ echo '{"a":1}' | json-fmt
68
+ json-fmt '{"name":"test"}'
69
+ json-fmt package.json
70
+ curl -s api.github.com/users/octocat | json-fmt
71
+ json-fmt data.json --indent 4
72
+ json-fmt '{"a":1}' --compact
73
+ `);
74
+ }
75
+
76
+ async function main() {
77
+ const args = process.argv.slice(2);
78
+
79
+ let indent = 2;
80
+ let useColor = process.stdout.isTTY;
81
+ let input = null;
82
+
83
+ // Parse arguments
84
+ for (let i = 0; i < args.length; i++) {
85
+ const arg = args[i];
86
+
87
+ if (arg === '-h' || arg === '--help') {
88
+ showHelp();
89
+ process.exit(0);
90
+ } else if (arg === '-i' || arg === '--indent') {
91
+ indent = parseInt(args[++i]) || 2;
92
+ } else if (arg === '-c' || arg === '--compact' || arg === '-m' || arg === '--minify') {
93
+ indent = 0;
94
+ } else if (arg === '--no-color') {
95
+ useColor = false;
96
+ } else if (!input) {
97
+ input = arg;
98
+ }
99
+ }
100
+
101
+ // Get input from file, argument, or stdin
102
+ let jsonInput;
103
+
104
+ if (input) {
105
+ // Check if it's a file
106
+ if (fs.existsSync(input)) {
107
+ jsonInput = fs.readFileSync(input, 'utf-8');
108
+ } else {
109
+ // Assume it's a JSON string
110
+ jsonInput = input;
111
+ }
112
+ } else if (!process.stdin.isTTY) {
113
+ // Read from stdin (pipe)
114
+ jsonInput = await new Promise((resolve) => {
115
+ let data = '';
116
+ process.stdin.setEncoding('utf8');
117
+ process.stdin.on('data', chunk => data += chunk);
118
+ process.stdin.on('end', () => resolve(data));
119
+ });
120
+ } else {
121
+ showHelp();
122
+ process.exit(0);
123
+ }
124
+
125
+ console.log(formatJSON(jsonInput.trim(), indent, useColor));
126
+ }
127
+
128
+ main();
package/package.json ADDED
@@ -0,0 +1,24 @@
1
+ {
2
+ "name": "jfmt-cli",
3
+ "version": "1.0.0",
4
+ "description": "Pretty-print JSON from the command line or pipe",
5
+ "bin": {
6
+ "jfmt": "index.js",
7
+ "jf": "index.js"
8
+ },
9
+ "main": "index.js",
10
+ "keywords": [
11
+ "json",
12
+ "format",
13
+ "pretty",
14
+ "print",
15
+ "cli",
16
+ "pipe"
17
+ ],
18
+ "author": "Arephan",
19
+ "license": "MIT",
20
+ "repository": {
21
+ "type": "git",
22
+ "url": "git+https://github.com/Arephan/jfmt-cli.git"
23
+ }
24
+ }