@kjanat/dreamcli 2.5.0 → 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.
Files changed (160) hide show
  1. package/CHANGELOG.md +153 -1
  2. package/README.md +9 -2
  3. package/dist/core/cli/dispatch.mjs +214 -1
  4. package/dist/core/cli/help-links.mjs +41 -1
  5. package/dist/core/cli/index.d.mts +109 -16
  6. package/dist/core/cli/index.mjs +901 -1
  7. package/dist/core/cli/planner.d.mts +27 -5
  8. package/dist/core/cli/planner.mjs +300 -1
  9. package/dist/core/cli/plugin.mjs +41 -1
  10. package/dist/core/cli/propagate.mjs +57 -1
  11. package/dist/core/cli/root-help.d.mts +16 -2
  12. package/dist/core/cli/root-help.mjs +208 -6
  13. package/dist/core/cli/root-surface.d.mts +18 -3
  14. package/dist/core/cli/root-surface.mjs +27 -1
  15. package/dist/core/cli/runtime-preflight.d.mts +16 -1
  16. package/dist/core/cli/runtime-preflight.mjs +168 -1
  17. package/dist/core/completion/index.d.mts +32 -1
  18. package/dist/core/completion/index.mjs +90 -1
  19. package/dist/core/completion/shells/bash.mjs +304 -3
  20. package/dist/core/completion/shells/fish.mjs +159 -2
  21. package/dist/core/completion/shells/powershell.mjs +353 -2
  22. package/dist/core/completion/shells/shared.d.mts +10 -0
  23. package/dist/core/completion/shells/shared.mjs +154 -1
  24. package/dist/core/completion/shells/zsh.mjs +244 -2
  25. package/dist/core/config/index.mjs +246 -1
  26. package/dist/core/config/package-json.mjs +385 -3
  27. package/dist/core/errors/index.mjs +87 -1
  28. package/dist/core/execution/index.mjs +189 -1
  29. package/dist/core/help/ansi.mjs +122 -2
  30. package/dist/core/help/index.d.mts +45 -1
  31. package/dist/core/help/index.mjs +329 -7
  32. package/dist/core/json-schema/index.mjs +487 -9
  33. package/dist/core/json-schema/meta-descriptions.generated.d.mts +38 -38
  34. package/dist/core/json-schema/meta-descriptions.generated.mjs +93 -28
  35. package/dist/core/output/activity.mjs +452 -1
  36. package/dist/core/output/bind.mjs +56 -1
  37. package/dist/core/output/contracts.mjs +57 -1
  38. package/dist/core/output/display-value.mjs +32 -1
  39. package/dist/core/output/index.d.mts +21 -0
  40. package/dist/core/output/index.mjs +442 -2
  41. package/dist/core/output/renderers.mjs +46 -1
  42. package/dist/core/output/writer.mjs +12 -1
  43. package/dist/core/parse/index.d.mts +15 -1
  44. package/dist/core/parse/index.mjs +555 -1
  45. package/dist/core/prompt/index.d.mts +11 -4
  46. package/dist/core/prompt/index.mjs +348 -6
  47. package/dist/core/resolve/args.mjs +105 -1
  48. package/dist/core/resolve/coerce.mjs +279 -1
  49. package/dist/core/resolve/config.mjs +19 -1
  50. package/dist/core/resolve/errors.mjs +140 -2
  51. package/dist/core/resolve/flags.mjs +196 -1
  52. package/dist/core/resolve/index.mjs +85 -1
  53. package/dist/core/resolve/property.d.mts +2 -1
  54. package/dist/core/resolve/property.mjs +45 -1
  55. package/dist/core/schema/arg.d.mts +90 -10
  56. package/dist/core/schema/arg.mjs +463 -1
  57. package/dist/core/schema/command.d.mts +21 -1
  58. package/dist/core/schema/command.mjs +712 -1
  59. package/dist/core/schema/flag.d.mts +92 -2
  60. package/dist/core/schema/flag.mjs +453 -1
  61. package/dist/core/schema/index.d.mts +2 -1
  62. package/dist/core/schema/middleware.mjs +41 -1
  63. package/dist/core/schema/number-constraints.d.mts +96 -0
  64. package/dist/core/schema/number-constraints.mjs +60 -0
  65. package/dist/core/schema/prompt.d.mts +15 -0
  66. package/dist/core/schema/run.d.mts +4 -4
  67. package/dist/core/schema-dsl/runtime.mjs +271 -2
  68. package/dist/core/schema-dsl/to-json-schema.mjs +74 -1
  69. package/dist/core/testkit/index.d.mts +9 -5
  70. package/dist/core/testkit/index.mjs +83 -1
  71. package/dist/index.d.mts +3 -1
  72. package/dist/index.mjs +22 -1
  73. package/dist/runtime/adapter.mjs +82 -1
  74. package/dist/runtime/auto.mjs +46 -1
  75. package/dist/runtime/bun.mjs +28 -1
  76. package/dist/runtime/deno.mjs +217 -2
  77. package/dist/runtime/detect.mjs +45 -1
  78. package/dist/runtime/node.mjs +169 -2
  79. package/dist/runtime/paths.mjs +48 -1
  80. package/dist/runtime.mjs +7 -1
  81. package/dist/strings.mjs +22 -1
  82. package/dist/testkit.mjs +5 -1
  83. package/dist/version.mjs +7 -1
  84. package/examples/basic.ts +5 -1
  85. package/examples/gh/package.json +2 -7
  86. package/examples/pwsh-demo/package.json +1 -7
  87. package/examples/pwsh-demo/src/main.test.ts +1 -4
  88. package/examples/pwsh-demo/src/main.ts +14 -4
  89. package/examples/transport-launcher.ts +85 -0
  90. package/package.json +23 -41
  91. package/dist/runtime/support.d.mts +0 -42
  92. package/dist/runtime/support.mjs +0 -1
  93. package/src/core/cli/dispatch.ts +0 -349
  94. package/src/core/cli/help-links.ts +0 -82
  95. package/src/core/cli/index.ts +0 -1413
  96. package/src/core/cli/planner.ts +0 -487
  97. package/src/core/cli/plugin.ts +0 -117
  98. package/src/core/cli/propagate.ts +0 -97
  99. package/src/core/cli/root-help.ts +0 -252
  100. package/src/core/cli/root-surface.ts +0 -74
  101. package/src/core/cli/runtime-preflight.ts +0 -438
  102. package/src/core/completion/index.ts +0 -88
  103. package/src/core/completion/shells/bash.ts +0 -459
  104. package/src/core/completion/shells/fish.ts +0 -259
  105. package/src/core/completion/shells/powershell.ts +0 -439
  106. package/src/core/completion/shells/shared.ts +0 -283
  107. package/src/core/completion/shells/zsh.ts +0 -381
  108. package/src/core/config/index.ts +0 -417
  109. package/src/core/config/package-json.ts +0 -560
  110. package/src/core/errors/index.ts +0 -193
  111. package/src/core/execution/index.ts +0 -268
  112. package/src/core/help/ansi.ts +0 -144
  113. package/src/core/help/index.ts +0 -516
  114. package/src/core/json-schema/index.ts +0 -891
  115. package/src/core/json-schema/meta-descriptions.generated.ts +0 -185
  116. package/src/core/output/activity.ts +0 -575
  117. package/src/core/output/bind.ts +0 -59
  118. package/src/core/output/contracts.ts +0 -194
  119. package/src/core/output/display-value.ts +0 -48
  120. package/src/core/output/index.ts +0 -704
  121. package/src/core/output/renderers.ts +0 -125
  122. package/src/core/output/writer.ts +0 -46
  123. package/src/core/parse/index.ts +0 -681
  124. package/src/core/prompt/index.ts +0 -583
  125. package/src/core/resolve/args.ts +0 -144
  126. package/src/core/resolve/coerce.ts +0 -432
  127. package/src/core/resolve/config.ts +0 -26
  128. package/src/core/resolve/contracts.ts +0 -135
  129. package/src/core/resolve/errors.ts +0 -227
  130. package/src/core/resolve/flags.ts +0 -255
  131. package/src/core/resolve/index.ts +0 -108
  132. package/src/core/resolve/property.ts +0 -99
  133. package/src/core/schema/activity.ts +0 -240
  134. package/src/core/schema/arg.ts +0 -719
  135. package/src/core/schema/command.ts +0 -1498
  136. package/src/core/schema/flag.ts +0 -801
  137. package/src/core/schema/index.ts +0 -91
  138. package/src/core/schema/middleware.ts +0 -171
  139. package/src/core/schema/prompt.ts +0 -156
  140. package/src/core/schema/run.ts +0 -198
  141. package/src/core/schema-dsl/index.ts +0 -109
  142. package/src/core/schema-dsl/parse.ts +0 -262
  143. package/src/core/schema-dsl/runtime.ts +0 -609
  144. package/src/core/schema-dsl/to-json-schema.ts +0 -102
  145. package/src/core/testkit/index.ts +0 -123
  146. package/src/index.ts +0 -179
  147. package/src/runtime/adapter.ts +0 -303
  148. package/src/runtime/auto.ts +0 -110
  149. package/src/runtime/bun.ts +0 -55
  150. package/src/runtime/deno.ts +0 -369
  151. package/src/runtime/detect.ts +0 -105
  152. package/src/runtime/index.ts +0 -21
  153. package/src/runtime/node-builtins.d.ts +0 -27
  154. package/src/runtime/node.ts +0 -300
  155. package/src/runtime/paths.ts +0 -64
  156. package/src/runtime/support.ts +0 -135
  157. package/src/runtime.ts +0 -31
  158. package/src/strings.ts +0 -34
  159. package/src/testkit.ts +0 -25
  160. package/src/version.ts +0 -20
