@kjanat/dreamcli 2.5.0 → 3.0.0-rc.10
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/CHANGELOG.md +344 -1
- package/README.md +9 -2
- package/dist/core/cli/dispatch.d.mts +6 -5
- package/dist/core/cli/dispatch.mjs +205 -1
- package/dist/core/cli/help-links.d.mts +0 -1
- package/dist/core/cli/help-links.mjs +41 -1
- package/dist/core/cli/index.d.mts +185 -19
- package/dist/core/cli/index.mjs +945 -1
- package/dist/core/cli/planner.d.mts +32 -6
- package/dist/core/cli/planner.mjs +300 -1
- package/dist/core/cli/plugin.d.mts +1 -1
- package/dist/core/cli/plugin.mjs +41 -1
- package/dist/core/cli/propagate.d.mts +1 -1
- package/dist/core/cli/propagate.mjs +57 -1
- package/dist/core/cli/root-help.d.mts +18 -3
- package/dist/core/cli/root-help.mjs +226 -6
- package/dist/core/cli/root-surface.d.mts +30 -4
- package/dist/core/cli/root-surface.mjs +28 -1
- package/dist/core/cli/runtime-preflight.d.mts +30 -3
- package/dist/core/cli/runtime-preflight.mjs +169 -1
- package/dist/core/completion/index.d.mts +32 -2
- package/dist/core/completion/index.mjs +90 -1
- package/dist/core/completion/shells/bash.d.mts +0 -1
- package/dist/core/completion/shells/bash.mjs +308 -3
- package/dist/core/completion/shells/fish.d.mts +0 -1
- package/dist/core/completion/shells/fish.mjs +162 -2
- package/dist/core/completion/shells/powershell.d.mts +0 -1
- package/dist/core/completion/shells/powershell.mjs +358 -2
- package/dist/core/completion/shells/shared.d.mts +23 -2
- package/dist/core/completion/shells/shared.mjs +176 -1
- package/dist/core/completion/shells/zsh.d.mts +0 -1
- package/dist/core/completion/shells/zsh.mjs +251 -2
- package/dist/core/config/index.d.mts +2 -3
- package/dist/core/config/index.mjs +246 -1
- package/dist/core/config/package-json.d.mts +27 -4
- package/dist/core/config/package-json.mjs +386 -3
- package/dist/core/errors/index.d.mts +3 -3
- package/dist/core/errors/index.mjs +87 -1
- package/dist/core/execution/index.d.mts +0 -1
- package/dist/core/execution/index.mjs +201 -1
- package/dist/core/help/ansi.d.mts +11 -5
- package/dist/core/help/ansi.mjs +121 -2
- package/dist/core/help/index.d.mts +65 -2
- package/dist/core/help/index.mjs +341 -7
- package/dist/core/help/theme.d.mts +68 -0
- package/dist/core/help/theme.mjs +46 -0
- package/dist/core/json-schema/index.d.mts +15 -2
- package/dist/core/json-schema/index.mjs +536 -12
- package/dist/core/json-schema/meta-descriptions.generated.d.mts +22 -5
- package/dist/core/json-schema/meta-descriptions.generated.mjs +102 -28
- package/dist/core/output/activity.d.mts +0 -1
- package/dist/core/output/activity.mjs +452 -1
- package/dist/core/output/bind.mjs +56 -1
- package/dist/core/output/contracts.d.mts +4 -5
- package/dist/core/output/contracts.mjs +57 -1
- package/dist/core/output/display-value.mjs +32 -1
- package/dist/core/output/index.d.mts +21 -1
- package/dist/core/output/index.mjs +442 -2
- package/dist/core/output/renderers.d.mts +0 -1
- package/dist/core/output/renderers.mjs +46 -1
- package/dist/core/output/writer.mjs +12 -1
- package/dist/core/parse/index.d.mts +54 -7
- package/dist/core/parse/index.mjs +727 -1
- package/dist/core/prompt/index.d.mts +12 -4
- package/dist/core/prompt/index.mjs +348 -6
- package/dist/core/resolve/args.d.mts +1 -1
- package/dist/core/resolve/args.mjs +105 -1
- package/dist/core/resolve/coerce.d.mts +1 -1
- package/dist/core/resolve/coerce.mjs +336 -1
- package/dist/core/resolve/config.mjs +19 -1
- package/dist/core/resolve/contracts.d.mts +9 -4
- package/dist/core/resolve/errors.d.mts +0 -1
- package/dist/core/resolve/errors.mjs +140 -2
- package/dist/core/resolve/flags.d.mts +7 -2
- package/dist/core/resolve/flags.mjs +251 -1
- package/dist/core/resolve/index.d.mts +2 -2
- package/dist/core/resolve/index.mjs +85 -1
- package/dist/core/resolve/property.d.mts +6 -2
- package/dist/core/resolve/property.mjs +53 -1
- package/dist/core/schema/activity.d.mts +31 -11
- package/dist/core/schema/arg.d.mts +92 -13
- package/dist/core/schema/arg.mjs +463 -1
- package/dist/core/schema/command.d.mts +29 -7
- package/dist/core/schema/command.mjs +726 -1
- package/dist/core/schema/flag.d.mts +577 -21
- package/dist/core/schema/flag.mjs +705 -1
- package/dist/core/schema/index.d.mts +5 -2
- package/dist/core/schema/middleware.d.mts +4 -3
- package/dist/core/schema/middleware.mjs +41 -1
- package/dist/core/schema/number-constraints.d.mts +96 -0
- package/dist/core/schema/number-constraints.mjs +60 -0
- package/dist/core/schema/prompt.d.mts +21 -3
- package/dist/core/schema/run.d.mts +24 -5
- package/dist/core/schema/string-constraints.d.mts +97 -0
- package/dist/core/schema/string-constraints.mjs +69 -0
- package/dist/core/schema/value-parsers.d.mts +78 -0
- package/dist/core/schema/value-parsers.mjs +142 -0
- package/dist/core/schema-dsl/runtime.mjs +271 -2
- package/dist/core/schema-dsl/to-json-schema.d.mts +0 -1
- package/dist/core/schema-dsl/to-json-schema.mjs +74 -1
- package/dist/core/testkit/index.d.mts +9 -36
- package/dist/core/testkit/index.mjs +83 -1
- package/dist/index.d.mts +12 -6
- package/dist/index.mjs +22 -1
- package/dist/runtime/adapter.d.mts +38 -2
- package/dist/runtime/adapter.mjs +87 -1
- package/dist/runtime/auto.d.mts +2 -3
- package/dist/runtime/auto.mjs +45 -1
- package/dist/runtime/deno.d.mts +24 -5
- package/dist/runtime/deno.mjs +262 -2
- package/dist/runtime/detect.mjs +45 -1
- package/dist/runtime/index.d.mts +3 -4
- package/dist/runtime/node.d.mts +8 -2
- package/dist/runtime/node.mjs +220 -2
- package/dist/runtime/paths.mjs +48 -1
- package/dist/runtime.d.mts +4 -4
- package/dist/runtime.mjs +6 -1
- package/dist/strings.mjs +22 -1
- package/dist/testkit.d.mts +1 -0
- package/dist/testkit.mjs +5 -1
- package/dist/version.d.mts +7 -3
- package/dist/version.mjs +7 -1
- package/dreamcli.schema.json +36 -7
- package/examples/basic.ts +5 -1
- package/examples/gh/package.json +2 -7
- package/examples/pwsh-demo/package.json +1 -7
- package/examples/pwsh-demo/src/main.test.ts +1 -4
- package/examples/pwsh-demo/src/main.ts +14 -4
- package/examples/transport-launcher.ts +85 -0
- package/package.json +43 -49
- package/dist/runtime/bun.d.mts +0 -28
- package/dist/runtime/bun.mjs +0 -1
- package/dist/runtime/support.d.mts +0 -42
- package/dist/runtime/support.mjs +0 -1
- package/src/core/cli/dispatch.ts +0 -349
- package/src/core/cli/help-links.ts +0 -82
- package/src/core/cli/index.ts +0 -1413
- package/src/core/cli/planner.ts +0 -487
- package/src/core/cli/plugin.ts +0 -117
- package/src/core/cli/propagate.ts +0 -97
- package/src/core/cli/root-help.ts +0 -252
- package/src/core/cli/root-surface.ts +0 -74
- package/src/core/cli/runtime-preflight.ts +0 -438
- package/src/core/completion/index.ts +0 -88
- package/src/core/completion/shells/bash.ts +0 -459
- package/src/core/completion/shells/fish.ts +0 -259
- package/src/core/completion/shells/powershell.ts +0 -439
- package/src/core/completion/shells/shared.ts +0 -283
- package/src/core/completion/shells/zsh.ts +0 -381
- package/src/core/config/index.ts +0 -417
- package/src/core/config/package-json.ts +0 -560
- package/src/core/errors/index.ts +0 -193
- package/src/core/execution/index.ts +0 -268
- package/src/core/help/ansi.ts +0 -144
- package/src/core/help/index.ts +0 -516
- package/src/core/json-schema/index.ts +0 -891
- package/src/core/json-schema/meta-descriptions.generated.ts +0 -185
- package/src/core/output/activity.ts +0 -575
- package/src/core/output/bind.ts +0 -59
- package/src/core/output/contracts.ts +0 -194
- package/src/core/output/display-value.ts +0 -48
- package/src/core/output/index.ts +0 -704
- package/src/core/output/renderers.ts +0 -125
- package/src/core/output/writer.ts +0 -46
- package/src/core/parse/index.ts +0 -681
- package/src/core/prompt/index.ts +0 -583
- package/src/core/resolve/args.ts +0 -144
- package/src/core/resolve/coerce.ts +0 -432
- package/src/core/resolve/config.ts +0 -26
- package/src/core/resolve/contracts.ts +0 -135
- package/src/core/resolve/errors.ts +0 -227
- package/src/core/resolve/flags.ts +0 -255
- package/src/core/resolve/index.ts +0 -108
- package/src/core/resolve/property.ts +0 -99
- package/src/core/schema/activity.ts +0 -240
- package/src/core/schema/arg.ts +0 -719
- package/src/core/schema/command.ts +0 -1498
- package/src/core/schema/flag.ts +0 -801
- package/src/core/schema/index.ts +0 -91
- package/src/core/schema/middleware.ts +0 -171
- package/src/core/schema/prompt.ts +0 -156
- package/src/core/schema/run.ts +0 -198
- package/src/core/schema-dsl/index.ts +0 -109
- package/src/core/schema-dsl/parse.ts +0 -262
- package/src/core/schema-dsl/runtime.ts +0 -609
- package/src/core/schema-dsl/to-json-schema.ts +0 -102
- package/src/core/testkit/index.ts +0 -123
- package/src/index.ts +0 -179
- package/src/runtime/adapter.ts +0 -303
- package/src/runtime/auto.ts +0 -110
- package/src/runtime/bun.ts +0 -55
- package/src/runtime/deno.ts +0 -369
- package/src/runtime/detect.ts +0 -105
- package/src/runtime/index.ts +0 -21
- package/src/runtime/node-builtins.d.ts +0 -27
- package/src/runtime/node.ts +0 -300
- package/src/runtime/paths.ts +0 -64
- package/src/runtime/support.ts +0 -135
- package/src/runtime.ts +0 -31
- package/src/strings.ts +0 -34
- package/src/testkit.ts +0 -25
- package/src/version.ts +0 -20
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
//#region src/core/schema/value-parsers.ts
|
|
2
|
+
/**
|
|
3
|
+
* Strict ISO-8601 shapes accepted by {@link parseDateValue}:
|
|
4
|
+
* `YYYY-MM-DD`, optionally followed by `THH:MM`, `:SS`, `.mmm`, and a
|
|
5
|
+
* `Z` / `±HH:MM` offset. Anything else — including `Date.parse`-lenient
|
|
6
|
+
* inputs like `'0'` or `'March 5'` — is rejected.
|
|
7
|
+
*/
|
|
8
|
+
const ISO_DATE_PATTERN = /^(\d{4})-(\d{2})-(\d{2})(?:T(\d{2}):(\d{2})(?::(\d{2})(?:\.(\d{1,3}))?)?(Z|[+-]\d{2}:\d{2})?)?$/;
|
|
9
|
+
/**
|
|
10
|
+
* Parse a strict ISO-8601 date string into a `Date`.
|
|
11
|
+
*
|
|
12
|
+
* Rejects non-string input, non-ISO shapes, and calendar-invalid components
|
|
13
|
+
* (`2026-02-31` does not silently roll over to March). Offset-less datetimes
|
|
14
|
+
* are interpreted as UTC so results never depend on the machine's timezone.
|
|
15
|
+
*
|
|
16
|
+
* @param raw - Raw flag value.
|
|
17
|
+
* @param options - Optional inclusive date bounds.
|
|
18
|
+
* @returns The parsed `Date`.
|
|
19
|
+
* @throws Error with a human-readable reason on invalid input.
|
|
20
|
+
*/
|
|
21
|
+
function parseDateValue(raw, options) {
|
|
22
|
+
if (raw instanceof Date) return validateDateBounds(raw, options);
|
|
23
|
+
if (typeof raw !== "string") throw new Error(`Invalid date '${String(raw)}': expected an ISO-8601 string`);
|
|
24
|
+
const match = ISO_DATE_PATTERN.exec(raw);
|
|
25
|
+
if (match === null) throw new Error(`Invalid date '${raw}': expected ISO-8601 (e.g. 2026-07-10 or 2026-07-10T14:30:00Z)`);
|
|
26
|
+
const [, yearPart, monthPart, dayPart, hourPart, minutePart, secondPart] = match;
|
|
27
|
+
const year = Number(yearPart);
|
|
28
|
+
const month = Number(monthPart);
|
|
29
|
+
const day = Number(dayPart);
|
|
30
|
+
if (month < 1 || month > 12) throw new Error(`Invalid date '${raw}': month must be 01-12`);
|
|
31
|
+
if (day < 1 || day > daysInMonth(year, month)) throw new Error(`Invalid date '${raw}': not a real calendar date`);
|
|
32
|
+
if (hourPart !== void 0 && Number(hourPart) > 23) throw new Error(`Invalid date '${raw}': hours must be 00-23`);
|
|
33
|
+
if (minutePart !== void 0 && Number(minutePart) > 59) throw new Error(`Invalid date '${raw}': minutes must be 00-59`);
|
|
34
|
+
if (secondPart !== void 0 && Number(secondPart) > 59) throw new Error(`Invalid date '${raw}': seconds must be 00-59`);
|
|
35
|
+
const hasTime = match[4] !== void 0;
|
|
36
|
+
const hasOffset = match[8] !== void 0;
|
|
37
|
+
const parsed = new Date(hasTime && !hasOffset ? `${raw}Z` : raw);
|
|
38
|
+
if (Number.isNaN(parsed.getTime())) throw new Error(`Invalid date '${raw}'`);
|
|
39
|
+
return validateDateBounds(parsed, options, raw);
|
|
40
|
+
}
|
|
41
|
+
/** Number of days in a month (1-12), accounting for leap years. */
|
|
42
|
+
function daysInMonth(year, month) {
|
|
43
|
+
return new Date(Date.UTC(year, month, 0)).getUTCDate();
|
|
44
|
+
}
|
|
45
|
+
/** Enforce inclusive date bounds, throwing a human-readable error on violation. */
|
|
46
|
+
function validateDateBounds(value, options, raw) {
|
|
47
|
+
const display = raw ?? value.toISOString();
|
|
48
|
+
if (options?.min !== void 0 && value.getTime() < options.min.getTime()) throw new Error(`Date '${display}' is before the earliest allowed ${options.min.toISOString()}`);
|
|
49
|
+
if (options?.max !== void 0 && value.getTime() > options.max.getTime()) throw new Error(`Date '${display}' is after the latest allowed ${options.max.toISOString()}`);
|
|
50
|
+
return value;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Parse a URL string into a `URL`, optionally restricting protocols.
|
|
54
|
+
*
|
|
55
|
+
* @param raw - Raw flag value.
|
|
56
|
+
* @param options - Optional protocol allowlist.
|
|
57
|
+
* @returns The parsed `URL`.
|
|
58
|
+
* @throws Error with a human-readable reason on invalid input.
|
|
59
|
+
*/
|
|
60
|
+
function parseUrlValue(raw, options) {
|
|
61
|
+
if (typeof raw !== "string" && !(raw instanceof URL)) throw new Error(`Invalid URL '${String(raw)}': expected a URL string`);
|
|
62
|
+
let parsed;
|
|
63
|
+
try {
|
|
64
|
+
parsed = raw instanceof URL ? raw : new URL(raw);
|
|
65
|
+
} catch {
|
|
66
|
+
throw new Error(`Invalid URL '${String(raw)}'`);
|
|
67
|
+
}
|
|
68
|
+
if (options?.protocols !== void 0) {
|
|
69
|
+
const protocol = parsed.protocol.replace(/:$/, "");
|
|
70
|
+
if (!options.protocols.includes(protocol)) throw new Error(`URL protocol '${protocol}' is not allowed. Allowed: ${options.protocols.join(", ")}`);
|
|
71
|
+
}
|
|
72
|
+
return parsed;
|
|
73
|
+
}
|
|
74
|
+
/** Duration unit suffixes mapped to their length in milliseconds. */
|
|
75
|
+
const DURATION_UNITS = {
|
|
76
|
+
ms: 1,
|
|
77
|
+
s: 1e3,
|
|
78
|
+
m: 6e4,
|
|
79
|
+
h: 36e5,
|
|
80
|
+
d: 864e5
|
|
81
|
+
};
|
|
82
|
+
/**
|
|
83
|
+
* Parse a human duration (`'30s'`, `'5m'`, `'1.5h'`, `'250ms'`, `'2d'`) into
|
|
84
|
+
* milliseconds. A bare number (`'1500'`) is treated as milliseconds. Compound
|
|
85
|
+
* values (`'1h30m'`) are supported.
|
|
86
|
+
*
|
|
87
|
+
* @param raw - Raw flag value.
|
|
88
|
+
* @returns Duration in milliseconds.
|
|
89
|
+
* @throws Error with a human-readable reason on invalid input.
|
|
90
|
+
*/
|
|
91
|
+
function parseDurationValue(raw) {
|
|
92
|
+
if (typeof raw === "number") {
|
|
93
|
+
if (!Number.isFinite(raw) || raw < 0) throw new Error(`Invalid duration '${String(raw)}': must be a non-negative number`);
|
|
94
|
+
return raw;
|
|
95
|
+
}
|
|
96
|
+
if (typeof raw !== "string" || raw.length === 0) throw new Error(`Invalid duration '${String(raw)}': expected e.g. 30s, 5m, 1h30m, or 250ms`);
|
|
97
|
+
if (/^\d+(?:\.\d+)?$/.test(raw)) return Number(raw);
|
|
98
|
+
const segmentPattern = /(\d+(?:\.\d+)?)(ms|s|m|h|d)/gy;
|
|
99
|
+
let total = 0;
|
|
100
|
+
let consumed = 0;
|
|
101
|
+
for (const segment of raw.matchAll(segmentPattern)) {
|
|
102
|
+
const [text, amount, unit] = segment;
|
|
103
|
+
const scale = unit === void 0 ? void 0 : DURATION_UNITS[unit];
|
|
104
|
+
if (amount === void 0 || scale === void 0) break;
|
|
105
|
+
total += Number(amount) * scale;
|
|
106
|
+
consumed += text.length;
|
|
107
|
+
}
|
|
108
|
+
if (consumed !== raw.length || consumed === 0) throw new Error(`Invalid duration '${raw}': expected e.g. 30s, 5m, 1h30m, or 250ms`);
|
|
109
|
+
return total;
|
|
110
|
+
}
|
|
111
|
+
/** Byte unit suffixes mapped to their size (binary, 1 kb = 1024 bytes). */
|
|
112
|
+
const BYTE_UNITS = {
|
|
113
|
+
b: 1,
|
|
114
|
+
kb: 1024,
|
|
115
|
+
mb: 1024 ** 2,
|
|
116
|
+
gb: 1024 ** 3,
|
|
117
|
+
tb: 1024 ** 4
|
|
118
|
+
};
|
|
119
|
+
/**
|
|
120
|
+
* Parse a human byte size (`'512mb'`, `'1.5gb'`, `'64kb'`, `'100b'`) into a
|
|
121
|
+
* byte count. Units are binary (`1kb` = 1024 bytes) and case-insensitive; a
|
|
122
|
+
* bare number is treated as bytes.
|
|
123
|
+
*
|
|
124
|
+
* @param raw - Raw flag value.
|
|
125
|
+
* @returns Size in bytes.
|
|
126
|
+
* @throws Error with a human-readable reason on invalid input.
|
|
127
|
+
*/
|
|
128
|
+
function parseBytesValue(raw) {
|
|
129
|
+
if (typeof raw === "number") {
|
|
130
|
+
if (!Number.isFinite(raw) || raw < 0) throw new Error(`Invalid size '${String(raw)}': must be a non-negative number`);
|
|
131
|
+
return raw;
|
|
132
|
+
}
|
|
133
|
+
if (typeof raw !== "string" || raw.length === 0) throw new Error(`Invalid size '${String(raw)}': expected e.g. 512mb, 1.5gb, or 64kb`);
|
|
134
|
+
const match = /^(\d+(?:\.\d+)?)\s*(b|kb|mb|gb|tb)?$/i.exec(raw);
|
|
135
|
+
if (match === null || match[1] === void 0) throw new Error(`Invalid size '${raw}': expected e.g. 512mb, 1.5gb, or 64kb`);
|
|
136
|
+
const unit = (match[2] ?? "b").toLowerCase();
|
|
137
|
+
const scale = BYTE_UNITS[unit];
|
|
138
|
+
if (scale === void 0) throw new Error(`Invalid size '${raw}': unknown unit '${match[2] ?? ""}'`);
|
|
139
|
+
return Math.round(Number(match[1]) * scale);
|
|
140
|
+
}
|
|
141
|
+
//#endregion
|
|
142
|
+
export { parseBytesValue, parseDateValue, parseDurationValue, parseUrlValue };
|
|
@@ -1,2 +1,271 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
//#region src/core/schema-dsl/runtime.ts
|
|
2
|
+
/** Map single characters to their token kinds. */
|
|
3
|
+
const SINGLE_CHAR_TOKENS = {
|
|
4
|
+
"{": "lbrace",
|
|
5
|
+
"}": "rbrace",
|
|
6
|
+
"[": "lbracket",
|
|
7
|
+
"]": "rbracket",
|
|
8
|
+
"<": "langle",
|
|
9
|
+
">": "rangle",
|
|
10
|
+
",": "comma",
|
|
11
|
+
":": "colon",
|
|
12
|
+
";": "semicolon",
|
|
13
|
+
"?": "question",
|
|
14
|
+
"|": "pipe"
|
|
15
|
+
};
|
|
16
|
+
/** Whitespace characters to skip. */
|
|
17
|
+
function isWhitespace(c) {
|
|
18
|
+
return c === " " || c === "\n" || c === " " || c === "\r";
|
|
19
|
+
}
|
|
20
|
+
/** Identifier-valid characters (a-z, A-Z, 0-9, _, $). */
|
|
21
|
+
function isIdentChar(c) {
|
|
22
|
+
return c >= "a" && c <= "z" || c >= "A" && c <= "Z" || c >= "0" && c <= "9" || c === "_" || c === "$";
|
|
23
|
+
}
|
|
24
|
+
/** Identifier start characters (a-z, A-Z, _, $). */
|
|
25
|
+
function isIdentStart(c) {
|
|
26
|
+
return c >= "a" && c <= "z" || c >= "A" && c <= "Z" || c === "_" || c === "$";
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Tokenize a schema source string.
|
|
30
|
+
*
|
|
31
|
+
* Produces a flat array of tokens: identifiers (`string`, `number`, etc.),
|
|
32
|
+
* punctuation (`{`, `}`, `[`, `]`, `:`, `;`, `?`, `|`), and skips
|
|
33
|
+
* whitespace. Throws on unrecognized characters.
|
|
34
|
+
*/
|
|
35
|
+
function tokenize(source) {
|
|
36
|
+
const tokens = [];
|
|
37
|
+
let i = 0;
|
|
38
|
+
while (i < source.length) {
|
|
39
|
+
const c = source[i] ?? "";
|
|
40
|
+
if (isWhitespace(c)) {
|
|
41
|
+
i++;
|
|
42
|
+
continue;
|
|
43
|
+
}
|
|
44
|
+
const singleKind = SINGLE_CHAR_TOKENS[c];
|
|
45
|
+
if (singleKind !== void 0) {
|
|
46
|
+
tokens.push({
|
|
47
|
+
kind: singleKind,
|
|
48
|
+
value: c
|
|
49
|
+
});
|
|
50
|
+
i++;
|
|
51
|
+
continue;
|
|
52
|
+
}
|
|
53
|
+
if (c === "'") {
|
|
54
|
+
let lit = "";
|
|
55
|
+
i++;
|
|
56
|
+
while (i < source.length && source[i] !== "'") {
|
|
57
|
+
lit += source[i];
|
|
58
|
+
i++;
|
|
59
|
+
}
|
|
60
|
+
if (i >= source.length) throw new SyntaxError("Unterminated string literal");
|
|
61
|
+
i++;
|
|
62
|
+
tokens.push({
|
|
63
|
+
kind: "string_literal",
|
|
64
|
+
value: lit
|
|
65
|
+
});
|
|
66
|
+
continue;
|
|
67
|
+
}
|
|
68
|
+
if (c === "@") {
|
|
69
|
+
i++;
|
|
70
|
+
let ref = "";
|
|
71
|
+
while (i < source.length) {
|
|
72
|
+
const ch = source[i] ?? "";
|
|
73
|
+
if (!isIdentChar(ch)) break;
|
|
74
|
+
ref += ch;
|
|
75
|
+
i++;
|
|
76
|
+
}
|
|
77
|
+
if (ref.length === 0) throw new SyntaxError(`Expected identifier after '@' at position ${String(i)}`);
|
|
78
|
+
tokens.push({
|
|
79
|
+
kind: "ref",
|
|
80
|
+
value: ref
|
|
81
|
+
});
|
|
82
|
+
continue;
|
|
83
|
+
}
|
|
84
|
+
if (isIdentStart(c)) {
|
|
85
|
+
let ident = "";
|
|
86
|
+
while (i < source.length) {
|
|
87
|
+
const ch = source[i] ?? "";
|
|
88
|
+
if (!isIdentChar(ch)) break;
|
|
89
|
+
ident += ch;
|
|
90
|
+
i++;
|
|
91
|
+
}
|
|
92
|
+
tokens.push({
|
|
93
|
+
kind: "ident",
|
|
94
|
+
value: ident
|
|
95
|
+
});
|
|
96
|
+
continue;
|
|
97
|
+
}
|
|
98
|
+
throw new SyntaxError(`Unexpected character '${c}' at position ${String(i)}`);
|
|
99
|
+
}
|
|
100
|
+
return tokens;
|
|
101
|
+
}
|
|
102
|
+
/** Primitive type names recognized by the parser. */
|
|
103
|
+
const PRIMITIVES = /* @__PURE__ */ new Set([
|
|
104
|
+
"string",
|
|
105
|
+
"number",
|
|
106
|
+
"integer",
|
|
107
|
+
"boolean",
|
|
108
|
+
"true",
|
|
109
|
+
"false",
|
|
110
|
+
"null",
|
|
111
|
+
"undefined",
|
|
112
|
+
"unknown",
|
|
113
|
+
"never"
|
|
114
|
+
]);
|
|
115
|
+
/** Map a primitive name to its AST node. */
|
|
116
|
+
function primitiveNode(name) {
|
|
117
|
+
switch (name) {
|
|
118
|
+
case "string": return { kind: "string" };
|
|
119
|
+
case "number": return { kind: "number" };
|
|
120
|
+
case "integer": return { kind: "integer" };
|
|
121
|
+
case "boolean": return { kind: "boolean" };
|
|
122
|
+
case "true": return { kind: "true" };
|
|
123
|
+
case "false": return { kind: "false" };
|
|
124
|
+
case "null": return { kind: "null" };
|
|
125
|
+
case "undefined": return { kind: "undefined" };
|
|
126
|
+
case "unknown": return { kind: "unknown" };
|
|
127
|
+
case "never": return { kind: "never" };
|
|
128
|
+
default: throw new SyntaxError(`Unknown type '${name}'`);
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* Parse a token stream into a {@link SchemaNode} AST.
|
|
133
|
+
*
|
|
134
|
+
* Implements the grammar documented at the module level using standard
|
|
135
|
+
* recursive descent. Each grammar rule maps to one `parse*` method.
|
|
136
|
+
*/
|
|
137
|
+
var SchemaParser = class {
|
|
138
|
+
tokens;
|
|
139
|
+
pos = 0;
|
|
140
|
+
constructor(tokens) {
|
|
141
|
+
this.tokens = tokens;
|
|
142
|
+
}
|
|
143
|
+
/** Parse the full token stream, asserting all tokens are consumed. */
|
|
144
|
+
parseRoot() {
|
|
145
|
+
const result = this.parseValue();
|
|
146
|
+
if (this.pos < this.tokens.length) {
|
|
147
|
+
const leftover = this.peek();
|
|
148
|
+
throw new SyntaxError(`Unexpected token '${leftover?.value ?? "?"}' at position ${String(this.pos)}`);
|
|
149
|
+
}
|
|
150
|
+
return result;
|
|
151
|
+
}
|
|
152
|
+
/** value = union */
|
|
153
|
+
parseValue() {
|
|
154
|
+
return this.parseUnion();
|
|
155
|
+
}
|
|
156
|
+
/** union = postfix ('|' postfix)* */
|
|
157
|
+
parseUnion() {
|
|
158
|
+
const first = this.parsePostfix();
|
|
159
|
+
if (!this.check("pipe")) return first;
|
|
160
|
+
const members = [first];
|
|
161
|
+
while (this.match("pipe")) members.push(this.parsePostfix());
|
|
162
|
+
return {
|
|
163
|
+
kind: "union",
|
|
164
|
+
members
|
|
165
|
+
};
|
|
166
|
+
}
|
|
167
|
+
/** postfix = primary ('[]')* */
|
|
168
|
+
parsePostfix() {
|
|
169
|
+
let node = this.parsePrimary();
|
|
170
|
+
while (this.check("lbracket") && this.checkAt(this.pos + 1, "rbracket")) {
|
|
171
|
+
this.advance();
|
|
172
|
+
this.advance();
|
|
173
|
+
node = {
|
|
174
|
+
kind: "array",
|
|
175
|
+
element: node
|
|
176
|
+
};
|
|
177
|
+
}
|
|
178
|
+
return node;
|
|
179
|
+
}
|
|
180
|
+
/** primary = object | record | ref | literal | IDENT */
|
|
181
|
+
parsePrimary() {
|
|
182
|
+
if (this.check("lbrace")) return this.parseObject();
|
|
183
|
+
if (this.check("string_literal")) return {
|
|
184
|
+
kind: "literal",
|
|
185
|
+
value: this.advance().value
|
|
186
|
+
};
|
|
187
|
+
if (this.check("ref")) return {
|
|
188
|
+
kind: "ref",
|
|
189
|
+
target: this.advance().value
|
|
190
|
+
};
|
|
191
|
+
const name = this.expectIdent();
|
|
192
|
+
if (name === "Record") {
|
|
193
|
+
this.expect("langle");
|
|
194
|
+
if (this.parseValue().kind !== "string") throw new SyntaxError("Record key type must be 'string' because JSON object keys are always strings");
|
|
195
|
+
this.expect("comma");
|
|
196
|
+
const valueSchema = this.parseValue();
|
|
197
|
+
this.expect("rangle");
|
|
198
|
+
return {
|
|
199
|
+
kind: "record",
|
|
200
|
+
value: valueSchema
|
|
201
|
+
};
|
|
202
|
+
}
|
|
203
|
+
if (!PRIMITIVES.has(name)) throw new SyntaxError(`Unknown type '${name}' at position ${String(this.pos - 1)}`);
|
|
204
|
+
return primitiveNode(name);
|
|
205
|
+
}
|
|
206
|
+
/** object = '{' properties '}' */
|
|
207
|
+
parseObject() {
|
|
208
|
+
this.expect("lbrace");
|
|
209
|
+
const properties = {};
|
|
210
|
+
while (!this.check("rbrace")) {
|
|
211
|
+
const name = this.expectIdent();
|
|
212
|
+
if (Object.hasOwn(properties, name)) throw new SyntaxError(`Duplicate property '${name}' at position ${String(this.pos - 1)}`);
|
|
213
|
+
const optional = this.match("question");
|
|
214
|
+
this.expect("colon");
|
|
215
|
+
properties[name] = {
|
|
216
|
+
optional,
|
|
217
|
+
schema: this.parseValue()
|
|
218
|
+
};
|
|
219
|
+
this.match("semicolon");
|
|
220
|
+
}
|
|
221
|
+
this.expect("rbrace");
|
|
222
|
+
return {
|
|
223
|
+
kind: "object",
|
|
224
|
+
properties
|
|
225
|
+
};
|
|
226
|
+
}
|
|
227
|
+
peek() {
|
|
228
|
+
return this.tokens[this.pos];
|
|
229
|
+
}
|
|
230
|
+
check(kind) {
|
|
231
|
+
return this.peek()?.kind === kind;
|
|
232
|
+
}
|
|
233
|
+
checkAt(pos, kind) {
|
|
234
|
+
return this.tokens[pos]?.kind === kind;
|
|
235
|
+
}
|
|
236
|
+
advance() {
|
|
237
|
+
const token = this.tokens[this.pos];
|
|
238
|
+
if (token === void 0) throw new SyntaxError("Unexpected end of input");
|
|
239
|
+
this.pos++;
|
|
240
|
+
return token;
|
|
241
|
+
}
|
|
242
|
+
match(kind) {
|
|
243
|
+
if (this.check(kind)) {
|
|
244
|
+
this.pos++;
|
|
245
|
+
return true;
|
|
246
|
+
}
|
|
247
|
+
return false;
|
|
248
|
+
}
|
|
249
|
+
expect(kind) {
|
|
250
|
+
if (!this.check(kind)) {
|
|
251
|
+
const got = this.peek();
|
|
252
|
+
throw new SyntaxError(got ? `Expected '${kind}' but got '${got.kind}' ('${got.value}') at position ${String(this.pos)}` : `Expected '${kind}' but reached end of input`);
|
|
253
|
+
}
|
|
254
|
+
return this.advance();
|
|
255
|
+
}
|
|
256
|
+
expectIdent() {
|
|
257
|
+
return this.expect("ident").value;
|
|
258
|
+
}
|
|
259
|
+
};
|
|
260
|
+
/**
|
|
261
|
+
* Parse a schema source string into a runtime AST.
|
|
262
|
+
*
|
|
263
|
+
* @param source - Schema string (e.g. `"{ name: string; age: number }"`).
|
|
264
|
+
* @returns The root {@link SchemaNode}.
|
|
265
|
+
* @throws {@link SyntaxError} on malformed input.
|
|
266
|
+
*/
|
|
267
|
+
function parseSchema(source) {
|
|
268
|
+
return new SchemaParser(tokenize(source)).parseRoot();
|
|
269
|
+
}
|
|
270
|
+
//#endregion
|
|
271
|
+
export { parseSchema };
|
|
@@ -1 +1,74 @@
|
|
|
1
|
-
|
|
1
|
+
//#region src/core/schema-dsl/to-json-schema.ts
|
|
2
|
+
/**
|
|
3
|
+
* Convert a {@link SchemaNode} AST node to a JSON Schema fragment.
|
|
4
|
+
*
|
|
5
|
+
* Mapping:
|
|
6
|
+
* - `string` → `{ type: 'string' }`
|
|
7
|
+
* - `number` → `{ type: 'number' }`
|
|
8
|
+
* - `integer` → `{ type: 'integer' }`
|
|
9
|
+
* - `boolean` → `{ type: 'boolean' }`
|
|
10
|
+
* - `true` → `{ const: true }`
|
|
11
|
+
* - `false` → `{ const: false }`
|
|
12
|
+
* - `null` → `{ type: 'null' }`
|
|
13
|
+
* - `undefined` → throws (JSON Schema has no `undefined` type)
|
|
14
|
+
* - `unknown` → `{}` (accepts any value)
|
|
15
|
+
* - `never` → `{ not: {} }` (rejects all values)
|
|
16
|
+
* - `'literal'` → `{ const: 'literal' }`
|
|
17
|
+
* - `@ref` → `{ $ref: '#/$defs/ref' }`
|
|
18
|
+
* - `T[]` → `{ type: 'array', items: convert(T) }`
|
|
19
|
+
* - `Record<K,V>` → `{ type: 'object', additionalProperties: convert(V) }`
|
|
20
|
+
* - `{...}` → `{ type: 'object', properties: ..., required: [...] }`
|
|
21
|
+
* - `A | B` → `{ enum: [...] }` if all literals, else `{ oneOf: [...] }`
|
|
22
|
+
*
|
|
23
|
+
* @param node - The AST node to convert.
|
|
24
|
+
* @returns A plain JSON Schema fragment.
|
|
25
|
+
*/
|
|
26
|
+
function nodeToJsonSchema(node) {
|
|
27
|
+
switch (node.kind) {
|
|
28
|
+
case "string": return { type: "string" };
|
|
29
|
+
case "number": return { type: "number" };
|
|
30
|
+
case "integer": return { type: "integer" };
|
|
31
|
+
case "boolean": return { type: "boolean" };
|
|
32
|
+
case "true": return { const: true };
|
|
33
|
+
case "false": return { const: false };
|
|
34
|
+
case "null": return { type: "null" };
|
|
35
|
+
case "literal": return { const: node.value };
|
|
36
|
+
case "undefined": throw new Error("Cannot convert 'undefined' type to JSON Schema; model optionality at the parent level");
|
|
37
|
+
case "unknown": return {};
|
|
38
|
+
case "never": return { not: {} };
|
|
39
|
+
case "ref": return { $ref: `#/$defs/${node.target}` };
|
|
40
|
+
case "array": return {
|
|
41
|
+
type: "array",
|
|
42
|
+
items: nodeToJsonSchema(node.element)
|
|
43
|
+
};
|
|
44
|
+
case "record": return {
|
|
45
|
+
type: "object",
|
|
46
|
+
additionalProperties: nodeToJsonSchema(node.value)
|
|
47
|
+
};
|
|
48
|
+
case "object": {
|
|
49
|
+
const properties = {};
|
|
50
|
+
const required = [];
|
|
51
|
+
for (const [key, prop] of Object.entries(node.properties)) {
|
|
52
|
+
properties[key] = nodeToJsonSchema(prop.schema);
|
|
53
|
+
if (!prop.optional) required.push(key);
|
|
54
|
+
}
|
|
55
|
+
const result = {
|
|
56
|
+
type: "object",
|
|
57
|
+
additionalProperties: false,
|
|
58
|
+
properties
|
|
59
|
+
};
|
|
60
|
+
if (required.length > 0) result.required = required;
|
|
61
|
+
return result;
|
|
62
|
+
}
|
|
63
|
+
case "union": {
|
|
64
|
+
const literalValues = [];
|
|
65
|
+
let allLiteral = true;
|
|
66
|
+
for (const member of node.members) if (member.kind === "literal") literalValues.push(member.value);
|
|
67
|
+
else allLiteral = false;
|
|
68
|
+
if (allLiteral) return { enum: literalValues };
|
|
69
|
+
return { oneOf: node.members.map(nodeToJsonSchema) };
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
//#endregion
|
|
74
|
+
export { nodeToJsonSchema };
|
|
@@ -1,54 +1,27 @@
|
|
|
1
1
|
import { RunOptions, RunResult } from "../schema/run.mjs";
|
|
2
2
|
import { RunnableCommand } from "../schema/command.mjs";
|
|
3
|
-
|
|
4
3
|
//#region src/core/testkit/index.d.ts
|
|
5
4
|
/**
|
|
6
5
|
* Run a command builder against the given argv with injected options.
|
|
7
6
|
*
|
|
8
7
|
* This is the testkit wrapper around the shared executor:
|
|
9
|
-
* 1.
|
|
10
|
-
* 2.
|
|
11
|
-
* 3.
|
|
12
|
-
* 4.
|
|
8
|
+
* 1. Apply the CLI root-flag layer (detect/strip `--json`) so copied real argv works
|
|
9
|
+
* 2. Create or reuse capture output
|
|
10
|
+
* 3. Build standalone schema/meta defaults when CLI dispatch did not
|
|
11
|
+
* 4. Delegate parse -> resolve -> execute to the shared executor
|
|
12
|
+
* 5. Return the structured result with captured buffers
|
|
13
13
|
*
|
|
14
14
|
* Errors are normalized by the shared executor into structured {@linkcode RunResult}s
|
|
15
15
|
* with appropriate exit codes. The function never throws.
|
|
16
16
|
*
|
|
17
17
|
* @param cmd - The command builder (must have an action handler)
|
|
18
|
-
* @param argv - Raw argv strings (NOT including the command name itself)
|
|
18
|
+
* @param argv - Raw argv strings (NOT including the command name itself). A
|
|
19
|
+
* CLI-level `--json` before the `--` separator is honored and stripped here,
|
|
20
|
+
* just like the real CLI root — so `['--json']` enables JSON mode rather than
|
|
21
|
+
* failing as an unknown flag. Equivalent to passing `{ jsonMode: true }`.
|
|
19
22
|
* @param options - Injectable runtime state
|
|
20
23
|
* @returns Structured run result with exit code and captured output
|
|
21
24
|
*/
|
|
22
25
|
declare function runCommand(cmd: RunnableCommand, argv: readonly string[], options?: RunOptions): Promise<RunResult>;
|
|
23
|
-
/**
|
|
24
|
-
* Options for {@linkcode runCommand} — process-free command execution.
|
|
25
|
-
*
|
|
26
|
-
* Every field is optional. Inject env, config, prompt answers, and
|
|
27
|
-
* output capture without touching process state.
|
|
28
|
-
*
|
|
29
|
-
* @example
|
|
30
|
-
* ```ts
|
|
31
|
-
* const result = await runCommand(cmd, ['--verbose'], {
|
|
32
|
-
* env: { LOG_LEVEL: 'debug' },
|
|
33
|
-
* config: { theme: 'dark' },
|
|
34
|
-
* });
|
|
35
|
-
* ```
|
|
36
|
-
*/
|
|
37
|
-
/**
|
|
38
|
-
* Structured result from {@linkcode runCommand}.
|
|
39
|
-
*
|
|
40
|
-
* Contains the exit code, captured stdout/stderr output, recorded
|
|
41
|
-
* {@linkcode ActivityEvent | activity events}, and an `error` field
|
|
42
|
-
* that is `undefined` on success and a {@linkcode CLIError} on failure.
|
|
43
|
-
*
|
|
44
|
-
* @example
|
|
45
|
-
* ```ts
|
|
46
|
-
* const result = await runCommand(greetCmd, ['World']);
|
|
47
|
-
*
|
|
48
|
-
* expect(result.exitCode).toBe(0);
|
|
49
|
-
* expect(result.stdout).toContain('Hello, World!');
|
|
50
|
-
* expect(result.error).toBeUndefined();
|
|
51
|
-
* ```
|
|
52
|
-
*/
|
|
53
26
|
//#endregion
|
|
54
27
|
export { type RunOptions, type RunResult, runCommand };
|
|
@@ -1 +1,83 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { includesBeforeSeparator, stripBeforeSeparator } from "../parse/index.mjs";
|
|
2
|
+
import { createCaptureOutput } from "../output/index.mjs";
|
|
3
|
+
import { buildRunResult, executeCommand } from "../execution/index.mjs";
|
|
4
|
+
//#region src/core/testkit/index.ts
|
|
5
|
+
/**
|
|
6
|
+
* Test harness: command.run() with injected state, output capture.
|
|
7
|
+
*
|
|
8
|
+
* Provides {@linkcode runCommand()} — the in-process test harness wrapper over DreamCLI's
|
|
9
|
+
* shared executor.\
|
|
10
|
+
* It wires capture output and standalone command metadata,
|
|
11
|
+
* then returns a structured {@linkcode RunResult} with exitCode and captured output.
|
|
12
|
+
*
|
|
13
|
+
* This keeps command tests process-free without testkit owning the canonical pipeline:
|
|
14
|
+
*
|
|
15
|
+
* ```text
|
|
16
|
+
* parse -> resolve -> execute
|
|
17
|
+
* ```
|
|
18
|
+
* @module dreamcli/core/testkit
|
|
19
|
+
*/
|
|
20
|
+
/**
|
|
21
|
+
* Run a command builder against the given argv with injected options.
|
|
22
|
+
*
|
|
23
|
+
* This is the testkit wrapper around the shared executor:
|
|
24
|
+
* 1. Apply the CLI root-flag layer (detect/strip `--json`) so copied real argv works
|
|
25
|
+
* 2. Create or reuse capture output
|
|
26
|
+
* 3. Build standalone schema/meta defaults when CLI dispatch did not
|
|
27
|
+
* 4. Delegate parse -> resolve -> execute to the shared executor
|
|
28
|
+
* 5. Return the structured result with captured buffers
|
|
29
|
+
*
|
|
30
|
+
* Errors are normalized by the shared executor into structured {@linkcode RunResult}s
|
|
31
|
+
* with appropriate exit codes. The function never throws.
|
|
32
|
+
*
|
|
33
|
+
* @param cmd - The command builder (must have an action handler)
|
|
34
|
+
* @param argv - Raw argv strings (NOT including the command name itself). A
|
|
35
|
+
* CLI-level `--json` before the `--` separator is honored and stripped here,
|
|
36
|
+
* just like the real CLI root — so `['--json']` enables JSON mode rather than
|
|
37
|
+
* failing as an unknown flag. Equivalent to passing `{ jsonMode: true }`.
|
|
38
|
+
* @param options - Injectable runtime state
|
|
39
|
+
* @returns Structured run result with exit code and captured output
|
|
40
|
+
*/
|
|
41
|
+
async function runCommand(cmd, argv, options) {
|
|
42
|
+
const hasJsonFlag = includesBeforeSeparator(argv, "--json");
|
|
43
|
+
const jsonMode = hasJsonFlag || options?.jsonMode === true;
|
|
44
|
+
const effectiveArgv = hasJsonFlag ? stripBeforeSeparator(argv, "--json") : argv;
|
|
45
|
+
let out;
|
|
46
|
+
let captured;
|
|
47
|
+
if (options?.out !== void 0) {
|
|
48
|
+
out = options.out;
|
|
49
|
+
captured = options.captured ?? {
|
|
50
|
+
stdout: [],
|
|
51
|
+
stderr: [],
|
|
52
|
+
activity: []
|
|
53
|
+
};
|
|
54
|
+
} else {
|
|
55
|
+
const captureOptions = {
|
|
56
|
+
...options?.verbosity !== void 0 ? { verbosity: options.verbosity } : {},
|
|
57
|
+
...jsonMode ? { jsonMode } : {},
|
|
58
|
+
...options?.isTTY !== void 0 ? { isTTY: options.isTTY } : {}
|
|
59
|
+
};
|
|
60
|
+
[out, captured] = createCaptureOutput(Object.keys(captureOptions).length > 0 ? captureOptions : void 0);
|
|
61
|
+
}
|
|
62
|
+
const schema = options?.mergedSchema ?? cmd.schema;
|
|
63
|
+
const meta = options?.meta ?? {
|
|
64
|
+
name: schema.name,
|
|
65
|
+
bin: options?.help?.binName ?? schema.name,
|
|
66
|
+
version: void 0,
|
|
67
|
+
command: schema.name
|
|
68
|
+
};
|
|
69
|
+
const effectiveOptions = options !== void 0 ? {
|
|
70
|
+
...options,
|
|
71
|
+
...jsonMode ? { jsonMode } : {}
|
|
72
|
+
} : jsonMode ? { jsonMode } : void 0;
|
|
73
|
+
return buildRunResult(await executeCommand({
|
|
74
|
+
command: cmd,
|
|
75
|
+
argv: effectiveArgv,
|
|
76
|
+
out,
|
|
77
|
+
schema,
|
|
78
|
+
meta,
|
|
79
|
+
...effectiveOptions !== void 0 ? { options: effectiveOptions } : {}
|
|
80
|
+
}), captured);
|
|
81
|
+
}
|
|
82
|
+
//#endregion
|
|
83
|
+
export { runCommand };
|