safe-try-with-ai 1.4.0 → 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.
Files changed (3) hide show
  1. package/README.md +43 -23
  2. package/bin/safe-try.js +44 -47
  3. 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
- = JSON valid (green)
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 example.json
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
- Example output:
121
+ ### CLI Output Legend
129
122
 
130
- ```
131
- Error: Unexpected token
132
- Suggestion: Check JSON formatting
133
- Fix: Ensure commas and brackets are correct
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,10 +1,9 @@
1
1
  #!/usr/bin/env node
2
2
 
3
3
  const fs = require("fs");
4
- const path = require("path");
5
4
  const { safeTryJson } = require("../src/index.js");
6
5
 
7
- /* ANSI colors (NO dependencies) */
6
+ // ANSI color codes
8
7
  const COLORS = {
9
8
  reset: "\x1b[0m",
10
9
  red: "\x1b[31m",
@@ -13,55 +12,53 @@ const COLORS = {
13
12
  white: "\x1b[37m"
14
13
  };
15
14
 
16
- const SYMBOLS = {
17
- success: "✔",
18
- error: "✖",
19
- info: "ℹ"
20
- };
21
-
22
- const green = (t) => COLORS.green + t + COLORS.reset;
23
- const red = (t) => COLORS.red + t + COLORS.reset;
24
- const blue = (t) => COLORS.blue + t + COLORS.reset;
25
- const white = (t) => COLORS.white + t + COLORS.reset;
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;
26
19
 
27
- /* CLI args */
20
+ // CLI Args
28
21
  const args = process.argv.slice(2);
29
-
30
- if (args.length === 0) {
31
- console.log(red(`${SYMBOLS.error} No file specified`));
32
- console.log("Usage: safe-try-with-ai <file.json> [--analyze]");
33
- process.exit(1);
34
- }
35
-
36
- const filePath = path.resolve(args[0]);
37
22
  const analyze = args.includes("--analyze");
38
23
 
39
- /* Read file */
40
- let jsonText;
41
- try {
42
- jsonText = fs.readFileSync(filePath, "utf8");
43
- } catch (e) {
44
- console.log(red(`${SYMBOLS.error} Cannot read file`));
45
- console.log(white(` └─ ${e.message}`));
46
- process.exit(1);
47
- }
48
-
49
- /* Validate JSON */
50
- const [err] = safeTryJson(jsonText, { analyze });
51
-
52
- if (err) {
53
- console.log(red(`${SYMBOLS.error} Invalid JSON`));
54
- console.log(white(` └─ Error: ${err.message}`));
55
-
56
- if (analyze && err.suggestion) {
57
- console.log(blue(` ├─ Suggestion: ${err.suggestion}`));
58
- if (err.fix) {
59
- console.log(green(` └─ Fix: ${err.fix}`));
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}`));
60
53
  }
54
+ process.exit(1);
55
+ } else {
56
+ console.log(green("✔ JSON is valid"));
57
+ process.exit(0);
61
58
  }
62
-
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
63
  process.exit(1);
64
- }
65
-
66
- console.log(green(`${SYMBOLS.success} JSON is valid`));
67
- process.exit(0);
64
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "safe-try-with-ai",
3
- "version": "1.4.0",
3
+ "version": "1.5.0",
4
4
  "description": "Clean error handling for JavaScript",
5
5
  "keywords": [
6
6
  "error-handling",