@@ -1,438 +0,0 @@
1
- /**
2
- * Runtime preflight helpers for CLIBuilder.run().
3
- *
4
- * Separates adapter-driven sourcing work from planner and executor logic:
5
- * config discovery, package metadata discovery, invocation-scoped stdin reads,
6
- * prompt auto-wiring, and runtime-derived schema overrides.
7
- *
8
- * @module dreamcli/core/cli/runtime-preflight
9
- * @internal
10
- */
11
-
12
- import type { FormatLoader } from '#internals/core/config/index.ts';
13
- import { discoverConfig } from '#internals/core/config/index.ts';
14
- import type { PackageJsonData } from '#internals/core/config/package-json.ts';
15
- import { discoverManifest, inferCliName } from '#internals/core/config/package-json.ts';
16
- import { CLIError, ParseError } from '#internals/core/errors/index.ts';
17
- import type { Verbosity } from '#internals/core/output/index.ts';
18
- import { parse } from '#internals/core/parse/index.ts';
19
- import type { PromptEngine } from '#internals/core/prompt/index.ts';
20
- import { createTerminalPrompter } from '#internals/core/prompt/index.ts';
21
- import type { CommandSchema, ErasedCommand } from '#internals/core/schema/command.ts';
22
- import type { RuntimeAdapter } from '#internals/runtime/adapter.ts';
23
- import type { HelpLinks } from './help-links.ts';
24
- import { deriveHelpLinks } from './help-links.ts';
25
- import { planInvocation } from './planner.ts';
26
- import type { CLIPlugin } from './plugin.ts';
27
-
28
- /** Config discovery settings extracted from CLISchema for preflight use. @internal */
29
- interface RuntimeConfigSettings {
30
- /** Application name used to locate config files (e.g. `~/.config/<appName>/`). */
31
- readonly appName: string;
32
- /** Optional format loaders (JSON/YAML/TOML); `undefined` uses built-in JSON. */
33
- readonly loaders: readonly FormatLoader[] | undefined;
34
- }
35
-
36
- /**
37
- * Manifest discovery settings extracted from CLISchema for preflight use.
38
- *
39
- * Mirrors the public `ResolvedManifestSettings` shape (consumed structurally),
40
- * named with the `Runtime` prefix to match its {@link RuntimeConfigSettings}
41
- * sibling and avoid colliding with the exported public type.
42
- * @internal
43
- */
44
- interface RuntimeManifestSettings {
45
- /** Whether to infer the CLI binary name from `bin` keys or `name`. */
46
- readonly inferName: boolean;
47
- /** Strip a leading `@scope/` from the inferred `name` fallback. */
48
- readonly stripScope: boolean;
49
- /** Explicit anchor (resolved path) for discovery; `undefined` falls back to `adapter.cwd`. */
50
- readonly from: string | undefined;
51
- /** Candidate manifest filenames in priority order. */
52
- readonly files: readonly string[];
53
- /** Pre-loaded data; when set, discovery is skipped entirely. */
54
- readonly data: PackageJsonData | undefined;
55
- }
56
-
57
- /**
58
- * Structural subset of CLISchema used by runtime preflight.
59
- *
60
- * Decouples adapter-driven sourcing (config, package.json, stdin) from the
61
- * full CLIBuilder surface so preflight can be tested independently.
62
- * @internal
63
- */
64
- interface RuntimePreflightSchemaLike {
65
- /** CLI program name; may be overridden by package.json discovery or inheritance. */
66
- readonly name: string;
67
- /** Whether this CLI inherits its name from a parent (nested CLI embedding). */
68
- readonly inheritName: boolean;
69
- /** Declared version; `undefined` means version will be inferred from package.json if available. */
70
- readonly version: string | undefined;
71
- /** Declared description; `undefined` allows package.json inference. */
72
- readonly description: string | undefined;
73
- /** Registered top-level commands for dispatch planning during stdin detection. */
74
- readonly commands: readonly ErasedCommand[];
75
- /** Fallback command when no subcommand token matches. */
76
- readonly defaultCommand: ErasedCommand | undefined;
77
- /** Config file discovery settings; `undefined` disables config loading. */
78
- readonly configSettings: RuntimeConfigSettings | undefined;
79
- /** Package.json discovery settings; `undefined` disables package.json inference. */
80
- readonly packageJsonSettings: RuntimeManifestSettings | undefined;
81
- /** Root-help header link targets; `undefined` fields may be derived from package.json. */
82
- readonly helpLinks: HelpLinks | undefined;
83
- /** Whether `.completions()` registered the built-in completions command. */
84
- readonly hasBuiltInCompletions: boolean;
85
- /** Plugins forwarded into the execution pipeline. */
86
- readonly plugins: readonly CLIPlugin[];
87
- }
88
-
89
- /**
90
- * Caller-supplied overrides for runtime preflight.
91
- *
92
- * When provided, these bypass adapter auto-detection (useful for testing
93
- * and the `CLIRunOptions` public surface).
94
- * @internal
95
- */
96
- interface RuntimePreflightOptions {
97
- /** Environment variables override; bypasses adapter env when set. */
98
- readonly env?: Readonly<Record<string, string | undefined>>;
99
- /** Pre-loaded config object; skips config file discovery when set. */
100
- readonly config?: Readonly<Record<string, unknown>>;
101
- /** Pre-read stdin data; `null` means stdin was explicitly empty. */
102
- readonly stdinData?: string | null;
103
- /** Custom prompt engine; bypasses terminal prompter auto-creation. */
104
- readonly prompter?: PromptEngine;
105
- /** Output verbosity level override. */
106
- readonly verbosity?: Verbosity;
107
- /** Force JSON output mode regardless of `--json` flag presence. */
108
- readonly jsonMode?: boolean;
109
- /** TTY detection override; bypasses adapter TTY check. */
110
- readonly isTTY?: boolean;
111
- }
112
-
113
- /**
114
- * Fully resolved runtime inputs ready for the execution pipeline.
115
- *
116
- * All adapter vs. caller-override decisions are settled by the time this
117
- * is constructed; the executor treats these as final truth.
118
- * @internal
119
- */
120
- interface RuntimeExecutionInputs {
121
- /** Resolved environment variables (from adapter or caller override). */
122
- readonly env: Readonly<Record<string, string | undefined>>;
123
- /** Whether stdout is a TTY (controls color, spinners, interactive prompts). */
124
- readonly isTTY: boolean;
125
- /** Whether structured JSON output mode is active. */
126
- readonly jsonMode: boolean;
127
- /** Output verbosity level. */
128
- readonly verbosity: Verbosity;
129
- /** Pre-read stdin data if the invocation declared stdin-mode args. */
130
- readonly stdinData?: string | null;
131
- /** Prompt engine for interactive flag resolution; absent in non-TTY. */
132
- readonly prompter?: PromptEngine;
133
- /** Loaded config data for the config resolution step. */
134
- readonly config?: Readonly<Record<string, unknown>>;
135
- }
136
-
137
- /** Preflight succeeded — all runtime inputs are resolved and ready for execution. @internal */
138
- interface ReadyRuntimePreflight {
139
- /** Discriminant — preflight completed without errors. */
140
- readonly kind: 'ready';
141
- /** Schema after package.json discovery and name inheritance applied. */
142
- readonly schema: RuntimePreflightSchemaLike;
143
- /** Argv with valid runtime `--config` tokens stripped out. */
144
- readonly filteredArgv: readonly string[];
145
- /** Fully resolved runtime inputs for the execution pipeline. */
146
- readonly inputs: RuntimeExecutionInputs;
147
- }
148
-
149
- /** Preflight failed during config file discovery/loading. @internal */
150
- interface RuntimeConfigErrorPreflight {
151
- /** Discriminant — config loading produced a structured error. */
152
- readonly kind: 'config-error';
153
- /** The config discovery/parse error to render. */
154
- readonly error: CLIError;
155
- /** Whether JSON output was requested (needed to choose error rendering). */
156
- readonly jsonMode: boolean;
157
- }
158
-
159
- /** Discriminated union of preflight outcomes — either ready or config-error. @internal */
160
- type RuntimePreflightResult = ReadyRuntimePreflight | RuntimeConfigErrorPreflight;
161
-
162
- /** Options bag for {@linkcode prepareRuntimePreflight}. @internal */
163
- interface PrepareRuntimePreflightOptions {
164
- /** CLI schema subset driving preflight decisions. */
165
- readonly schema: RuntimePreflightSchemaLike;
166
- /** Runtime adapter providing argv, env, stdin, and filesystem access. */
167
- readonly adapter: RuntimeAdapter;
168
- /** Caller-supplied overrides; `undefined` means auto-detect everything. */
169
- readonly options: RuntimePreflightOptions | undefined;
170
- /** Name inherited from a parent CLI (nested embedding); `undefined` for standalone. */
171
- readonly inheritedName: string | undefined;
172
- }
173
-
174
- const PRECHECK_OUTPUT = {
175
- jsonMode: false,
176
- isTTY: false,
177
- verbosity: 'normal' as const,
178
- };
179
-
180
- /** Extract and strip valid runtime `--config` forms from argv, returning path + filtered tokens. @internal */
181
- function extractConfigFlag(argv: readonly string[]): {
182
- readonly configPath: string | undefined;
183
- readonly filteredArgv: readonly string[];
184
- } {
185
- const filteredArgv: string[] = [];
186
- let configPath: string | undefined;
187
-
188
- for (let i = 0; i < argv.length; i += 1) {
189
- const arg = argv[i];
190
- if (arg === undefined) {
191
- continue;
192
- }
193
-
194
- if (arg === '--') {
195
- filteredArgv.push(...argv.slice(i));
196
- break;
197
- }
198
-
199
- if (configPath === undefined && arg.startsWith('--config=')) {
200
- const value = arg.slice('--config='.length);
201
- if (value.length > 0) {
202
- configPath = value;
203
- continue;
204
- }
205
-
206
- filteredArgv.push(arg);
207
- continue;
208
- }
209
-
210
- if (configPath === undefined && arg === '--config') {
211
- const nextArg = argv[i + 1];
212
- if (nextArg !== undefined && !nextArg.startsWith('-') && nextArg !== '--') {
213
- configPath = nextArg;
214
- i += 1;
215
- continue;
216
- }
217
-
218
- filteredArgv.push(arg);
219
- continue;
220
- }
221
-
222
- filteredArgv.push(arg);
223
- }
224
-
225
- return { configPath, filteredArgv };
226
- }
227
-
228
- /** Check whether a single command's args declare stdin-mode and argv leaves them unresolved. @internal */
229
- function commandInvocationNeedsStdin(schema: CommandSchema, argv: readonly string[]): boolean {
230
- if (argv.includes('--help') || argv.includes('-h')) {
231
- return false;
232
- }
233
-
234
- try {
235
- const parsed = parse(schema, argv);
236
- return schema.args.some(({ name, schema: argSchema }) => {
237
- const parsedValue = parsed.args[name];
238
- return argSchema.stdinMode && (parsedValue === undefined || parsedValue === '-');
239
- });
240
- } catch (error: unknown) {
241
- if (error instanceof ParseError) {
242
- return false;
243
- }
244
- throw error;
245
- }
246
- }
247
-
248
- /** Plan the invocation and check whether the matched command needs stdin data. @internal */
249
- function invocationNeedsStdin(
250
- schema: RuntimePreflightSchemaLike,
251
- argv: readonly string[],
252
- ): boolean {
253
- const plan = planInvocation({
254
- schema,
255
- argv,
256
- help: { binName: schema.name },
257
- output: PRECHECK_OUTPUT,
258
- });
259
-
260
- switch (plan.kind) {
261
- case 'match':
262
- return commandInvocationNeedsStdin(plan.plan.mergedSchema, plan.plan.argv);
263
- case 'dispatch-error':
264
- case 'needs-subcommand':
265
- case 'root-help':
266
- case 'root-version':
267
- return false;
268
- }
269
- }
270
-
271
- function isCompletionsInvocation(
272
- schema: RuntimePreflightSchemaLike,
273
- argv: readonly string[],
274
- ): boolean {
275
- if (!schema.hasBuiltInCompletions) {
276
- return false;
277
- }
278
-
279
- const plan = planInvocation({
280
- schema,
281
- argv,
282
- help: { binName: schema.name },
283
- output: PRECHECK_OUTPUT,
284
- });
285
-
286
- return plan.kind === 'match' && plan.plan.command.schema.name === 'completions';
287
- }
288
-
289
- async function applyPackageJsonDiscovery(
290
- schema: RuntimePreflightSchemaLike,
291
- adapter: RuntimeAdapter,
292
- inheritedName: string | undefined,
293
- isCompletions: boolean,
294
- ): Promise<RuntimePreflightSchemaLike> {
295
- const packageJsonSettings = schema.packageJsonSettings;
296
- const packageSchema =
297
- packageJsonSettings !== undefined && !isCompletions
298
- ? await (async (): Promise<RuntimePreflightSchemaLike> => {
299
- // Pre-loaded data short-circuits filesystem discovery entirely.
300
- const pkg =
301
- packageJsonSettings.data ??
302
- (await discoverManifest(adapter, {
303
- ...(packageJsonSettings.from !== undefined ? { startDir: packageJsonSettings.from } : {}),
304
- files: packageJsonSettings.files,
305
- }));
306
- if (pkg === null) return schema;
307
-
308
- const inferredName = packageJsonSettings.inferName
309
- ? inferCliName(pkg, { stripScope: packageJsonSettings.stripScope })
310
- : undefined;
311
- return {
312
- ...schema,
313
- ...(schema.version === undefined && pkg.version !== undefined
314
- ? { version: pkg.version }
315
- : {}),
316
- ...(schema.description === undefined && pkg.description !== undefined
317
- ? { description: pkg.description }
318
- : {}),
319
- ...(inferredName !== undefined ? { name: inferredName } : {}),
320
- helpLinks: deriveHelpLinks(schema.helpLinks, pkg, schema.version ?? pkg.version),
321
- };
322
- })()
323
- : schema;
324
-
325
- return inheritedName !== undefined ? { ...packageSchema, name: inheritedName } : packageSchema;
326
- }
327
-
328
- async function loadRuntimeConfig(
329
- schema: RuntimePreflightSchemaLike,
330
- adapter: RuntimeAdapter,
331
- configPath: string | undefined,
332
- isCompletions: boolean,
333
- existingConfig: Readonly<Record<string, unknown>> | undefined,
334
- ): Promise<Readonly<Record<string, unknown>> | CLIError | undefined> {
335
- if (schema.configSettings === undefined || isCompletions || existingConfig !== undefined) {
336
- return existingConfig;
337
- }
338
-
339
- try {
340
- const result = await discoverConfig(schema.configSettings.appName, adapter, {
341
- ...(configPath !== undefined ? { configPath } : {}),
342
- ...(schema.configSettings.loaders !== undefined
343
- ? { loaders: schema.configSettings.loaders }
344
- : {}),
345
- });
346
- return result.found ? result.data : undefined;
347
- } catch (error: unknown) {
348
- if (error instanceof CLIError) {
349
- return error;
350
- }
351
- throw error;
352
- }
353
- }
354
-
355
- /**
356
- * Run all adapter-driven sourcing work before command execution.
357
- *
358
- * Discovers config files, reads package.json metadata, detects stdin needs,
359
- * wires up the prompt engine, and resolves output policy overrides into a
360
- * single {@linkcode RuntimePreflightResult}.
361
- * @internal
362
- */
363
- async function prepareRuntimePreflight(
364
- options: PrepareRuntimePreflightOptions,
365
- ): Promise<RuntimePreflightResult> {
366
- const rawArgv = options.adapter.argv.slice(2);
367
- const { configPath, filteredArgv } =
368
- options.schema.configSettings !== undefined
369
- ? extractConfigFlag(rawArgv)
370
- : { configPath: undefined, filteredArgv: rawArgv };
371
- const hasJsonFlag = filteredArgv.includes('--json');
372
- const jsonMode = hasJsonFlag || options.options?.jsonMode === true;
373
- const isCompletions = isCompletionsInvocation(options.schema, filteredArgv);
374
- const schema = await applyPackageJsonDiscovery(
375
- options.schema,
376
- options.adapter,
377
- options.inheritedName,
378
- isCompletions,
379
- );
380
- const loadedConfig = await loadRuntimeConfig(
381
- schema,
382
- options.adapter,
383
- configPath,
384
- isCompletions,
385
- options.options?.config,
386
- );
387
-
388
- if (loadedConfig instanceof CLIError) {
389
- return {
390
- kind: 'config-error',
391
- error: loadedConfig,
392
- jsonMode,
393
- };
394
- }
395
-
396
- const autoPrompter =
397
- options.options?.prompter === undefined && options.adapter.stdinIsTTY
398
- ? createTerminalPrompter(options.adapter.stdin, options.adapter.stderr)
399
- : undefined;
400
- const stdinData =
401
- options.options?.stdinData === undefined && invocationNeedsStdin(schema, filteredArgv)
402
- ? await options.adapter.readStdin()
403
- : options.options?.stdinData;
404
-
405
- return {
406
- kind: 'ready',
407
- schema,
408
- filteredArgv,
409
- inputs: {
410
- env: options.options?.env ?? options.adapter.env,
411
- isTTY: options.options?.isTTY ?? options.adapter.isTTY,
412
- jsonMode,
413
- verbosity: options.options?.verbosity ?? 'normal',
414
- ...(stdinData !== undefined ? { stdinData } : {}),
415
- ...(options.options?.prompter !== undefined
416
- ? { prompter: options.options.prompter }
417
- : autoPrompter !== undefined
418
- ? { prompter: autoPrompter }
419
- : {}),
420
- ...(loadedConfig !== undefined ? { config: loadedConfig } : {}),
421
- },
422
- };
423
- }
424
-
425
- export type {
426
- PrepareRuntimePreflightOptions,
427
- ReadyRuntimePreflight,
428
- RuntimeExecutionInputs,
429
- RuntimePreflightOptions,
430
- RuntimePreflightResult,
431
- RuntimePreflightSchemaLike,
432
- };
433
- export {
434
- commandInvocationNeedsStdin,
435
- extractConfigFlag,
436
- invocationNeedsStdin,
437
- prepareRuntimePreflight,
438
- };
@@ -1,88 +0,0 @@
1
- /**
2
- * Shell completion script generation from command schemas.
3
- *
4
- * Each shell has a dedicated generator in `shells/` that takes a
5
- * {@link CLISchema} and returns a complete shell-specific completion
6
- * script as a string. Generators walk the full command tree depth,
7
- * including propagated flags from ancestor commands at each nesting level.
8
- *
9
- * @module dreamcli/core/completion
10
- */
11
-
12
- import type { CLISchema } from '#internals/core/cli/index.ts';
13
- import { generateBashCompletion } from './shells/bash.ts';
14
- import { generateFishCompletion } from './shells/fish.ts';
15
- import { generatePowerShellCompletion } from './shells/powershell.ts';
16
- import type { CompletionOptions } from './shells/shared.ts';
17
- import { generateZshCompletion } from './shells/zsh.ts';
18
-
19
- // --- Shell type — supported completion targets
20
-
21
- /**
22
- * Supported shell targets for completion script generation.
23
- *
24
- * `bash`, `zsh`, `fish`, and `powershell` are implemented today.
25
- */
26
- type Shell = 'bash' | 'zsh' | 'fish' | 'powershell';
27
-
28
- /**
29
- * Implemented shell values as a frozen readonly non-empty tuple.
30
- *
31
- * Use this tuple for user-facing validation and shell selection UIs.
32
- * It intentionally matches the shipped {@link Shell} union exactly so docs,
33
- * help output, and completion generation advertise the same support surface.
34
- *
35
- * @see {@link Shell} for the union type matching these entries.
36
- */
37
- const SHELLS: Readonly<readonly ['bash', 'zsh', 'fish', 'powershell']> = Object.freeze([
38
- 'bash',
39
- 'zsh',
40
- 'fish',
41
- 'powershell',
42
- ] as const satisfies readonly ['bash', 'zsh', 'fish', 'powershell']);
43
-
44
- // --- Shell-agnostic dispatch
45
-
46
- /**
47
- * Generate a completion script for the given shell.
48
- *
49
- * This is the primary completion entrypoint for most consumers. Pass a CLI
50
- * schema and target shell, then write the returned script to a file or source
51
- * it directly from the command line.
52
- *
53
- * @param schema - The CLI schema describing commands, flags, and args.
54
- * @param shell - Target shell.
55
- * @param options - Optional generator configuration such as function naming
56
- * and root default-command completion behavior.
57
- * @returns A complete shell completion script as a string.
58
- *
59
- * @example
60
- * ```ts
61
- * const script = generateCompletion(app.schema, 'bash');
62
- * // e.g. source <(mycli completions bash)
63
- * ```
64
- */
65
- function generateCompletion(schema: CLISchema, shell: Shell, options?: CompletionOptions): string {
66
- switch (shell) {
67
- case 'bash':
68
- return generateBashCompletion(schema, options);
69
- case 'zsh':
70
- return generateZshCompletion(schema, options);
71
- case 'fish':
72
- return generateFishCompletion(schema, options);
73
- case 'powershell':
74
- return generatePowerShellCompletion(schema, options);
75
- }
76
- }
77
-
78
- // --- Exports
79
-
80
- export type { CompletionOptions, Shell };
81
- export {
82
- generateBashCompletion,
83
- generateCompletion,
84
- generateFishCompletion,
85
- generatePowerShellCompletion,
86
- generateZshCompletion,
87
- SHELLS,
88
- };