@kjanat/dreamcli 3.0.0-rc.1 → 3.0.0-rc.11
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 +227 -1
- 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 +81 -8
- package/dist/core/cli/index.mjs +945 -1
- package/dist/core/cli/planner.d.mts +5 -1
- 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 +2 -1
- package/dist/core/cli/root-help.mjs +226 -4
- package/dist/core/cli/root-surface.d.mts +12 -1
- package/dist/core/cli/root-surface.mjs +28 -1
- package/dist/core/cli/runtime-preflight.d.mts +14 -2
- package/dist/core/cli/runtime-preflight.mjs +169 -1
- package/dist/core/completion/index.d.mts +0 -1
- 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 +13 -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 +22 -3
- 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 +55 -38
- 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 +40 -7
- package/dist/core/parse/index.mjs +727 -1
- package/dist/core/prompt/index.d.mts +1 -1
- 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 +4 -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 +3 -4
- package/dist/core/schema/arg.mjs +463 -1
- package/dist/core/schema/command.d.mts +26 -6
- package/dist/core/schema/command.mjs +726 -1
- package/dist/core/schema/flag.d.mts +485 -19
- package/dist/core/schema/flag.mjs +705 -1
- package/dist/core/schema/index.d.mts +4 -2
- package/dist/core/schema/middleware.d.mts +4 -3
- package/dist/core/schema/middleware.mjs +41 -1
- package/dist/core/schema/number-constraints.mjs +60 -1
- package/dist/core/schema/prompt.d.mts +6 -3
- package/dist/core/schema/run.d.mts +20 -1
- 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 +0 -31
- package/dist/core/testkit/index.mjs +83 -1
- package/dist/index.d.mts +11 -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/package.json +45 -42
- package/dist/runtime/bun.d.mts +0 -28
- package/dist/runtime/bun.mjs +0 -1
- 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
package/src/core/prompt/index.ts
DELETED
|
@@ -1,623 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Prompt engine interface and implementations.
|
|
3
|
-
*
|
|
4
|
-
* The prompt engine is the pluggable rendering seam for interactive
|
|
5
|
-
* flag resolution. The framework ships two implementations:
|
|
6
|
-
*
|
|
7
|
-
* - `createTerminalPrompter(read, write)` — line-based terminal I/O
|
|
8
|
-
* - `createTestPrompter(answers)` — pre-configured answers for testing
|
|
9
|
-
*
|
|
10
|
-
* Custom engines implement the {@linkcode PromptEngine} interface to swap in
|
|
11
|
-
* alternative UI (e.g. web-based, TUI library, etc.).
|
|
12
|
-
*
|
|
13
|
-
* The engine receives a {@linkcode ResolvedPromptConfig} — a variant of
|
|
14
|
-
* {@linkcode PromptConfig} where select/multiselect choices are guaranteed
|
|
15
|
-
* non-empty. The resolution chain handles merging enum values from
|
|
16
|
-
* `FlagSchema` into choices before calling the engine.
|
|
17
|
-
*
|
|
18
|
-
* @module dreamcli/core/prompt
|
|
19
|
-
*/
|
|
20
|
-
|
|
21
|
-
import type { WriteFn } from '#internals/core/output/index.ts';
|
|
22
|
-
import type {
|
|
23
|
-
ConfirmPromptConfig,
|
|
24
|
-
InputPromptConfig,
|
|
25
|
-
PromptConfig,
|
|
26
|
-
PromptResult,
|
|
27
|
-
SelectChoice,
|
|
28
|
-
} from '#internals/core/schema/prompt.ts';
|
|
29
|
-
|
|
30
|
-
// --- Resolved prompt config — choices guaranteed present for select kinds
|
|
31
|
-
|
|
32
|
-
/**
|
|
33
|
-
* A select prompt config with choices guaranteed non-empty.
|
|
34
|
-
*
|
|
35
|
-
* The resolution chain populates choices from `FlagSchema.enumValues`
|
|
36
|
-
* when the user's {@linkcode PromptConfig} omits them.
|
|
37
|
-
*/
|
|
38
|
-
interface ResolvedSelectPromptConfig {
|
|
39
|
-
/** Discriminant — single-choice selection prompt. */
|
|
40
|
-
readonly kind: 'select';
|
|
41
|
-
/** User-facing question text. */
|
|
42
|
-
readonly message: string;
|
|
43
|
-
/** Non-empty list of selectable options (populated from flag enum values when omitted). */
|
|
44
|
-
readonly choices: readonly [SelectChoice, ...SelectChoice[]];
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
/**
|
|
48
|
-
* A multiselect prompt config with choices guaranteed non-empty.
|
|
49
|
-
*
|
|
50
|
-
* Same guarantee as {@linkcode ResolvedSelectPromptConfig} — choices are always
|
|
51
|
-
* present and non-empty.
|
|
52
|
-
*/
|
|
53
|
-
interface ResolvedMultiselectPromptConfig {
|
|
54
|
-
/** Discriminant — multiple-choice selection prompt. */
|
|
55
|
-
readonly kind: 'multiselect';
|
|
56
|
-
/** User-facing question text. */
|
|
57
|
-
readonly message: string;
|
|
58
|
-
/** Non-empty list of selectable options (populated from flag enum values when omitted). */
|
|
59
|
-
readonly choices: readonly [SelectChoice, ...SelectChoice[]];
|
|
60
|
-
/** Minimum number of selections required (validated after input). */
|
|
61
|
-
readonly min?: number;
|
|
62
|
-
/** Maximum number of selections allowed (validated after input). */
|
|
63
|
-
readonly max?: number;
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
/**
|
|
67
|
-
* Prompt config variant where select/multiselect choices are guaranteed
|
|
68
|
-
* present. The prompt engine receives this (not raw {@linkcode PromptConfig}),
|
|
69
|
-
* so it never needs to merge enum values from `FlagSchema`.
|
|
70
|
-
*
|
|
71
|
-
* confirm and input configs pass through unchanged.
|
|
72
|
-
*/
|
|
73
|
-
type ResolvedPromptConfig =
|
|
74
|
-
| ConfirmPromptConfig
|
|
75
|
-
| InputPromptConfig
|
|
76
|
-
| ResolvedSelectPromptConfig
|
|
77
|
-
| ResolvedMultiselectPromptConfig;
|
|
78
|
-
|
|
79
|
-
// --- PromptEngine interface — the pluggable seam
|
|
80
|
-
|
|
81
|
-
/**
|
|
82
|
-
* Prompt engine interface.
|
|
83
|
-
*
|
|
84
|
-
* Implementations render a single prompt to the user and return the
|
|
85
|
-
* result. The engine is stateless per call — each `promptOne` is
|
|
86
|
-
* independent.
|
|
87
|
-
*
|
|
88
|
-
* The resolution chain calls `promptOne` for each flag that needs
|
|
89
|
-
* interactive input. Engines do not need schema knowledge — all
|
|
90
|
-
* relevant context (message, choices, validation) is in the config.
|
|
91
|
-
*
|
|
92
|
-
* @example
|
|
93
|
-
* ```ts
|
|
94
|
-
* // Custom engine (e.g. wrapping @clack/prompts)
|
|
95
|
-
* const engine: PromptEngine = {
|
|
96
|
-
* async promptOne(config) {
|
|
97
|
-
* // ... render with your library
|
|
98
|
-
* return { answered: true, value: userInput };
|
|
99
|
-
* }
|
|
100
|
-
* };
|
|
101
|
-
* ```
|
|
102
|
-
*/
|
|
103
|
-
interface PromptEngine {
|
|
104
|
-
/**
|
|
105
|
-
* Present a single prompt and return the user's response.
|
|
106
|
-
*
|
|
107
|
-
* @param config - The resolved prompt configuration (choices guaranteed
|
|
108
|
-
* for select/multiselect).
|
|
109
|
-
* @returns The user's answer, or `{ answered: false }` if cancelled.
|
|
110
|
-
*/
|
|
111
|
-
promptOne(config: ResolvedPromptConfig): Promise<PromptResult>;
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
// --- ReadFn — the minimal stdin abstraction
|
|
115
|
-
|
|
116
|
-
/**
|
|
117
|
-
* A function that reads a single line of user input.
|
|
118
|
-
*
|
|
119
|
-
* Returns `null` on EOF (Ctrl+D on Unix, Ctrl+Z on Windows),
|
|
120
|
-
* indicating the user closed the input stream (treated as cancel).
|
|
121
|
-
*
|
|
122
|
-
* The terminal prompter uses this as its sole input seam. The
|
|
123
|
-
* resolution chain (prompt-adapter-1) will wire this to the
|
|
124
|
-
* runtime adapter's stdin.
|
|
125
|
-
*/
|
|
126
|
-
type ReadFn = () => Promise<string | null>;
|
|
127
|
-
|
|
128
|
-
// --- Sentinel for cancelled prompts in test prompter
|
|
129
|
-
|
|
130
|
-
/**
|
|
131
|
-
* Sentinel value representing a cancelled/aborted prompt in the test
|
|
132
|
-
* prompter's answer queue.
|
|
133
|
-
*
|
|
134
|
-
* Uses `Symbol.for()` for cross-bundle safety — the same symbol is
|
|
135
|
-
* returned regardless of which copy of the module is loaded.
|
|
136
|
-
*
|
|
137
|
-
* @example
|
|
138
|
-
* ```ts
|
|
139
|
-
* const prompter = createTestPrompter([
|
|
140
|
-
* 'us', // first prompt answered 'us'
|
|
141
|
-
* PROMPT_CANCEL, // second prompt cancelled
|
|
142
|
-
* ]);
|
|
143
|
-
* ```
|
|
144
|
-
*/
|
|
145
|
-
const PROMPT_CANCEL: unique symbol = Symbol.for('dreamcli.prompt.cancel') as typeof PROMPT_CANCEL;
|
|
146
|
-
|
|
147
|
-
/**
|
|
148
|
-
* A queued answer consumed by {@link createTestPrompter}.
|
|
149
|
-
*
|
|
150
|
-
* The test prompter returns these values exactly as provided; it does not
|
|
151
|
-
* coerce them. The one exception mirrors the terminal prompter: a string answer
|
|
152
|
-
* to an `input` prompt is run through the prompt's `validate` function, and a
|
|
153
|
-
* failing answer is rejected as a cancellation (see {@link createTestPrompter}).
|
|
154
|
-
* The normal resolution pipeline performs any later type coercion, so tests can
|
|
155
|
-
* supply values in the same shapes real prompts would yield:
|
|
156
|
-
*
|
|
157
|
-
* - `string` for `input` and `select`
|
|
158
|
-
* - `boolean` for `confirm`
|
|
159
|
-
* - `string[]` for `multiselect`
|
|
160
|
-
* - `PROMPT_CANCEL` to simulate user cancellation
|
|
161
|
-
*
|
|
162
|
-
* Because the type is intentionally `unknown`, tests may also inject malformed
|
|
163
|
-
* answers to exercise downstream validation and error reporting.
|
|
164
|
-
*/
|
|
165
|
-
type TestAnswer = unknown;
|
|
166
|
-
|
|
167
|
-
// --- Test prompter
|
|
168
|
-
|
|
169
|
-
/**
|
|
170
|
-
* Options for `createTestPrompter`.
|
|
171
|
-
*/
|
|
172
|
-
interface TestPrompterOptions {
|
|
173
|
-
/**
|
|
174
|
-
* Behavior when all answers have been consumed.
|
|
175
|
-
*
|
|
176
|
-
* - `'throw'` (default) — throws an error, making the test fail
|
|
177
|
-
* loudly if more prompts fire than expected.
|
|
178
|
-
* - `'cancel'` — returns `{ answered: false }`, simulating the
|
|
179
|
-
* user cancelling all subsequent prompts.
|
|
180
|
-
*/
|
|
181
|
-
readonly onExhausted?: 'throw' | 'cancel';
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
/**
|
|
185
|
-
* Create a prompt engine that returns pre-configured answers.
|
|
186
|
-
*
|
|
187
|
-
* Each call to `promptOne` consumes the next answer from the queue.
|
|
188
|
-
* Pass `PROMPT_CANCEL` as an answer to simulate the user cancelling
|
|
189
|
-
* that prompt.
|
|
190
|
-
*
|
|
191
|
-
* For `input` prompts that declare a `validate` function, a string answer is
|
|
192
|
-
* validated just as the terminal prompter does. An answer that fails validation
|
|
193
|
-
* is rejected as a cancellation (`{ answered: false }`) — mirroring the terminal
|
|
194
|
-
* prompter exhausting its retries — so prompt validation is integration-testable
|
|
195
|
-
* via {@linkcode runCommand} rather than silently accepted as the resolved value.
|
|
196
|
-
*
|
|
197
|
-
* @param answers - Ordered queue of answers. Use `PROMPT_CANCEL` for
|
|
198
|
-
* cancellation.
|
|
199
|
-
* @param options - Controls behavior when the queue is exhausted.
|
|
200
|
-
* @returns A {@linkcode PromptEngine} suitable for testing.
|
|
201
|
-
*
|
|
202
|
-
* @example
|
|
203
|
-
* ```ts
|
|
204
|
-
* const prompter = createTestPrompter(['eu', true, PROMPT_CANCEL]);
|
|
205
|
-
*
|
|
206
|
-
* // First promptOne → { answered: true, value: 'eu' }
|
|
207
|
-
* // Second promptOne → { answered: true, value: true }
|
|
208
|
-
* // Third promptOne → { answered: false }
|
|
209
|
-
* ```
|
|
210
|
-
*/
|
|
211
|
-
function createTestPrompter(
|
|
212
|
-
answers: readonly TestAnswer[],
|
|
213
|
-
options?: TestPrompterOptions,
|
|
214
|
-
): PromptEngine {
|
|
215
|
-
let index = 0;
|
|
216
|
-
return {
|
|
217
|
-
promptOne(config): Promise<PromptResult> {
|
|
218
|
-
if (index >= answers.length) {
|
|
219
|
-
if (options?.onExhausted === 'cancel') {
|
|
220
|
-
return Promise.resolve({ answered: false });
|
|
221
|
-
}
|
|
222
|
-
return Promise.reject(
|
|
223
|
-
new Error(
|
|
224
|
-
`Test prompter exhausted: expected at most ${answers.length} prompts, got prompt #${index + 1}`,
|
|
225
|
-
),
|
|
226
|
-
);
|
|
227
|
-
}
|
|
228
|
-
const answer = answers[index];
|
|
229
|
-
index += 1;
|
|
230
|
-
if (answer === PROMPT_CANCEL) {
|
|
231
|
-
return Promise.resolve({ answered: false });
|
|
232
|
-
}
|
|
233
|
-
// Mirror the terminal prompter's `input` contract: run `config.validate`
|
|
234
|
-
// against the queued answer so prompt validation is integration-testable
|
|
235
|
-
// (#36). A failing answer is rejected the way the terminal prompter
|
|
236
|
-
// rejects after exhausting its retries — as a cancellation — so the
|
|
237
|
-
// resolution pipeline surfaces it (required-flag error / default
|
|
238
|
-
// fallback) instead of injecting the invalid value verbatim. Only
|
|
239
|
-
// `input` prompts carry a validator and only string answers are the
|
|
240
|
-
// shape the terminal prompter would ever validate; non-string answers
|
|
241
|
-
// stay verbatim so downstream coercion paths remain testable.
|
|
242
|
-
if (config.kind === 'input' && config.validate !== undefined && typeof answer === 'string') {
|
|
243
|
-
if (config.validate(answer) !== true) {
|
|
244
|
-
return Promise.resolve({ answered: false });
|
|
245
|
-
}
|
|
246
|
-
}
|
|
247
|
-
return Promise.resolve({ answered: true, value: answer });
|
|
248
|
-
},
|
|
249
|
-
};
|
|
250
|
-
}
|
|
251
|
-
|
|
252
|
-
// --- Terminal prompter — line-based interactive prompts
|
|
253
|
-
|
|
254
|
-
/**
|
|
255
|
-
* Create a prompt engine backed by line-based terminal I/O.
|
|
256
|
-
*
|
|
257
|
-
* Uses a {@linkcode ReadFn} for input and a {@linkcode WriteFn} for output. This is the
|
|
258
|
-
* built-in renderer — sufficient for most CLI use cases. For richer
|
|
259
|
-
* TUI experiences, users can implement {@linkcode PromptEngine} with a library
|
|
260
|
-
* like `@clack/prompts` or `inquirer`.
|
|
261
|
-
*
|
|
262
|
-
* The prompter does **not** use raw mode — all input is line-based.
|
|
263
|
-
* This keeps the implementation portable across Node, Bun, and Deno
|
|
264
|
-
* without platform-specific stdin configuration.
|
|
265
|
-
*
|
|
266
|
-
* @param read - Line reader function (returns `null` on EOF)
|
|
267
|
-
* @param write - Output writer function
|
|
268
|
-
* @returns A {@linkcode PromptEngine} that prompts via terminal I/O
|
|
269
|
-
*
|
|
270
|
-
* @example
|
|
271
|
-
* ```ts
|
|
272
|
-
* import { createInterface } from 'readline';
|
|
273
|
-
*
|
|
274
|
-
* const rl = createInterface({ input: process.stdin, output: process.stdout });
|
|
275
|
-
* const read = () => new Promise<string | null>((resolve) => {
|
|
276
|
-
* rl.question('', (answer) => resolve(answer));
|
|
277
|
-
* });
|
|
278
|
-
* const write: WriteFn = (s) => process.stdout.write(s);
|
|
279
|
-
*
|
|
280
|
-
* const prompter = createTerminalPrompter(read, write);
|
|
281
|
-
* ```
|
|
282
|
-
*/
|
|
283
|
-
function createTerminalPrompter(read: ReadFn, write: WriteFn): PromptEngine {
|
|
284
|
-
return {
|
|
285
|
-
promptOne(config): Promise<PromptResult> {
|
|
286
|
-
switch (config.kind) {
|
|
287
|
-
case 'confirm':
|
|
288
|
-
return promptConfirm(config, read, write);
|
|
289
|
-
case 'input':
|
|
290
|
-
return promptInput(config, read, write);
|
|
291
|
-
case 'select':
|
|
292
|
-
return promptSelect(config, read, write);
|
|
293
|
-
case 'multiselect':
|
|
294
|
-
return promptMultiselect(config, read, write);
|
|
295
|
-
}
|
|
296
|
-
},
|
|
297
|
-
};
|
|
298
|
-
}
|
|
299
|
-
|
|
300
|
-
// --- Per-kind prompt implementations
|
|
301
|
-
|
|
302
|
-
/** Maximum retries for invalid input before treating as cancel. */
|
|
303
|
-
const MAX_RETRIES = 10;
|
|
304
|
-
|
|
305
|
-
/**
|
|
306
|
-
* Confirm prompt: yes/no question.
|
|
307
|
-
*
|
|
308
|
-
* The hint reflects {@link ConfirmPromptConfig.default}: `(Y/n)` when the
|
|
309
|
-
* default is `true` (or unset), `(y/N)` when `false`. An empty submission
|
|
310
|
-
* resolves to that default. Accepts: y, yes, n, no, empty. Case-insensitive.
|
|
311
|
-
*
|
|
312
|
-
* @param config - {@link ConfirmPromptConfig} with the question message and optional default
|
|
313
|
-
* @param read - {@link ReadFn} for reading a line of user input
|
|
314
|
-
* @param write - {@link WriteFn} for rendering prompt text
|
|
315
|
-
* @returns Resolved {@link PromptResult} — `true`/`false` value, or cancelled on EOF
|
|
316
|
-
*/
|
|
317
|
-
async function promptConfirm(
|
|
318
|
-
config: ConfirmPromptConfig,
|
|
319
|
-
read: ReadFn,
|
|
320
|
-
write: WriteFn,
|
|
321
|
-
): Promise<PromptResult> {
|
|
322
|
-
const defaultValue = config.default ?? true;
|
|
323
|
-
let retries = 0;
|
|
324
|
-
|
|
325
|
-
while (retries < MAX_RETRIES) {
|
|
326
|
-
const hint = defaultValue ? '(Y/n)' : '(y/N)';
|
|
327
|
-
write(`${config.message} ${hint} `);
|
|
328
|
-
|
|
329
|
-
const line = await read();
|
|
330
|
-
if (line === null) return { answered: false };
|
|
331
|
-
|
|
332
|
-
const lower = line.trim().toLowerCase();
|
|
333
|
-
if (lower === '') {
|
|
334
|
-
return { answered: true, value: defaultValue };
|
|
335
|
-
}
|
|
336
|
-
if (lower === 'y' || lower === 'yes') {
|
|
337
|
-
return { answered: true, value: true };
|
|
338
|
-
}
|
|
339
|
-
if (lower === 'n' || lower === 'no') {
|
|
340
|
-
return { answered: true, value: false };
|
|
341
|
-
}
|
|
342
|
-
|
|
343
|
-
write('Please answer y or n.\n');
|
|
344
|
-
retries += 1;
|
|
345
|
-
}
|
|
346
|
-
|
|
347
|
-
write('Too many invalid attempts.\n');
|
|
348
|
-
return { answered: false };
|
|
349
|
-
}
|
|
350
|
-
|
|
351
|
-
/**
|
|
352
|
-
* Input prompt: free-text string entry.
|
|
353
|
-
*
|
|
354
|
-
* Displays the message and a hint listing the placeholder and/or
|
|
355
|
-
* {@link InputPromptConfig.default | default}, reads a line, and optionally
|
|
356
|
-
* validates via {@link InputPromptConfig.validate | validate}. When a default
|
|
357
|
-
* is configured, an empty submission resolves to it without running validation;
|
|
358
|
-
* otherwise an empty submission is returned verbatim (and treated as "no answer"
|
|
359
|
-
* by the resolver). Loops on invalid input up to `MAX_RETRIES`.
|
|
360
|
-
*
|
|
361
|
-
* @param config - {@link InputPromptConfig} with message, optional placeholder, default, and validator
|
|
362
|
-
* @param read - {@link ReadFn} for reading a line of user input
|
|
363
|
-
* @param write - {@link WriteFn} for rendering prompt text and validation errors
|
|
364
|
-
* @returns Resolved {@link PromptResult} — trimmed string value (or default), or cancelled on EOF/exhausted retries
|
|
365
|
-
*/
|
|
366
|
-
async function promptInput(
|
|
367
|
-
config: InputPromptConfig,
|
|
368
|
-
read: ReadFn,
|
|
369
|
-
write: WriteFn,
|
|
370
|
-
): Promise<PromptResult> {
|
|
371
|
-
let retries = 0;
|
|
372
|
-
|
|
373
|
-
const hints: string[] = [];
|
|
374
|
-
if (config.placeholder !== undefined) hints.push(config.placeholder);
|
|
375
|
-
if (config.default !== undefined) hints.push(`default: ${config.default}`);
|
|
376
|
-
const hint = hints.length > 0 ? ` (${hints.join(', ')})` : '';
|
|
377
|
-
|
|
378
|
-
while (retries < MAX_RETRIES) {
|
|
379
|
-
write(`${config.message}${hint}: `);
|
|
380
|
-
|
|
381
|
-
const line = await read();
|
|
382
|
-
if (line === null) return { answered: false };
|
|
383
|
-
|
|
384
|
-
const trimmed = line.trim();
|
|
385
|
-
|
|
386
|
-
// An empty submission is the absence of a value, not a value to validate:
|
|
387
|
-
// resolve to the configured default, else return blank so the resolver can
|
|
388
|
-
// treat it as "no answer" and fall through to the flag's own default.
|
|
389
|
-
if (trimmed === '') {
|
|
390
|
-
return config.default !== undefined
|
|
391
|
-
? { answered: true, value: config.default }
|
|
392
|
-
: { answered: true, value: '' };
|
|
393
|
-
}
|
|
394
|
-
|
|
395
|
-
if (config.validate !== undefined) {
|
|
396
|
-
const result = config.validate(trimmed);
|
|
397
|
-
if (result !== true) {
|
|
398
|
-
write(`${result}\n`);
|
|
399
|
-
retries += 1;
|
|
400
|
-
continue;
|
|
401
|
-
}
|
|
402
|
-
}
|
|
403
|
-
|
|
404
|
-
return { answered: true, value: trimmed };
|
|
405
|
-
}
|
|
406
|
-
|
|
407
|
-
// Exhausted retries — cancel
|
|
408
|
-
write('Too many invalid attempts.\n');
|
|
409
|
-
return { answered: false };
|
|
410
|
-
}
|
|
411
|
-
|
|
412
|
-
/**
|
|
413
|
-
* Select prompt: single choice from a numbered list.
|
|
414
|
-
*
|
|
415
|
-
* Displays choices with 1-based indices. User enters the number of
|
|
416
|
-
* their selection.
|
|
417
|
-
*
|
|
418
|
-
* @param config - {@link ResolvedSelectPromptConfig} with message and guaranteed non-empty choices
|
|
419
|
-
* @param read - {@link ReadFn} for reading a line of user input
|
|
420
|
-
* @param write - {@link WriteFn} for rendering the choice list and validation errors
|
|
421
|
-
* @returns Resolved {@link PromptResult} — selected {@link SelectChoice.value}, or cancelled on EOF/exhausted retries
|
|
422
|
-
*/
|
|
423
|
-
async function promptSelect(
|
|
424
|
-
config: ResolvedSelectPromptConfig,
|
|
425
|
-
read: ReadFn,
|
|
426
|
-
write: WriteFn,
|
|
427
|
-
): Promise<PromptResult> {
|
|
428
|
-
let retries = 0;
|
|
429
|
-
|
|
430
|
-
while (retries < MAX_RETRIES) {
|
|
431
|
-
write(`${config.message}\n`);
|
|
432
|
-
for (let i = 0; i < config.choices.length; i += 1) {
|
|
433
|
-
const choice = config.choices[i];
|
|
434
|
-
// Choice always defined — guaranteed non-empty tuple and i < length
|
|
435
|
-
if (choice === undefined) continue;
|
|
436
|
-
const label = choice.label ?? choice.value;
|
|
437
|
-
const desc = choice.description !== undefined ? ` - ${choice.description}` : '';
|
|
438
|
-
write(` ${i + 1}) ${label}${desc}\n`);
|
|
439
|
-
}
|
|
440
|
-
write('Enter number: ');
|
|
441
|
-
|
|
442
|
-
const line = await read();
|
|
443
|
-
if (line === null) return { answered: false };
|
|
444
|
-
|
|
445
|
-
const num = Number(line.trim());
|
|
446
|
-
if (Number.isNaN(num) || !Number.isInteger(num) || num < 1 || num > config.choices.length) {
|
|
447
|
-
write(`Please enter a number between 1 and ${config.choices.length}.\n`);
|
|
448
|
-
retries += 1;
|
|
449
|
-
continue;
|
|
450
|
-
}
|
|
451
|
-
|
|
452
|
-
const selected = config.choices[num - 1];
|
|
453
|
-
if (selected === undefined) {
|
|
454
|
-
// Should not happen given bounds check, but satisfies noUncheckedIndexedAccess
|
|
455
|
-
retries += 1;
|
|
456
|
-
continue;
|
|
457
|
-
}
|
|
458
|
-
return { answered: true, value: selected.value };
|
|
459
|
-
}
|
|
460
|
-
|
|
461
|
-
write('Too many invalid attempts.\n');
|
|
462
|
-
return { answered: false };
|
|
463
|
-
}
|
|
464
|
-
|
|
465
|
-
/**
|
|
466
|
-
* Multiselect prompt: multiple choices from a numbered list.
|
|
467
|
-
*
|
|
468
|
-
* Displays choices with 1-based indices. User enters comma-separated
|
|
469
|
-
* numbers of their selections (e.g. `1,3,5`).
|
|
470
|
-
*
|
|
471
|
-
* Validates against `min`/`max` constraints if configured.
|
|
472
|
-
*
|
|
473
|
-
* @param config - {@link ResolvedMultiselectPromptConfig} with message, guaranteed non-empty choices, and optional min/max
|
|
474
|
-
* @param read - {@link ReadFn} for reading a line of user input
|
|
475
|
-
* @param write - {@link WriteFn} for rendering the choice list, hints, and validation errors
|
|
476
|
-
* @returns Resolved {@link PromptResult} — array of selected {@link SelectChoice.value} strings, or cancelled on EOF/exhausted retries
|
|
477
|
-
*/
|
|
478
|
-
async function promptMultiselect(
|
|
479
|
-
config: ResolvedMultiselectPromptConfig,
|
|
480
|
-
read: ReadFn,
|
|
481
|
-
write: WriteFn,
|
|
482
|
-
): Promise<PromptResult> {
|
|
483
|
-
let retries = 0;
|
|
484
|
-
|
|
485
|
-
while (retries < MAX_RETRIES) {
|
|
486
|
-
write(`${config.message}\n`);
|
|
487
|
-
for (let i = 0; i < config.choices.length; i += 1) {
|
|
488
|
-
const choice = config.choices[i];
|
|
489
|
-
if (choice === undefined) continue;
|
|
490
|
-
const label = choice.label ?? choice.value;
|
|
491
|
-
const desc = choice.description !== undefined ? ` - ${choice.description}` : '';
|
|
492
|
-
write(` ${i + 1}) ${label}${desc}\n`);
|
|
493
|
-
}
|
|
494
|
-
|
|
495
|
-
const minHint = config.min !== undefined ? `, min: ${config.min}` : '';
|
|
496
|
-
const maxHint = config.max !== undefined ? `, max: ${config.max}` : '';
|
|
497
|
-
write(`Enter numbers separated by commas (e.g. 1,3${minHint}${maxHint}): `);
|
|
498
|
-
|
|
499
|
-
const line = await read();
|
|
500
|
-
if (line === null) return { answered: false };
|
|
501
|
-
|
|
502
|
-
const trimmed = line.trim();
|
|
503
|
-
if (trimmed === '') {
|
|
504
|
-
// Empty = no selection
|
|
505
|
-
if (config.min !== undefined && config.min > 0) {
|
|
506
|
-
write(`Please select at least ${config.min}.\n`);
|
|
507
|
-
retries += 1;
|
|
508
|
-
continue;
|
|
509
|
-
}
|
|
510
|
-
return { answered: true, value: [] };
|
|
511
|
-
}
|
|
512
|
-
|
|
513
|
-
const parts = trimmed.split(',').map((s) => s.trim());
|
|
514
|
-
const indices: number[] = [];
|
|
515
|
-
let valid = true;
|
|
516
|
-
|
|
517
|
-
for (const part of parts) {
|
|
518
|
-
const num = Number(part);
|
|
519
|
-
if (Number.isNaN(num) || !Number.isInteger(num) || num < 1 || num > config.choices.length) {
|
|
520
|
-
write(`Invalid selection '${part}'. Use numbers between 1 and ${config.choices.length}.\n`);
|
|
521
|
-
valid = false;
|
|
522
|
-
break;
|
|
523
|
-
}
|
|
524
|
-
indices.push(num);
|
|
525
|
-
}
|
|
526
|
-
|
|
527
|
-
if (!valid) {
|
|
528
|
-
retries += 1;
|
|
529
|
-
continue;
|
|
530
|
-
}
|
|
531
|
-
|
|
532
|
-
// Deduplicate
|
|
533
|
-
const unique = [...new Set(indices)];
|
|
534
|
-
|
|
535
|
-
if (config.min !== undefined && unique.length < config.min) {
|
|
536
|
-
write(`Please select at least ${config.min}.\n`);
|
|
537
|
-
retries += 1;
|
|
538
|
-
continue;
|
|
539
|
-
}
|
|
540
|
-
|
|
541
|
-
if (config.max !== undefined && unique.length > config.max) {
|
|
542
|
-
write(`Please select at most ${config.max}.\n`);
|
|
543
|
-
retries += 1;
|
|
544
|
-
continue;
|
|
545
|
-
}
|
|
546
|
-
|
|
547
|
-
const selected = unique.map((n) => {
|
|
548
|
-
const choice = config.choices[n - 1];
|
|
549
|
-
// Bounds already validated above; satisfies noUncheckedIndexedAccess
|
|
550
|
-
return choice?.value ?? '';
|
|
551
|
-
});
|
|
552
|
-
|
|
553
|
-
return { answered: true, value: selected };
|
|
554
|
-
}
|
|
555
|
-
|
|
556
|
-
write('Too many invalid attempts.\n');
|
|
557
|
-
return { answered: false };
|
|
558
|
-
}
|
|
559
|
-
|
|
560
|
-
// --- Utility: prepare resolved prompt config from raw config + flag schema
|
|
561
|
-
|
|
562
|
-
/**
|
|
563
|
-
* Prepare a {@linkcode ResolvedPromptConfig} from a raw {@linkcode PromptConfig} and optional
|
|
564
|
-
* enum values from the flag schema.
|
|
565
|
-
*
|
|
566
|
-
* For select/multiselect prompts without explicit choices, this merges
|
|
567
|
-
* the flag's enum values into the choices list. Throws if no choices
|
|
568
|
-
* are available.
|
|
569
|
-
*
|
|
570
|
-
* This function is called by the resolution chain (prompt-resolve-1),
|
|
571
|
-
* not by the engine itself — keeping the engine free of schema knowledge.
|
|
572
|
-
*
|
|
573
|
-
* @param config - Raw prompt config from `FlagSchema.prompt`
|
|
574
|
-
* @param enumValues - Optional enum values from `FlagSchema.enumValues`
|
|
575
|
-
* @returns Resolved config with choices guaranteed for select kinds
|
|
576
|
-
* @throws Error if select/multiselect has no choices and no enum values
|
|
577
|
-
*/
|
|
578
|
-
function resolvePromptConfig(
|
|
579
|
-
config: PromptConfig,
|
|
580
|
-
enumValues: readonly string[] | undefined,
|
|
581
|
-
): ResolvedPromptConfig {
|
|
582
|
-
if (config.kind === 'confirm' || config.kind === 'input') {
|
|
583
|
-
return config;
|
|
584
|
-
}
|
|
585
|
-
|
|
586
|
-
// Select or multiselect — ensure choices are present
|
|
587
|
-
const rawChoices: readonly SelectChoice[] =
|
|
588
|
-
config.choices ?? enumValues?.map((v) => ({ value: v })) ?? [];
|
|
589
|
-
|
|
590
|
-
if (rawChoices.length === 0) {
|
|
591
|
-
throw new Error(
|
|
592
|
-
`Prompt config (${config.kind}) requires choices but none were provided and no enum values are available`,
|
|
593
|
-
);
|
|
594
|
-
}
|
|
595
|
-
|
|
596
|
-
// Non-empty assertion: we just checked length > 0
|
|
597
|
-
const choices = rawChoices as readonly [SelectChoice, ...SelectChoice[]];
|
|
598
|
-
|
|
599
|
-
if (config.kind === 'select') {
|
|
600
|
-
return { kind: 'select', message: config.message, choices };
|
|
601
|
-
}
|
|
602
|
-
|
|
603
|
-
return {
|
|
604
|
-
kind: 'multiselect',
|
|
605
|
-
message: config.message,
|
|
606
|
-
choices,
|
|
607
|
-
...(config.min !== undefined ? { min: config.min } : {}),
|
|
608
|
-
...(config.max !== undefined ? { max: config.max } : {}),
|
|
609
|
-
};
|
|
610
|
-
}
|
|
611
|
-
|
|
612
|
-
// --- Exports
|
|
613
|
-
|
|
614
|
-
export type {
|
|
615
|
-
PromptEngine,
|
|
616
|
-
ReadFn,
|
|
617
|
-
ResolvedMultiselectPromptConfig,
|
|
618
|
-
ResolvedPromptConfig,
|
|
619
|
-
ResolvedSelectPromptConfig,
|
|
620
|
-
TestAnswer,
|
|
621
|
-
TestPrompterOptions,
|
|
622
|
-
};
|
|
623
|
-
export { createTerminalPrompter, createTestPrompter, PROMPT_CANCEL, resolvePromptConfig };
|