@kjanat/dreamcli 3.0.0-rc.1 → 3.0.0-rc.2
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 +26 -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.mjs +7 -1
- package/package.json +17 -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,6 +1,348 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
1
|
+
//#region src/core/prompt/index.ts
|
|
2
|
+
/**
|
|
3
|
+
* Sentinel value representing a cancelled/aborted prompt in the test
|
|
4
|
+
* prompter's answer queue.
|
|
5
|
+
*
|
|
6
|
+
* Uses `Symbol.for()` for cross-bundle safety — the same symbol is
|
|
7
|
+
* returned regardless of which copy of the module is loaded.
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```ts
|
|
11
|
+
* const prompter = createTestPrompter([
|
|
12
|
+
* 'us', // first prompt answered 'us'
|
|
13
|
+
* PROMPT_CANCEL, // second prompt cancelled
|
|
14
|
+
* ]);
|
|
15
|
+
* ```
|
|
16
|
+
*/
|
|
17
|
+
const PROMPT_CANCEL = Symbol.for("dreamcli.prompt.cancel");
|
|
18
|
+
/**
|
|
19
|
+
* Create a prompt engine that returns pre-configured answers.
|
|
20
|
+
*
|
|
21
|
+
* Each call to `promptOne` consumes the next answer from the queue.
|
|
22
|
+
* Pass `PROMPT_CANCEL` as an answer to simulate the user cancelling
|
|
23
|
+
* that prompt.
|
|
24
|
+
*
|
|
25
|
+
* For `input` prompts that declare a `validate` function, a string answer is
|
|
26
|
+
* validated just as the terminal prompter does. An answer that fails validation
|
|
27
|
+
* is rejected as a cancellation (`{ answered: false }`) — mirroring the terminal
|
|
28
|
+
* prompter exhausting its retries — so prompt validation is integration-testable
|
|
29
|
+
* via {@linkcode runCommand} rather than silently accepted as the resolved value.
|
|
30
|
+
*
|
|
31
|
+
* @param answers - Ordered queue of answers. Use `PROMPT_CANCEL` for
|
|
32
|
+
* cancellation.
|
|
33
|
+
* @param options - Controls behavior when the queue is exhausted.
|
|
34
|
+
* @returns A {@linkcode PromptEngine} suitable for testing.
|
|
35
|
+
*
|
|
36
|
+
* @example
|
|
37
|
+
* ```ts
|
|
38
|
+
* const prompter = createTestPrompter(['eu', true, PROMPT_CANCEL]);
|
|
39
|
+
*
|
|
40
|
+
* // First promptOne → { answered: true, value: 'eu' }
|
|
41
|
+
* // Second promptOne → { answered: true, value: true }
|
|
42
|
+
* // Third promptOne → { answered: false }
|
|
43
|
+
* ```
|
|
44
|
+
*/
|
|
45
|
+
function createTestPrompter(answers, options) {
|
|
46
|
+
let index = 0;
|
|
47
|
+
return { promptOne(config) {
|
|
48
|
+
if (index >= answers.length) {
|
|
49
|
+
if (options?.onExhausted === "cancel") return Promise.resolve({ answered: false });
|
|
50
|
+
return Promise.reject(/* @__PURE__ */ new Error(`Test prompter exhausted: expected at most ${answers.length} prompts, got prompt #${index + 1}`));
|
|
51
|
+
}
|
|
52
|
+
const answer = answers[index];
|
|
53
|
+
index += 1;
|
|
54
|
+
if (answer === PROMPT_CANCEL) return Promise.resolve({ answered: false });
|
|
55
|
+
if (config.kind === "input" && config.validate !== void 0 && typeof answer === "string") {
|
|
56
|
+
if (config.validate(answer) !== true) return Promise.resolve({ answered: false });
|
|
57
|
+
}
|
|
58
|
+
return Promise.resolve({
|
|
59
|
+
answered: true,
|
|
60
|
+
value: answer
|
|
61
|
+
});
|
|
62
|
+
} };
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Create a prompt engine backed by line-based terminal I/O.
|
|
66
|
+
*
|
|
67
|
+
* Uses a {@linkcode ReadFn} for input and a {@linkcode WriteFn} for output. This is the
|
|
68
|
+
* built-in renderer — sufficient for most CLI use cases. For richer
|
|
69
|
+
* TUI experiences, users can implement {@linkcode PromptEngine} with a library
|
|
70
|
+
* like `@clack/prompts` or `inquirer`.
|
|
71
|
+
*
|
|
72
|
+
* The prompter does **not** use raw mode — all input is line-based.
|
|
73
|
+
* This keeps the implementation portable across Node, Bun, and Deno
|
|
74
|
+
* without platform-specific stdin configuration.
|
|
75
|
+
*
|
|
76
|
+
* @param read - Line reader function (returns `null` on EOF)
|
|
77
|
+
* @param write - Output writer function
|
|
78
|
+
* @returns A {@linkcode PromptEngine} that prompts via terminal I/O
|
|
79
|
+
*
|
|
80
|
+
* @example
|
|
81
|
+
* ```ts
|
|
82
|
+
* import { createInterface } from 'readline';
|
|
83
|
+
*
|
|
84
|
+
* const rl = createInterface({ input: process.stdin, output: process.stdout });
|
|
85
|
+
* const read = () => new Promise<string | null>((resolve) => {
|
|
86
|
+
* rl.question('', (answer) => resolve(answer));
|
|
87
|
+
* });
|
|
88
|
+
* const write: WriteFn = (s) => process.stdout.write(s);
|
|
89
|
+
*
|
|
90
|
+
* const prompter = createTerminalPrompter(read, write);
|
|
91
|
+
* ```
|
|
92
|
+
*/
|
|
93
|
+
function createTerminalPrompter(read, write) {
|
|
94
|
+
return { promptOne(config) {
|
|
95
|
+
switch (config.kind) {
|
|
96
|
+
case "confirm": return promptConfirm(config, read, write);
|
|
97
|
+
case "input": return promptInput(config, read, write);
|
|
98
|
+
case "select": return promptSelect(config, read, write);
|
|
99
|
+
case "multiselect": return promptMultiselect(config, read, write);
|
|
100
|
+
}
|
|
101
|
+
} };
|
|
102
|
+
}
|
|
103
|
+
/** Maximum retries for invalid input before treating as cancel. */
|
|
104
|
+
const MAX_RETRIES = 10;
|
|
105
|
+
/**
|
|
106
|
+
* Confirm prompt: yes/no question.
|
|
107
|
+
*
|
|
108
|
+
* The hint reflects {@link ConfirmPromptConfig.default}: `(Y/n)` when the
|
|
109
|
+
* default is `true` (or unset), `(y/N)` when `false`. An empty submission
|
|
110
|
+
* resolves to that default. Accepts: y, yes, n, no, empty. Case-insensitive.
|
|
111
|
+
*
|
|
112
|
+
* @param config - {@link ConfirmPromptConfig} with the question message and optional default
|
|
113
|
+
* @param read - {@link ReadFn} for reading a line of user input
|
|
114
|
+
* @param write - {@link WriteFn} for rendering prompt text
|
|
115
|
+
* @returns Resolved {@link PromptResult} — `true`/`false` value, or cancelled on EOF
|
|
116
|
+
*/
|
|
117
|
+
async function promptConfirm(config, read, write) {
|
|
118
|
+
const defaultValue = config.default ?? true;
|
|
119
|
+
let retries = 0;
|
|
120
|
+
while (retries < MAX_RETRIES) {
|
|
121
|
+
const hint = defaultValue ? "(Y/n)" : "(y/N)";
|
|
122
|
+
write(`${config.message} ${hint} `);
|
|
123
|
+
const line = await read();
|
|
124
|
+
if (line === null) return { answered: false };
|
|
125
|
+
const lower = line.trim().toLowerCase();
|
|
126
|
+
if (lower === "") return {
|
|
127
|
+
answered: true,
|
|
128
|
+
value: defaultValue
|
|
129
|
+
};
|
|
130
|
+
if (lower === "y" || lower === "yes") return {
|
|
131
|
+
answered: true,
|
|
132
|
+
value: true
|
|
133
|
+
};
|
|
134
|
+
if (lower === "n" || lower === "no") return {
|
|
135
|
+
answered: true,
|
|
136
|
+
value: false
|
|
137
|
+
};
|
|
138
|
+
write("Please answer y or n.\n");
|
|
139
|
+
retries += 1;
|
|
140
|
+
}
|
|
141
|
+
write("Too many invalid attempts.\n");
|
|
142
|
+
return { answered: false };
|
|
143
|
+
}
|
|
144
|
+
/**
|
|
145
|
+
* Input prompt: free-text string entry.
|
|
146
|
+
*
|
|
147
|
+
* Displays the message and a hint listing the placeholder and/or
|
|
148
|
+
* {@link InputPromptConfig.default | default}, reads a line, and optionally
|
|
149
|
+
* validates via {@link InputPromptConfig.validate | validate}. When a default
|
|
150
|
+
* is configured, an empty submission resolves to it without running validation;
|
|
151
|
+
* otherwise an empty submission is returned verbatim (and treated as "no answer"
|
|
152
|
+
* by the resolver). Loops on invalid input up to `MAX_RETRIES`.
|
|
153
|
+
*
|
|
154
|
+
* @param config - {@link InputPromptConfig} with message, optional placeholder, default, and validator
|
|
155
|
+
* @param read - {@link ReadFn} for reading a line of user input
|
|
156
|
+
* @param write - {@link WriteFn} for rendering prompt text and validation errors
|
|
157
|
+
* @returns Resolved {@link PromptResult} — trimmed string value (or default), or cancelled on EOF/exhausted retries
|
|
158
|
+
*/
|
|
159
|
+
async function promptInput(config, read, write) {
|
|
160
|
+
let retries = 0;
|
|
161
|
+
const hints = [];
|
|
162
|
+
if (config.placeholder !== void 0) hints.push(config.placeholder);
|
|
163
|
+
if (config.default !== void 0) hints.push(`default: ${config.default}`);
|
|
164
|
+
const hint = hints.length > 0 ? ` (${hints.join(", ")})` : "";
|
|
165
|
+
while (retries < MAX_RETRIES) {
|
|
166
|
+
write(`${config.message}${hint}: `);
|
|
167
|
+
const line = await read();
|
|
168
|
+
if (line === null) return { answered: false };
|
|
169
|
+
const trimmed = line.trim();
|
|
170
|
+
if (trimmed === "") return config.default !== void 0 ? {
|
|
171
|
+
answered: true,
|
|
172
|
+
value: config.default
|
|
173
|
+
} : {
|
|
174
|
+
answered: true,
|
|
175
|
+
value: ""
|
|
176
|
+
};
|
|
177
|
+
if (config.validate !== void 0) {
|
|
178
|
+
const result = config.validate(trimmed);
|
|
179
|
+
if (result !== true) {
|
|
180
|
+
write(`${result}\n`);
|
|
181
|
+
retries += 1;
|
|
182
|
+
continue;
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
return {
|
|
186
|
+
answered: true,
|
|
187
|
+
value: trimmed
|
|
188
|
+
};
|
|
189
|
+
}
|
|
190
|
+
write("Too many invalid attempts.\n");
|
|
191
|
+
return { answered: false };
|
|
192
|
+
}
|
|
193
|
+
/**
|
|
194
|
+
* Select prompt: single choice from a numbered list.
|
|
195
|
+
*
|
|
196
|
+
* Displays choices with 1-based indices. User enters the number of
|
|
197
|
+
* their selection.
|
|
198
|
+
*
|
|
199
|
+
* @param config - {@link ResolvedSelectPromptConfig} with message and guaranteed non-empty choices
|
|
200
|
+
* @param read - {@link ReadFn} for reading a line of user input
|
|
201
|
+
* @param write - {@link WriteFn} for rendering the choice list and validation errors
|
|
202
|
+
* @returns Resolved {@link PromptResult} — selected {@link SelectChoice.value}, or cancelled on EOF/exhausted retries
|
|
203
|
+
*/
|
|
204
|
+
async function promptSelect(config, read, write) {
|
|
205
|
+
let retries = 0;
|
|
206
|
+
while (retries < MAX_RETRIES) {
|
|
207
|
+
write(`${config.message}\n`);
|
|
208
|
+
for (let i = 0; i < config.choices.length; i += 1) {
|
|
209
|
+
const choice = config.choices[i];
|
|
210
|
+
if (choice === void 0) continue;
|
|
211
|
+
const label = choice.label ?? choice.value;
|
|
212
|
+
const desc = choice.description !== void 0 ? ` - ${choice.description}` : "";
|
|
213
|
+
write(` ${i + 1}) ${label}${desc}\n`);
|
|
214
|
+
}
|
|
215
|
+
write("Enter number: ");
|
|
216
|
+
const line = await read();
|
|
217
|
+
if (line === null) return { answered: false };
|
|
218
|
+
const num = Number(line.trim());
|
|
219
|
+
if (Number.isNaN(num) || !Number.isInteger(num) || num < 1 || num > config.choices.length) {
|
|
220
|
+
write(`Please enter a number between 1 and ${config.choices.length}.\n`);
|
|
221
|
+
retries += 1;
|
|
222
|
+
continue;
|
|
223
|
+
}
|
|
224
|
+
const selected = config.choices[num - 1];
|
|
225
|
+
if (selected === void 0) {
|
|
226
|
+
retries += 1;
|
|
227
|
+
continue;
|
|
228
|
+
}
|
|
229
|
+
return {
|
|
230
|
+
answered: true,
|
|
231
|
+
value: selected.value
|
|
232
|
+
};
|
|
233
|
+
}
|
|
234
|
+
write("Too many invalid attempts.\n");
|
|
235
|
+
return { answered: false };
|
|
236
|
+
}
|
|
237
|
+
/**
|
|
238
|
+
* Multiselect prompt: multiple choices from a numbered list.
|
|
239
|
+
*
|
|
240
|
+
* Displays choices with 1-based indices. User enters comma-separated
|
|
241
|
+
* numbers of their selections (e.g. `1,3,5`).
|
|
242
|
+
*
|
|
243
|
+
* Validates against `min`/`max` constraints if configured.
|
|
244
|
+
*
|
|
245
|
+
* @param config - {@link ResolvedMultiselectPromptConfig} with message, guaranteed non-empty choices, and optional min/max
|
|
246
|
+
* @param read - {@link ReadFn} for reading a line of user input
|
|
247
|
+
* @param write - {@link WriteFn} for rendering the choice list, hints, and validation errors
|
|
248
|
+
* @returns Resolved {@link PromptResult} — array of selected {@link SelectChoice.value} strings, or cancelled on EOF/exhausted retries
|
|
249
|
+
*/
|
|
250
|
+
async function promptMultiselect(config, read, write) {
|
|
251
|
+
let retries = 0;
|
|
252
|
+
while (retries < MAX_RETRIES) {
|
|
253
|
+
write(`${config.message}\n`);
|
|
254
|
+
for (let i = 0; i < config.choices.length; i += 1) {
|
|
255
|
+
const choice = config.choices[i];
|
|
256
|
+
if (choice === void 0) continue;
|
|
257
|
+
const label = choice.label ?? choice.value;
|
|
258
|
+
const desc = choice.description !== void 0 ? ` - ${choice.description}` : "";
|
|
259
|
+
write(` ${i + 1}) ${label}${desc}\n`);
|
|
260
|
+
}
|
|
261
|
+
write(`Enter numbers separated by commas (e.g. 1,3${config.min !== void 0 ? `, min: ${config.min}` : ""}${config.max !== void 0 ? `, max: ${config.max}` : ""}): `);
|
|
262
|
+
const line = await read();
|
|
263
|
+
if (line === null) return { answered: false };
|
|
264
|
+
const trimmed = line.trim();
|
|
265
|
+
if (trimmed === "") {
|
|
266
|
+
if (config.min !== void 0 && config.min > 0) {
|
|
267
|
+
write(`Please select at least ${config.min}.\n`);
|
|
268
|
+
retries += 1;
|
|
269
|
+
continue;
|
|
270
|
+
}
|
|
271
|
+
return {
|
|
272
|
+
answered: true,
|
|
273
|
+
value: []
|
|
274
|
+
};
|
|
275
|
+
}
|
|
276
|
+
const parts = trimmed.split(",").map((s) => s.trim());
|
|
277
|
+
const indices = [];
|
|
278
|
+
let valid = true;
|
|
279
|
+
for (const part of parts) {
|
|
280
|
+
const num = Number(part);
|
|
281
|
+
if (Number.isNaN(num) || !Number.isInteger(num) || num < 1 || num > config.choices.length) {
|
|
282
|
+
write(`Invalid selection '${part}'. Use numbers between 1 and ${config.choices.length}.\n`);
|
|
283
|
+
valid = false;
|
|
284
|
+
break;
|
|
285
|
+
}
|
|
286
|
+
indices.push(num);
|
|
287
|
+
}
|
|
288
|
+
if (!valid) {
|
|
289
|
+
retries += 1;
|
|
290
|
+
continue;
|
|
291
|
+
}
|
|
292
|
+
const unique = [...new Set(indices)];
|
|
293
|
+
if (config.min !== void 0 && unique.length < config.min) {
|
|
294
|
+
write(`Please select at least ${config.min}.\n`);
|
|
295
|
+
retries += 1;
|
|
296
|
+
continue;
|
|
297
|
+
}
|
|
298
|
+
if (config.max !== void 0 && unique.length > config.max) {
|
|
299
|
+
write(`Please select at most ${config.max}.\n`);
|
|
300
|
+
retries += 1;
|
|
301
|
+
continue;
|
|
302
|
+
}
|
|
303
|
+
return {
|
|
304
|
+
answered: true,
|
|
305
|
+
value: unique.map((n) => {
|
|
306
|
+
return config.choices[n - 1]?.value ?? "";
|
|
307
|
+
})
|
|
308
|
+
};
|
|
309
|
+
}
|
|
310
|
+
write("Too many invalid attempts.\n");
|
|
311
|
+
return { answered: false };
|
|
312
|
+
}
|
|
313
|
+
/**
|
|
314
|
+
* Prepare a {@linkcode ResolvedPromptConfig} from a raw {@linkcode PromptConfig} and optional
|
|
315
|
+
* enum values from the flag schema.
|
|
316
|
+
*
|
|
317
|
+
* For select/multiselect prompts without explicit choices, this merges
|
|
318
|
+
* the flag's enum values into the choices list. Throws if no choices
|
|
319
|
+
* are available.
|
|
320
|
+
*
|
|
321
|
+
* This function is called by the resolution chain (prompt-resolve-1),
|
|
322
|
+
* not by the engine itself — keeping the engine free of schema knowledge.
|
|
323
|
+
*
|
|
324
|
+
* @param config - Raw prompt config from `FlagSchema.prompt`
|
|
325
|
+
* @param enumValues - Optional enum values from `FlagSchema.enumValues`
|
|
326
|
+
* @returns Resolved config with choices guaranteed for select kinds
|
|
327
|
+
* @throws Error if select/multiselect has no choices and no enum values
|
|
328
|
+
*/
|
|
329
|
+
function resolvePromptConfig(config, enumValues) {
|
|
330
|
+
if (config.kind === "confirm" || config.kind === "input") return config;
|
|
331
|
+
const rawChoices = config.choices ?? enumValues?.map((v) => ({ value: v })) ?? [];
|
|
332
|
+
if (rawChoices.length === 0) throw new Error(`Prompt config (${config.kind}) requires choices but none were provided and no enum values are available`);
|
|
333
|
+
const choices = rawChoices;
|
|
334
|
+
if (config.kind === "select") return {
|
|
335
|
+
kind: "select",
|
|
336
|
+
message: config.message,
|
|
337
|
+
choices
|
|
338
|
+
};
|
|
339
|
+
return {
|
|
340
|
+
kind: "multiselect",
|
|
341
|
+
message: config.message,
|
|
342
|
+
choices,
|
|
343
|
+
...config.min !== void 0 ? { min: config.min } : {},
|
|
344
|
+
...config.max !== void 0 ? { max: config.max } : {}
|
|
345
|
+
};
|
|
346
|
+
}
|
|
347
|
+
//#endregion
|
|
348
|
+
export { PROMPT_CANCEL, createTerminalPrompter, createTestPrompter, resolvePromptConfig };
|
|
@@ -1 +1,105 @@
|
|
|
1
|
-
import{ValidationError
|
|
1
|
+
import { ValidationError } from "../errors/index.mjs";
|
|
2
|
+
import { coerceArgStringValue } from "./coerce.mjs";
|
|
3
|
+
import { isNonEmpty, throwAggregatedErrors } from "./errors.mjs";
|
|
4
|
+
//#region src/core/resolve/args.ts
|
|
5
|
+
/**
|
|
6
|
+
* Internal arg resolution helpers.
|
|
7
|
+
*
|
|
8
|
+
* @module dreamcli/core/resolve/args
|
|
9
|
+
* @internal
|
|
10
|
+
*/
|
|
11
|
+
/** Walk every declared arg through the resolution chain (cli -> stdin -> env -> default), collecting deprecations and throwing aggregated errors. */
|
|
12
|
+
function resolveArgs(argEntries, parsedArgs, stdinData, env, deprecations) {
|
|
13
|
+
const resolved = {};
|
|
14
|
+
const errors = [];
|
|
15
|
+
for (const entry of argEntries) {
|
|
16
|
+
const { name, schema } = entry;
|
|
17
|
+
const hasParsedValue = Object.hasOwn(parsedArgs, name);
|
|
18
|
+
const parsedValue = parsedArgs[name];
|
|
19
|
+
if (hasParsedValue && parsedValue !== void 0 && (!schema.stdinMode || parsedValue !== "-") && !(schema.variadic && Array.isArray(parsedValue) && parsedValue.length === 0)) {
|
|
20
|
+
if (schema.deprecated !== void 0) deprecations.push({
|
|
21
|
+
kind: "arg",
|
|
22
|
+
name,
|
|
23
|
+
message: schema.deprecated
|
|
24
|
+
});
|
|
25
|
+
resolved[name] = parsedValue;
|
|
26
|
+
continue;
|
|
27
|
+
}
|
|
28
|
+
if (schema.stdinMode && (!hasParsedValue || parsedValue === void 0 || parsedValue === "-")) {
|
|
29
|
+
if (stdinData !== void 0 && stdinData !== null) {
|
|
30
|
+
const coerced = coerceArgStringValue(name, { kind: "stdin" }, stdinData, schema);
|
|
31
|
+
if (coerced.ok) {
|
|
32
|
+
if (schema.deprecated !== void 0) deprecations.push({
|
|
33
|
+
kind: "arg",
|
|
34
|
+
name,
|
|
35
|
+
message: schema.deprecated
|
|
36
|
+
});
|
|
37
|
+
resolved[name] = coerced.value;
|
|
38
|
+
continue;
|
|
39
|
+
}
|
|
40
|
+
errors.push(coerced.error);
|
|
41
|
+
continue;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
if (schema.envVar !== void 0) {
|
|
45
|
+
const envValue = env[schema.envVar];
|
|
46
|
+
if (envValue !== void 0) {
|
|
47
|
+
const coerced = coerceArgStringValue(name, {
|
|
48
|
+
kind: "env",
|
|
49
|
+
envVar: schema.envVar
|
|
50
|
+
}, envValue, schema);
|
|
51
|
+
if (coerced.ok) {
|
|
52
|
+
if (schema.deprecated !== void 0) deprecations.push({
|
|
53
|
+
kind: "arg",
|
|
54
|
+
name,
|
|
55
|
+
message: schema.deprecated
|
|
56
|
+
});
|
|
57
|
+
resolved[name] = coerced.value;
|
|
58
|
+
continue;
|
|
59
|
+
}
|
|
60
|
+
errors.push(coerced.error);
|
|
61
|
+
continue;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
if (schema.defaultValue !== void 0) {
|
|
65
|
+
resolved[name] = schema.defaultValue;
|
|
66
|
+
continue;
|
|
67
|
+
}
|
|
68
|
+
if (schema.variadic) {
|
|
69
|
+
if (schema.presence === "required") {
|
|
70
|
+
errors.push(new ValidationError(`Missing required argument <${name}>`, {
|
|
71
|
+
code: "REQUIRED_ARG",
|
|
72
|
+
details: {
|
|
73
|
+
arg: name,
|
|
74
|
+
variadic: true
|
|
75
|
+
},
|
|
76
|
+
suggest: buildRequiredArgSuggest(name, schema, true)
|
|
77
|
+
}));
|
|
78
|
+
continue;
|
|
79
|
+
}
|
|
80
|
+
resolved[name] = [];
|
|
81
|
+
continue;
|
|
82
|
+
}
|
|
83
|
+
if (schema.presence === "required") {
|
|
84
|
+
errors.push(new ValidationError(`Missing required argument <${name}>`, {
|
|
85
|
+
code: "REQUIRED_ARG",
|
|
86
|
+
details: { arg: name },
|
|
87
|
+
suggest: buildRequiredArgSuggest(name, schema)
|
|
88
|
+
}));
|
|
89
|
+
continue;
|
|
90
|
+
}
|
|
91
|
+
resolved[name] = void 0;
|
|
92
|
+
}
|
|
93
|
+
if (isNonEmpty(errors)) throwAggregatedErrors(errors);
|
|
94
|
+
return resolved;
|
|
95
|
+
}
|
|
96
|
+
function buildRequiredArgSuggest(name, schema, variadic) {
|
|
97
|
+
const sources = [variadic ? `Provide at least one value for <${name}>` : `Provide a value for <${name}>`];
|
|
98
|
+
if (schema.stdinMode) sources.push(`pipe a value to stdin or pass '-'`);
|
|
99
|
+
if (schema.envVar !== void 0) sources.push(`set ${schema.envVar}`);
|
|
100
|
+
if (sources.length === 1) return sources[0];
|
|
101
|
+
if (sources.length === 2) return `${sources[0]} or ${sources[1]}`;
|
|
102
|
+
return `${sources.slice(0, -1).join(", ")}, or ${sources[sources.length - 1]}`;
|
|
103
|
+
}
|
|
104
|
+
//#endregion
|
|
105
|
+
export { resolveArgs };
|