safe-try-with-ai 1.3.2 → 1.5.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 +43 -23
- package/bin/safe-try.js +63 -45
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
|
|
1
2
|
---
|
|
2
3
|
|
|
3
4
|
# safe-try-with-ai
|
|
@@ -10,7 +11,7 @@ A lightweight JavaScript utility for **clean error handling** with optional **AI
|
|
|
10
11
|
|
|
11
12
|
```bash
|
|
12
13
|
npm install safe-try-with-ai
|
|
13
|
-
|
|
14
|
+
````
|
|
14
15
|
|
|
15
16
|
---
|
|
16
17
|
|
|
@@ -103,41 +104,37 @@ if (err) {
|
|
|
103
104
|
|
|
104
105
|
## CLI Usage
|
|
105
106
|
|
|
106
|
-
## CLI Usage
|
|
107
|
-
|
|
108
107
|
Validate JSON files from the terminal:
|
|
109
108
|
|
|
109
|
+
```bash
|
|
110
110
|
npx safe-try-with-ai example.json
|
|
111
111
|
npx safe-try-with-ai example.json --analyze
|
|
112
|
+
```
|
|
112
113
|
|
|
113
|
-
|
|
114
|
-
✖ = JSON invalid (red)
|
|
115
|
-
Suggestions (blue)
|
|
116
|
-
Fix (green)
|
|
117
|
-
|
|
118
|
-
Exit codes:
|
|
119
|
-
0 = valid JSON
|
|
120
|
-
1 = invalid JSON or runtime error
|
|
121
|
-
|
|
122
|
-
|
|
114
|
+
### Pipe JSON via stdin
|
|
123
115
|
|
|
124
116
|
```bash
|
|
125
|
-
npx safe-try-with-ai
|
|
117
|
+
cat example.json | npx safe-try-with-ai --stdin
|
|
118
|
+
cat bad.json | npx safe-try-with-ai --stdin --analyze
|
|
126
119
|
```
|
|
127
120
|
|
|
128
|
-
|
|
121
|
+
### CLI Output Legend
|
|
129
122
|
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
Fix
|
|
134
|
-
|
|
123
|
+
* ✔ JSON valid (green)
|
|
124
|
+
* ✖ JSON invalid (red)
|
|
125
|
+
* Suggestions (blue)
|
|
126
|
+
* Fix (green)
|
|
127
|
+
|
|
128
|
+
### Exit Codes
|
|
129
|
+
|
|
130
|
+
* `0` = valid JSON
|
|
131
|
+
* `1` = invalid JSON or runtime error
|
|
135
132
|
|
|
136
133
|
---
|
|
137
134
|
|
|
138
135
|
## TypeScript Support
|
|
139
136
|
|
|
140
|
-
Built-in TypeScript definitions included
|
|
137
|
+
Built-in TypeScript definitions included:
|
|
141
138
|
|
|
142
139
|
```ts
|
|
143
140
|
import { safeTry } from "safe-try-with-ai";
|
|
@@ -156,7 +153,7 @@ No configuration required.
|
|
|
156
153
|
* Optional AI-style runtime suggestions
|
|
157
154
|
* Default fallback handling
|
|
158
155
|
* Safe JSON parsing helper
|
|
159
|
-
* CLI support via `npx`
|
|
156
|
+
* CLI support via `npx` and `--stdin`
|
|
160
157
|
* Built-in TypeScript definitions
|
|
161
158
|
* Zero dependencies
|
|
162
159
|
* Lightweight and fast
|
|
@@ -165,6 +162,24 @@ No configuration required.
|
|
|
165
162
|
|
|
166
163
|
## Changelog
|
|
167
164
|
|
|
165
|
+
### v1.5.0
|
|
166
|
+
|
|
167
|
+
* Added `--stdin` support for piping JSON to CLI
|
|
168
|
+
* CLI improvements for clearer output with colors and symbols
|
|
169
|
+
|
|
170
|
+
### v1.4.0
|
|
171
|
+
|
|
172
|
+
* Enhanced CLI with colors and symbols for valid/invalid JSON
|
|
173
|
+
* Improved AI-style suggestions formatting
|
|
174
|
+
|
|
175
|
+
### v1.3.2
|
|
176
|
+
|
|
177
|
+
* Bug fixes and small improvements
|
|
178
|
+
|
|
179
|
+
### v1.3.1
|
|
180
|
+
|
|
181
|
+
* Minor CLI fixes
|
|
182
|
+
|
|
168
183
|
### v1.3.0
|
|
169
184
|
|
|
170
185
|
* Added TypeScript definitions
|
|
@@ -187,4 +202,9 @@ No configuration required.
|
|
|
187
202
|
|
|
188
203
|
MIT
|
|
189
204
|
|
|
190
|
-
|
|
205
|
+
```
|
|
206
|
+
|
|
207
|
+
---
|
|
208
|
+
|
|
209
|
+
|
|
210
|
+
|
package/bin/safe-try.js
CHANGED
|
@@ -1,46 +1,64 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
|
|
3
|
-
const fs = require("fs");
|
|
4
|
-
const
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
green: "\x1b[32m",
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
const
|
|
17
|
-
const
|
|
18
|
-
const
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
const
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
const
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
const fs = require("fs");
|
|
4
|
+
const { safeTryJson } = require("../src/index.js");
|
|
5
|
+
|
|
6
|
+
// ANSI color codes
|
|
7
|
+
const COLORS = {
|
|
8
|
+
reset: "\x1b[0m",
|
|
9
|
+
red: "\x1b[31m",
|
|
10
|
+
green: "\x1b[32m",
|
|
11
|
+
blue: "\x1b[34m",
|
|
12
|
+
white: "\x1b[37m"
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
const green = (text) => COLORS.green + text + COLORS.reset;
|
|
16
|
+
const red = (text) => COLORS.red + text + COLORS.reset;
|
|
17
|
+
const blue = (text) => COLORS.blue + text + COLORS.reset;
|
|
18
|
+
const white = (text) => COLORS.white + text + COLORS.reset;
|
|
19
|
+
|
|
20
|
+
// CLI Args
|
|
21
|
+
const args = process.argv.slice(2);
|
|
22
|
+
const analyze = args.includes("--analyze");
|
|
23
|
+
|
|
24
|
+
// Check if stdin or file
|
|
25
|
+
if (args.includes("--stdin")) {
|
|
26
|
+
let input = "";
|
|
27
|
+
process.stdin.setEncoding("utf8");
|
|
28
|
+
process.stdin.on("data", chunk => input += chunk);
|
|
29
|
+
process.stdin.on("end", () => {
|
|
30
|
+
const [err] = safeTryJson(() => input, { analyze });
|
|
31
|
+
if (err) {
|
|
32
|
+
console.log(red("✖ Invalid JSON"));
|
|
33
|
+
console.log(white(` └─ Error: ${err.message}`));
|
|
34
|
+
if (analyze && err.suggestion) {
|
|
35
|
+
console.log(blue(` ├─ Suggestion: ${err.suggestion}`));
|
|
36
|
+
if (err.fix) console.log(green(` └─ Fix: ${err.fix}`));
|
|
37
|
+
}
|
|
38
|
+
process.exit(1);
|
|
39
|
+
} else {
|
|
40
|
+
console.log(green("✔ JSON is valid"));
|
|
41
|
+
process.exit(0);
|
|
42
|
+
}
|
|
43
|
+
});
|
|
44
|
+
} else if (args[0]) {
|
|
45
|
+
const filePath = args[0];
|
|
46
|
+
const [err] = safeTryJson(() => fs.readFileSync(filePath, "utf8"), { analyze });
|
|
47
|
+
if (err) {
|
|
48
|
+
console.log(red("✖ Cannot read or invalid JSON"));
|
|
49
|
+
console.log(white(` └─ Error: ${err.message}`));
|
|
50
|
+
if (analyze && err.suggestion) {
|
|
51
|
+
console.log(blue(` ├─ Suggestion: ${err.suggestion}`));
|
|
52
|
+
if (err.fix) console.log(green(` └─ Fix: ${err.fix}`));
|
|
53
|
+
}
|
|
54
|
+
process.exit(1);
|
|
55
|
+
} else {
|
|
56
|
+
console.log(green("✔ JSON is valid"));
|
|
57
|
+
process.exit(0);
|
|
58
|
+
}
|
|
59
|
+
} else {
|
|
60
|
+
console.log(red("✖ No file specified"));
|
|
61
|
+
console.log("Usage: safe-try-with-ai <file.json> [--analyze]");
|
|
62
|
+
console.log(" cat <file.json> | safe-try-with-ai --stdin [--analyze]");
|
|
63
|
+
process.exit(1);
|
|
46
64
|
}
|