@zjy4fun/json-open 0.1.1 → 0.1.3

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 CHANGED
@@ -107,9 +107,16 @@ Input source:
107
107
  - stdin (pipe)
108
108
  - inline argument JSON string
109
109
 
110
- Example:
110
+ Options:
111
+
112
+ - `-h, --help` show help
113
+ - `-v, --version` show CLI version
114
+
115
+ Examples:
111
116
 
112
117
  ```bash
118
+ json --help
119
+ json --version
113
120
  json '{"ok":true}'
114
121
  ```
115
122
 
package/README.zh-CN.md CHANGED
@@ -107,9 +107,16 @@ json
107
107
  - stdin(管道)
108
108
  - 命令行参数中的 JSON 字符串
109
109
 
110
+ 参数选项:
111
+
112
+ - `-h, --help` 显示帮助
113
+ - `-v, --version` 显示版本
114
+
110
115
  示例:
111
116
 
112
117
  ```bash
118
+ json --help
119
+ json --version
113
120
  json '{"ok":true}'
114
121
  ```
115
122
 
package/bin/json.js CHANGED
@@ -1,8 +1,34 @@
1
1
  #!/usr/bin/env node
2
2
  import fs from 'node:fs/promises'
3
+ import fsSync from 'node:fs'
3
4
  import os from 'node:os'
4
5
  import path from 'node:path'
5
6
  import { spawn } from 'node:child_process'
7
+ import { fileURLToPath } from 'node:url'
8
+
9
+ function getCliVersion() {
10
+ try {
11
+ const currentFile = fileURLToPath(import.meta.url)
12
+ const packageJsonPath = path.resolve(path.dirname(currentFile), '..', 'package.json')
13
+ const packageJson = JSON.parse(fsSync.readFileSync(packageJsonPath, 'utf8'))
14
+ return packageJson.version
15
+ } catch {
16
+ return 'unknown'
17
+ }
18
+ }
19
+
20
+ function printHelp() {
21
+ console.log(`json-open
22
+
23
+ Usage:
24
+ json <json-string>
25
+ cat data.json | json
26
+ curl https://example.com/api | json
27
+
28
+ Options:
29
+ -h, --help Show help
30
+ -v, --version Show version`)
31
+ }
6
32
 
7
33
  function readStdin() {
8
34
  return new Promise((resolve, reject) => {
@@ -178,10 +204,22 @@ function openInBrowser(filePath) {
178
204
  }
179
205
 
180
206
  async function main() {
181
- const inlineInput = process.argv.slice(2).join(' ').trim()
207
+ const args = process.argv.slice(2)
208
+
209
+ if (args.includes('-h') || args.includes('--help')) {
210
+ printHelp()
211
+ process.exit(0)
212
+ }
213
+
214
+ if (args.includes('-v') || args.includes('--version')) {
215
+ console.log(getCliVersion())
216
+ process.exit(0)
217
+ }
218
+
219
+ const inlineInput = args.join(' ').trim()
182
220
 
183
221
  if (!inlineInput && process.stdin.isTTY) {
184
- console.error('Usage: curl https://example.com | json\n or: json "{\"hello\":\"world\"}"')
222
+ printHelp()
185
223
  process.exit(1)
186
224
  }
187
225
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zjy4fun/json-open",
3
- "version": "0.1.1",
3
+ "version": "0.1.3",
4
4
  "description": "Open JSON (stdin or inline text) in a browser with collapsible tree view",
5
5
  "type": "module",
6
6
  "bin": {