@newpeak/barista-cli 0.1.61 → 0.1.63

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.
@@ -0,0 +1,24 @@
1
+ export interface JsonRepairOptions {
2
+ /** Enable debug logging to stderr */
3
+ debug?: boolean;
4
+ /** Custom error prefix for the thrown error message */
5
+ errorPrefix?: string;
6
+ }
7
+ /**
8
+ * Attempt to repair and parse a JSON string that may have been mangled
9
+ * by shell argument processing (especially Windows PowerShell).
10
+ *
11
+ * Heuristics applied (in order):
12
+ * 1. Trim whitespace
13
+ * 2. Strip surrounding single quotes ('[...]' → [...])
14
+ * 3. Fix PowerShell/CMD escaped double quotes (\" → ")
15
+ * 4. Fix doubled quotes ("{""key"":""value""}" → "{"key":"value"}")
16
+ * 5. Fall through to JSON.parse
17
+ *
18
+ * @param raw - The raw string from CLI option
19
+ * @param options - Configuration options
20
+ * @returns Parsed JSON array
21
+ * @throws Error with descriptive message showing the raw input and expected format
22
+ */
23
+ export declare function parseItemsJson(raw: string, options?: JsonRepairOptions): unknown[];
24
+ //# sourceMappingURL=json-repair.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"json-repair.d.ts","sourceRoot":"","sources":["../../src/utils/json-repair.ts"],"names":[],"mappings":"AAMA,MAAM,WAAW,iBAAiB;IAChC,qCAAqC;IACrC,KAAK,CAAC,EAAE,OAAO,CAAC;IAChB,uDAAuD;IACvD,WAAW,CAAC,EAAE,MAAM,CAAC;CACtB;AAED;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,cAAc,CAAC,GAAG,EAAE,MAAM,EAAE,OAAO,GAAE,iBAAsB,GAAG,OAAO,EAAE,CA+EtF"}
@@ -0,0 +1,104 @@
1
+ /**
2
+ * JSON repair utility for handling shell-escaped/mangled JSON strings
3
+ * from CLI --items options, especially on Windows PowerShell.
4
+ */
5
+ import chalk from 'chalk';
6
+ /**
7
+ * Attempt to repair and parse a JSON string that may have been mangled
8
+ * by shell argument processing (especially Windows PowerShell).
9
+ *
10
+ * Heuristics applied (in order):
11
+ * 1. Trim whitespace
12
+ * 2. Strip surrounding single quotes ('[...]' → [...])
13
+ * 3. Fix PowerShell/CMD escaped double quotes (\" → ")
14
+ * 4. Fix doubled quotes ("{""key"":""value""}" → "{"key":"value"}")
15
+ * 5. Fall through to JSON.parse
16
+ *
17
+ * @param raw - The raw string from CLI option
18
+ * @param options - Configuration options
19
+ * @returns Parsed JSON array
20
+ * @throws Error with descriptive message showing the raw input and expected format
21
+ */
22
+ export function parseItemsJson(raw, options = {}) {
23
+ const { debug = false, errorPrefix = 'Invalid JSON for --items' } = options;
24
+ const debugLog = (...args) => {
25
+ if (debug)
26
+ console.error(chalk.gray(`[DEBUG]`, ...args));
27
+ };
28
+ debugLog(`Raw --items input:`, raw);
29
+ let input = raw.trim();
30
+ // Heuristic 1: Strip surrounding single quotes (common on Linux/macOS)
31
+ if ((input.startsWith("'") && input.endsWith("'")) ||
32
+ (input.startsWith('\u2018') && input.endsWith('\u2019'))) {
33
+ input = input.slice(1, -1).trim();
34
+ debugLog(`Stripped single quotes, result:`, input);
35
+ }
36
+ // Heuristic 2: Fix PowerShell-escaped double quotes (\" → ")
37
+ // PowerShell converts "key":"value" to \"key\":\"value\" when passing via --items
38
+ if (input.includes('\\"')) {
39
+ const repaired = input.replace(/\\"/g, '"');
40
+ debugLog(`Repairing PowerShell escaped quotes:`, repaired);
41
+ try {
42
+ const parsed = JSON.parse(repaired);
43
+ if (Array.isArray(parsed) && parsed.length > 0) {
44
+ debugLog(`PowerShell repair succeeded, items:`, parsed.length);
45
+ return parsed;
46
+ }
47
+ if (!Array.isArray(parsed)) {
48
+ throw new Error('not an array');
49
+ }
50
+ throw new Error('empty array');
51
+ }
52
+ catch {
53
+ debugLog(`PowerShell repair parse failed, trying next heuristic`);
54
+ }
55
+ }
56
+ // Heuristic 3: Try raw parse first
57
+ try {
58
+ const parsed = JSON.parse(input);
59
+ if (Array.isArray(parsed) && parsed.length > 0) {
60
+ debugLog(`Direct parse succeeded, items:`, parsed.length);
61
+ return parsed;
62
+ }
63
+ if (!Array.isArray(parsed)) {
64
+ throw new Error(`${errorPrefix}: must be a non-empty JSON array. Got: ${typeof parsed}`);
65
+ }
66
+ throw new Error(`${errorPrefix}: must be a non-empty JSON array`);
67
+ }
68
+ catch (e) {
69
+ // If it's our own format error, re-throw
70
+ if (e instanceof Error && e.message.includes(errorPrefix)) {
71
+ throw e;
72
+ }
73
+ debugLog(`Direct parse failed:`, e.message);
74
+ }
75
+ // Heuristic 4: Fix doubled quotes (Windows CMD sometimes doubles quotes)
76
+ if (input.includes('""')) {
77
+ const repaired = input.replace(/""/g, '"');
78
+ debugLog(`Repairing doubled quotes:`, repaired);
79
+ try {
80
+ const parsed = JSON.parse(repaired);
81
+ if (Array.isArray(parsed) && parsed.length > 0) {
82
+ debugLog(`Doubled-quote repair succeeded, items:`, parsed.length);
83
+ return parsed;
84
+ }
85
+ }
86
+ catch {
87
+ debugLog(`Doubled-quote repair parse failed`);
88
+ }
89
+ }
90
+ // All heuristics failed - throw descriptive error
91
+ throw new Error(`${errorPrefix}\n` +
92
+ `Raw input received: ${raw}\n` +
93
+ `Expected format: '[{"materialCode":"CODE","quantity":100}]'\n` +
94
+ `Tip: Try surrounding the JSON with single quotes: '${renderExample(raw)}'`);
95
+ }
96
+ function renderExample(raw) {
97
+ // Try to extract the first few chars to show the user a relevant example
98
+ const trimmed = raw.trim();
99
+ if (trimmed.startsWith('[')) {
100
+ return '[{"materialCode":"CODE","materialName":"NAME","quantity":100}]';
101
+ }
102
+ return '[{"materialCode":"CODE","quantity":100}]';
103
+ }
104
+ //# sourceMappingURL=json-repair.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"json-repair.js","sourceRoot":"","sources":["../../src/utils/json-repair.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,OAAO,KAAK,MAAM,OAAO,CAAC;AAS1B;;;;;;;;;;;;;;;GAeG;AACH,MAAM,UAAU,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,GAAG,KAAK,CAAC,OAAO,CAAC,KAAK,EAAE,GAAG,CAAC,CAAC;QAC3C,QAAQ,CAAC,2BAA2B,EAAE,QAAQ,CAAC,CAAC;QAChD,IAAI,CAAC;YACH,MAAM,MAAM,GAAG,IAAI,CAAC,KAAK,CAAC,QAAQ,CAAC,CAAC;YACpC,IAAI,KAAK,CAAC,OAAO,CAAC,MAAM,CAAC,IAAI,MAAM,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;gBAC/C,QAAQ,CAAC,wCAAwC,EAAE,MAAM,CAAC,MAAM,CAAC,CAAC;gBAClE,OAAO,MAAM,CAAC;YAChB,CAAC;QACH,CAAC;QAAC,MAAM,CAAC;YACP,QAAQ,CAAC,mCAAmC,CAAC,CAAC;QAChD,CAAC;IACH,CAAC;IAED,kDAAkD;IAClD,MAAM,IAAI,KAAK,CACb,GAAG,WAAW,IAAI;QAClB,uBAAuB,GAAG,IAAI;QAC9B,+DAA+D;QAC/D,sDAAsD,aAAa,CAAC,GAAG,CAAC,GAAG,CAC5E,CAAC;AACJ,CAAC;AAED,SAAS,aAAa,CAAC,GAAW;IAChC,yEAAyE;IACzE,MAAM,OAAO,GAAG,GAAG,CAAC,IAAI,EAAE,CAAC;IAC3B,IAAI,OAAO,CAAC,UAAU,CAAC,GAAG,CAAC,EAAE,CAAC;QAC5B,OAAO,gEAAgE,CAAC;IAC1E,CAAC;IACD,OAAO,0CAA0C,CAAC;AACpD,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@newpeak/barista-cli",
3
- "version": "0.1.61",
3
+ "version": "0.1.63",
4
4
  "description": "AI Tools CLI for Liberica and Arabica services",
5
5
  "license": "MIT",
6
6
  "keywords": [