@kjanat/dreamcli 3.0.0-rc.1 → 3.0.0-rc.3
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 +37 -1
- package/dist/core/cli/dispatch.mjs +214 -1
- package/dist/core/cli/help-links.mjs +41 -1
- package/dist/core/cli/index.mjs +901 -1
- package/dist/core/cli/planner.mjs +300 -1
- package/dist/core/cli/plugin.mjs +41 -1
- package/dist/core/cli/propagate.mjs +57 -1
- package/dist/core/cli/root-help.mjs +208 -4
- package/dist/core/cli/root-surface.mjs +27 -1
- package/dist/core/cli/runtime-preflight.mjs +168 -1
- package/dist/core/completion/index.mjs +90 -1
- package/dist/core/completion/shells/bash.mjs +304 -3
- package/dist/core/completion/shells/fish.mjs +159 -2
- package/dist/core/completion/shells/powershell.mjs +353 -2
- package/dist/core/completion/shells/shared.mjs +154 -1
- package/dist/core/completion/shells/zsh.mjs +244 -2
- package/dist/core/config/index.mjs +246 -1
- package/dist/core/config/package-json.mjs +385 -3
- package/dist/core/errors/index.mjs +87 -1
- package/dist/core/execution/index.mjs +189 -1
- package/dist/core/help/ansi.mjs +122 -2
- package/dist/core/help/index.mjs +329 -7
- package/dist/core/json-schema/index.mjs +487 -9
- package/dist/core/json-schema/meta-descriptions.generated.mjs +93 -28
- package/dist/core/output/activity.mjs +452 -1
- package/dist/core/output/bind.mjs +56 -1
- 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 -0
- package/dist/core/output/index.mjs +442 -2
- package/dist/core/output/renderers.mjs +46 -1
- package/dist/core/output/writer.mjs +12 -1
- package/dist/core/parse/index.mjs +555 -1
- package/dist/core/prompt/index.d.mts +0 -1
- package/dist/core/prompt/index.mjs +348 -6
- package/dist/core/resolve/args.mjs +105 -1
- package/dist/core/resolve/coerce.mjs +279 -1
- package/dist/core/resolve/config.mjs +19 -1
- package/dist/core/resolve/errors.mjs +140 -2
- package/dist/core/resolve/flags.mjs +196 -1
- package/dist/core/resolve/index.mjs +85 -1
- package/dist/core/resolve/property.d.mts +0 -1
- package/dist/core/resolve/property.mjs +45 -1
- package/dist/core/schema/arg.mjs +463 -1
- package/dist/core/schema/command.d.mts +18 -0
- package/dist/core/schema/command.mjs +712 -1
- package/dist/core/schema/flag.mjs +453 -1
- package/dist/core/schema/middleware.mjs +41 -1
- package/dist/core/schema/number-constraints.mjs +60 -1
- package/dist/core/schema-dsl/runtime.mjs +271 -2
- package/dist/core/schema-dsl/to-json-schema.mjs +74 -1
- package/dist/core/testkit/index.mjs +83 -1
- package/dist/index.d.mts +2 -1
- package/dist/index.mjs +22 -1
- package/dist/runtime/adapter.mjs +82 -1
- package/dist/runtime/auto.mjs +46 -1
- package/dist/runtime/bun.mjs +28 -1
- package/dist/runtime/deno.mjs +217 -2
- package/dist/runtime/detect.mjs +45 -1
- package/dist/runtime/node.mjs +169 -2
- package/dist/runtime/paths.mjs +48 -1
- package/dist/runtime.mjs +7 -1
- package/dist/strings.mjs +22 -1
- package/dist/testkit.mjs +5 -1
- package/dist/version.d.mts +7 -3
- package/dist/version.mjs +7 -1
- package/package.json +21 -26
- package/src/core/cli/dispatch.ts +0 -355
- package/src/core/cli/help-links.ts +0 -82
- package/src/core/cli/index.ts +0 -1614
- package/src/core/cli/planner.ts +0 -656
- package/src/core/cli/plugin.ts +0 -117
- package/src/core/cli/propagate.ts +0 -97
- package/src/core/cli/root-help.ts +0 -342
- package/src/core/cli/root-surface.ts +0 -87
- package/src/core/cli/runtime-preflight.ts +0 -456
- package/src/core/completion/index.ts +0 -138
- 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 -310
- 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 -265
- package/src/core/help/ansi.ts +0 -144
- package/src/core/help/index.ts +0 -563
- package/src/core/json-schema/index.ts +0 -910
- package/src/core/json-schema/meta-descriptions.generated.ts +0 -202
- 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 -742
- package/src/core/prompt/index.ts +0 -623
- package/src/core/resolve/args.ts +0 -144
- package/src/core/resolve/coerce.ts +0 -500
- 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 -269
- 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 -841
- package/src/core/schema/command.ts +0 -1500
- package/src/core/schema/flag.ts +0 -917
- package/src/core/schema/index.ts +0 -96
- package/src/core/schema/middleware.ts +0 -171
- package/src/core/schema/number-constraints.ts +0 -153
- package/src/core/schema/prompt.ts +0 -171
- 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 -147
- package/src/index.ts +0 -181
- package/src/runtime/adapter.ts +0 -303
- package/src/runtime/auto.ts +0 -86
- package/src/runtime/bun.ts +0 -48
- package/src/runtime/deno.ts +0 -367
- 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 -289
- package/src/runtime/paths.ts +0 -64
- package/src/runtime/support.ts +0 -77
- package/src/runtime.ts +0 -31
- package/src/schema.ts +0 -16
- package/src/strings.ts +0 -34
- package/src/testkit.ts +0 -25
- package/src/version.ts +0 -20
|
@@ -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 +1,83 @@
|
|
|
1
|
-
import{createCaptureOutput
|
|
1
|
+
import { createCaptureOutput } from "../output/index.mjs";
|
|
2
|
+
import { includesBeforeSeparator, stripBeforeSeparator } from "../parse/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 };
|
package/dist/index.d.mts
CHANGED
|
@@ -28,4 +28,5 @@ import { SHELLS, Shell, generateCompletion } from "./core/completion/index.mjs";
|
|
|
28
28
|
import { ConfigAdapter, ConfigDiscoveryOptions, ConfigDiscoveryResult, ConfigFound, ConfigNotFound, FormatLoader, buildConfigSearchPaths, configFormat, discoverConfig } from "./core/config/index.mjs";
|
|
29
29
|
import { CLIBuilder, CLIOptions, CLIRunOptions, CLISchema, ConfigSettings, InferNameOption, ManifestPresetSettings, ManifestSettings, PackageJsonSettings, ResolvedManifestSettings, cli } from "./core/cli/index.mjs";
|
|
30
30
|
import { JsonSchemaOptions, definitionMetaSchema, generateInputSchema, generateSchema } from "./core/json-schema/index.mjs";
|
|
31
|
-
|
|
31
|
+
import { Colors } from "ansispeck";
|
|
32
|
+
export { type ActionHandler, type ActionParams, type ActivityEvent, type AnyCommandBuilder, ArgBuilder, type ArgConfig, type ArgFactory, type ArgKind, type ArgParseFn, type ArgPresence, type ArgSchema, type BeforeParseParams, CLIBuilder, CLIError, type CLIErrorJSON, type CLIErrorOptions, type CLIOptions, type CLIPlugin, type CLIPluginHooks, type CLIRunOptions, type CLISchema, type Colors, type CommandArgEntry, CommandBuilder, type CommandConfig, type CommandExample, type CommandMeta, type CommandSchema, type CompletionOptions, type ConfigAdapter, type ConfigDiscoveryOptions, type ConfigDiscoveryResult, type ConfigFound, type ConfigNotFound, type ConfigSettings, type ConfirmPromptConfig, type DeprecationWarning, type DeriveHandler, type DeriveParams, type ErrorCode, type Fallback, FlagBuilder, type FlagConfig, type FlagFactory, type FlagKind, type FlagParseFn, type FlagPresence, type FlagSchema, type FormatLoader, type HelpLinks, type HelpOptions, type InferArg, type InferArgs, type InferFlag, type InferFlags, type InferNameOption, type InputPromptConfig, type InteractiveParams, type InteractiveResolver, type InteractiveResult, type JsonSchemaOptions, type ManifestDiscoveryOptions, type ManifestPresetSettings, type ManifestSettings, type Middleware, type MiddlewareHandler, type MiddlewareParams, type MultiselectPromptConfig, type NumberConstraintViolation, type NumberConstraints, type Out, type OutputOptions, type PackageJsonAdapter, type PackageJsonData, type PackageJsonSettings, type PackageRepository, ParseError, type ParseErrorCode, type ParseErrorOptions, type ParseResult, type PluginCommandContext, type ProgressHandle, type ProgressOptions, type PromptConfig, type PromptConfigBase, type PromptEngine, type PromptKind, type PromptResult, type ReadFn, type ResolveOptions, type ResolveResult, type ResolvedArgValue, type ResolvedCommandParams, type ResolvedManifestSettings, type ResolvedMultiselectPromptConfig, type ResolvedPromptConfig, type ResolvedSelectPromptConfig, type ResolvedValue, type RunResult, SHELLS, type SelectChoice, type SelectPromptConfig, type Shell, type SpinnerHandle, type SpinnerOptions, type TableColumn, type TableFormat, type TableOptions, type TableStream, type Token, ValidationError, type ValidationErrorCode, type ValidationErrorOptions, type Verbosity, type WithArgPresence, type WithPresence, type WithVariadic, type WriteFn, arg, buildConfigSearchPaths, cli, command, configFormat, createArgSchema, createOutput, createSchema, createTerminalPrompter, definitionMetaSchema, discoverConfig, discoverManifest, discoverPackageJson, flag, formatHelp, generateBashCompletion, generateCompletion, generateFishCompletion, generateInputSchema, generatePowerShellCompletion, generateSchema, generateZshCompletion, group, inferCliName, isCLIError, isParseError, isValidationError, middleware, osc8, packageRepositoryUrl, parse, plugin, resolve, resolvePromptConfig, tokenize, visibleWidth };
|
package/dist/index.mjs
CHANGED
|
@@ -1 +1,22 @@
|
|
|
1
|
-
import{FlagBuilder
|
|
1
|
+
import { FlagBuilder, createSchema, flag } from "./core/schema/flag.mjs";
|
|
2
|
+
import { generateBashCompletion } from "./core/completion/shells/bash.mjs";
|
|
3
|
+
import { generateFishCompletion } from "./core/completion/shells/fish.mjs";
|
|
4
|
+
import { generatePowerShellCompletion } from "./core/completion/shells/powershell.mjs";
|
|
5
|
+
import { CLIError, ParseError, ValidationError, isCLIError, isParseError, isValidationError } from "./core/errors/index.mjs";
|
|
6
|
+
import { generateZshCompletion } from "./core/completion/shells/zsh.mjs";
|
|
7
|
+
import { SHELLS, generateCompletion } from "./core/completion/index.mjs";
|
|
8
|
+
import { osc8, visibleWidth } from "./core/help/ansi.mjs";
|
|
9
|
+
import { formatHelp } from "./core/help/index.mjs";
|
|
10
|
+
import { createOutput } from "./core/output/index.mjs";
|
|
11
|
+
import { ArgBuilder, arg, createArgSchema } from "./core/schema/arg.mjs";
|
|
12
|
+
import { CommandBuilder, command, group } from "./core/schema/command.mjs";
|
|
13
|
+
import { middleware } from "./core/schema/middleware.mjs";
|
|
14
|
+
import { parse, tokenize } from "./core/parse/index.mjs";
|
|
15
|
+
import { createTerminalPrompter, resolvePromptConfig } from "./core/prompt/index.mjs";
|
|
16
|
+
import { resolve } from "./core/resolve/index.mjs";
|
|
17
|
+
import { discoverManifest, discoverPackageJson, inferCliName, packageRepositoryUrl } from "./core/config/package-json.mjs";
|
|
18
|
+
import { plugin } from "./core/cli/plugin.mjs";
|
|
19
|
+
import { buildConfigSearchPaths, configFormat, discoverConfig } from "./core/config/index.mjs";
|
|
20
|
+
import { CLIBuilder, cli } from "./core/cli/index.mjs";
|
|
21
|
+
import { definitionMetaSchema, generateInputSchema, generateSchema } from "./core/json-schema/index.mjs";
|
|
22
|
+
export { ArgBuilder, CLIBuilder, CLIError, CommandBuilder, FlagBuilder, ParseError, SHELLS, ValidationError, arg, buildConfigSearchPaths, cli, command, configFormat, createArgSchema, createOutput, createSchema, createTerminalPrompter, definitionMetaSchema, discoverConfig, discoverManifest, discoverPackageJson, flag, formatHelp, generateBashCompletion, generateCompletion, generateFishCompletion, generateInputSchema, generatePowerShellCompletion, generateSchema, generateZshCompletion, group, inferCliName, isCLIError, isParseError, isValidationError, middleware, osc8, packageRepositoryUrl, parse, plugin, resolve, resolvePromptConfig, tokenize, visibleWidth };
|
package/dist/runtime/adapter.mjs
CHANGED
|
@@ -1 +1,82 @@
|
|
|
1
|
-
|
|
1
|
+
//#region src/runtime/adapter.ts
|
|
2
|
+
/**
|
|
3
|
+
* Error thrown by adapter implementations that model process exit as an
|
|
4
|
+
* exception instead of terminating immediately.
|
|
5
|
+
*
|
|
6
|
+
* Runtime and CLI dispatch layers can catch this to perform real process
|
|
7
|
+
* termination, while tests can assert on exit codes without killing the
|
|
8
|
+
* runner:
|
|
9
|
+
*
|
|
10
|
+
* ```ts
|
|
11
|
+
* try {
|
|
12
|
+
* await cli.run({ adapter: createTestAdapter() });
|
|
13
|
+
* } catch (e) {
|
|
14
|
+
* if (e instanceof ExitError) expect(e.code).toBe(0);
|
|
15
|
+
* }
|
|
16
|
+
* ```
|
|
17
|
+
*/
|
|
18
|
+
var ExitError = class extends Error {
|
|
19
|
+
/** Error name — always `'ExitError'` for `instanceof` checks. */
|
|
20
|
+
name = "ExitError";
|
|
21
|
+
/** The exit code passed to `exit()`. */
|
|
22
|
+
code;
|
|
23
|
+
/** Create an ExitError for the given process exit code. */
|
|
24
|
+
constructor(code) {
|
|
25
|
+
super(`Process exited with code ${code}`);
|
|
26
|
+
this.code = code;
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
/** Noop writer — silently discards output. */
|
|
30
|
+
const noopWrite = () => {};
|
|
31
|
+
/** Noop reader — returns `null` (EOF) immediately. */
|
|
32
|
+
const eofRead = () => Promise.resolve(null);
|
|
33
|
+
/** Noop file reader — returns `null` (not found) for all paths. */
|
|
34
|
+
const noopReadFile = () => Promise.resolve(null);
|
|
35
|
+
/**
|
|
36
|
+
* Create a test runtime adapter with injectable process state.
|
|
37
|
+
*
|
|
38
|
+
* Use this in `@kjanat/dreamcli/testkit` tests when you need to simulate argv,
|
|
39
|
+
* environment variables, TTY state, stdin, or config-file reads without
|
|
40
|
+
* touching the host process.
|
|
41
|
+
*
|
|
42
|
+
* @param options - Optional overrides for any adapter field.
|
|
43
|
+
* @returns A {@linkcode RuntimeAdapter} suitable for test scenarios.
|
|
44
|
+
*
|
|
45
|
+
* @example
|
|
46
|
+
* ```ts
|
|
47
|
+
* const adapter = createTestAdapter({
|
|
48
|
+
* argv: ['node', 'cli.js', 'deploy', '--force'],
|
|
49
|
+
* env: { DEPLOY_REGION: 'us' },
|
|
50
|
+
* });
|
|
51
|
+
*
|
|
52
|
+
* const result = await cli('mycli').run({ adapter });
|
|
53
|
+
* ```
|
|
54
|
+
*/
|
|
55
|
+
function createTestAdapter(options) {
|
|
56
|
+
const stdinIsTTY = options?.stdinIsTTY ?? false;
|
|
57
|
+
let stdinData = !stdinIsTTY ? options?.stdinData : void 0;
|
|
58
|
+
return {
|
|
59
|
+
argv: options?.argv ?? ["node", "test"],
|
|
60
|
+
env: options?.env ?? {},
|
|
61
|
+
cwd: options?.cwd ?? "/test",
|
|
62
|
+
stdout: options?.stdout ?? noopWrite,
|
|
63
|
+
stderr: options?.stderr ?? noopWrite,
|
|
64
|
+
stdin: options?.stdin ?? eofRead,
|
|
65
|
+
readStdin: () => {
|
|
66
|
+
if (stdinData === void 0) return Promise.resolve(null);
|
|
67
|
+
const result = stdinData;
|
|
68
|
+
stdinData = void 0;
|
|
69
|
+
return Promise.resolve(result);
|
|
70
|
+
},
|
|
71
|
+
isTTY: options?.isTTY ?? false,
|
|
72
|
+
stdinIsTTY,
|
|
73
|
+
exit: options?.exit ?? ((code) => {
|
|
74
|
+
throw new ExitError(code);
|
|
75
|
+
}),
|
|
76
|
+
readFile: options?.readFile ?? noopReadFile,
|
|
77
|
+
homedir: options?.homedir ?? "/home/test",
|
|
78
|
+
configDir: options?.configDir ?? "/home/test/.config"
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
//#endregion
|
|
82
|
+
export { ExitError, createTestAdapter };
|
package/dist/runtime/auto.mjs
CHANGED
|
@@ -1 +1,46 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { createNodeAdapter } from "./node.mjs";
|
|
2
|
+
import { createBunAdapter } from "./bun.mjs";
|
|
3
|
+
import { createDenoAdapter } from "./deno.mjs";
|
|
4
|
+
import { detectRuntime } from "./detect.mjs";
|
|
5
|
+
//#region src/runtime/auto.ts
|
|
6
|
+
function isDenoNamespace(value) {
|
|
7
|
+
if (typeof value !== "object" || value === null) return false;
|
|
8
|
+
const candidate = value;
|
|
9
|
+
return Array.isArray(candidate.args) && typeof candidate.cwd === "function" && typeof candidate.exit === "function" && typeof candidate.readTextFile === "function" && typeof candidate.env?.get === "function" && typeof candidate.env.toObject === "function" && typeof candidate.stdout?.writeSync === "function" && typeof candidate.stdout.isTerminal === "function" && typeof candidate.stderr?.writeSync === "function" && typeof candidate.stdin?.isTerminal === "function" && typeof candidate.stdin?.readable?.getReader === "function";
|
|
10
|
+
}
|
|
11
|
+
function getInjectedDenoNamespace(globals) {
|
|
12
|
+
const candidate = globals.Deno;
|
|
13
|
+
return isDenoNamespace(candidate) ? candidate : void 0;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Create a runtime adapter for the current environment.
|
|
17
|
+
*
|
|
18
|
+
* Detection order follows {@link detectRuntime}: Bun → Deno → Node → unknown.
|
|
19
|
+
* Unknown runtimes fall back to the Node adapter because most JS runtimes
|
|
20
|
+
* expose a Node-compatible `process` global.
|
|
21
|
+
*
|
|
22
|
+
* @param globals - Override `globalThis` for testing. Production callers
|
|
23
|
+
* should omit this parameter.
|
|
24
|
+
* @returns A {@linkcode RuntimeAdapter} for the detected runtime.
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* ```ts
|
|
28
|
+
* import { cli } from '@kjanat/dreamcli';
|
|
29
|
+
*
|
|
30
|
+
* // Auto-detects Node/Bun/Deno and creates the right adapter
|
|
31
|
+
* cli('mycli').run(); // uses createAdapter() internally
|
|
32
|
+
* ```
|
|
33
|
+
*/
|
|
34
|
+
function createAdapter(globals) {
|
|
35
|
+
const runtimeGlobals = globals ?? globalThis;
|
|
36
|
+
const runtime = detectRuntime(runtimeGlobals);
|
|
37
|
+
switch (runtime) {
|
|
38
|
+
case "bun": return createBunAdapter();
|
|
39
|
+
case "deno": return createDenoAdapter(getInjectedDenoNamespace(runtimeGlobals));
|
|
40
|
+
case "node":
|
|
41
|
+
case "unknown": return createNodeAdapter();
|
|
42
|
+
default: return runtime;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
//#endregion
|
|
46
|
+
export { createAdapter };
|
package/dist/runtime/bun.mjs
CHANGED
|
@@ -1 +1,28 @@
|
|
|
1
|
-
import{createNodeAdapter
|
|
1
|
+
import { createNodeAdapter } from "./node.mjs";
|
|
2
|
+
//#region src/runtime/bun.ts
|
|
3
|
+
/**
|
|
4
|
+
* Create a runtime adapter backed by Bun's Node-compatible `process` global.
|
|
5
|
+
*
|
|
6
|
+
* Bun exposes `globalThis.process` with the same shape as Node.js, so this
|
|
7
|
+
* delegates entirely to {@link createNodeAdapter}. If Bun-specific optimizations
|
|
8
|
+
* are needed in the future (e.g. `Bun.write` for I/O), they can be layered
|
|
9
|
+
* here without changing the public API.
|
|
10
|
+
*
|
|
11
|
+
* @param proc - Override the process object (useful for testing the adapter itself).
|
|
12
|
+
* @returns A {@linkcode RuntimeAdapter} backed by Bun's process state.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```ts
|
|
16
|
+
* import { cli } from '@kjanat/dreamcli';
|
|
17
|
+
* import { createBunAdapter } from '@kjanat/dreamcli/runtime';
|
|
18
|
+
*
|
|
19
|
+
* cli('mycli')
|
|
20
|
+
* .command(deploy)
|
|
21
|
+
* .run({ adapter: createBunAdapter() });
|
|
22
|
+
* ```
|
|
23
|
+
*/
|
|
24
|
+
function createBunAdapter(proc) {
|
|
25
|
+
return createNodeAdapter(proc);
|
|
26
|
+
}
|
|
27
|
+
//#endregion
|
|
28
|
+
export { createBunAdapter };
|