safe-try-with-ai 1.3.2 → 1.4.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 (2) hide show
  1. package/bin/safe-try.js +67 -46
  2. package/package.json +1 -1
package/bin/safe-try.js CHANGED
@@ -1,46 +1,67 @@
1
- #!/usr/bin/env node
2
-
3
- const fs = require("fs");
4
- const path = require("path");
5
- const { safeTryJson } = require("../src/index.js");
6
-
7
- // ANSI color codes
8
- const COLORS = {
9
- reset: "\x1b[0m",
10
- green: "\x1b[32m",
11
- red: "\x1b[31m",
12
- blue: "\x1b[34m",
13
- white: "\x1b[37m",
14
- };
15
-
16
- const green = (text) => COLORS.green + text + COLORS.reset;
17
- const red = (text) => COLORS.red + text + COLORS.reset;
18
- const blue = (text) => COLORS.blue + text + COLORS.reset;
19
- const white = (text) => COLORS.white + text + COLORS.reset;
20
-
21
- // CLI Args
22
- const args = process.argv.slice(2);
23
- if (args.length === 0) {
24
- console.log(red("✖ No file specified"));
25
- console.log("Usage: npx safe-try-with-ai <file.json> [--analyze]");
26
- process.exit(1);
27
- }
28
-
29
- const filePath = path.resolve(args[0]);
30
- const analyze = args.includes("--analyze");
31
-
32
- // Read and validate JSON
33
- const [err] = safeTryJson(() => fs.readFileSync(filePath, "utf8"), { analyze });
34
-
35
- if (err) {
36
- console.log(red("✖ JSON is invalid"));
37
- console.log(white(` └─ Error: ${err.message}`));
38
- if (analyze && err.suggestion) {
39
- console.log(blue(` ├─ Suggestion: ${err.suggestion}`));
40
- if (err.fix) console.log(green(` └─ Fix: ${err.fix}`));
41
- }
42
- process.exit(1);
43
- } else {
44
- console.log(green("✔ JSON is valid"));
45
- process.exit(0);
46
- }
1
+ #!/usr/bin/env node
2
+
3
+ const fs = require("fs");
4
+ const path = require("path");
5
+ const { safeTryJson } = require("../src/index.js");
6
+
7
+ /* ANSI colors (NO dependencies) */
8
+ const COLORS = {
9
+ reset: "\x1b[0m",
10
+ red: "\x1b[31m",
11
+ green: "\x1b[32m",
12
+ blue: "\x1b[34m",
13
+ white: "\x1b[37m"
14
+ };
15
+
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;
26
+
27
+ /* CLI args */
28
+ 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
+ const analyze = args.includes("--analyze");
38
+
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}`));
60
+ }
61
+ }
62
+
63
+ process.exit(1);
64
+ }
65
+
66
+ console.log(green(`${SYMBOLS.success} JSON is valid`));
67
+ process.exit(0);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "safe-try-with-ai",
3
- "version": "1.3.2",
3
+ "version": "1.4.0",
4
4
  "description": "Clean error handling for JavaScript",
5
5
  "keywords": [
6
6
  "error-handling",