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.
- package/bin/safe-try.js +67 -46
- 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
|
-
|
|
8
|
-
const COLORS = {
|
|
9
|
-
reset: "\x1b[0m",
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
blue: "\x1b[34m",
|
|
13
|
-
white: "\x1b[37m"
|
|
14
|
-
};
|
|
15
|
-
|
|
16
|
-
const
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
const
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
}
|
|
44
|
-
console.log(
|
|
45
|
-
|
|
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);
|