@kjanat/dreamcli 1.0.0 → 2.0.0
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 +45 -6
- package/LICENSE +1 -1
- package/README.md +296 -164
- package/dist/core/cli/dispatch.d.mts +85 -0
- package/dist/core/cli/dispatch.mjs +1 -0
- package/dist/core/cli/index.d.mts +418 -0
- package/dist/core/cli/index.mjs +1 -0
- package/dist/core/cli/planner.d.mts +143 -0
- package/dist/core/cli/planner.mjs +1 -0
- package/dist/core/cli/plugin.d.mts +94 -0
- package/dist/core/cli/plugin.mjs +1 -0
- package/dist/core/cli/propagate.d.mts +39 -0
- package/dist/core/cli/propagate.mjs +1 -0
- package/dist/core/cli/root-help.d.mts +28 -0
- package/dist/core/cli/root-help.mjs +7 -0
- package/dist/core/cli/root-surface.d.mts +42 -0
- package/dist/core/cli/root-surface.mjs +1 -0
- package/dist/core/cli/runtime-preflight.d.mts +149 -0
- package/dist/core/cli/runtime-preflight.mjs +1 -0
- package/dist/core/completion/index.d.mts +46 -0
- package/dist/core/completion/index.mjs +1 -0
- package/dist/core/completion/shells/bash.d.mts +24 -0
- package/dist/core/completion/shells/bash.mjs +3 -0
- package/dist/core/completion/shells/fish.d.mts +20 -0
- package/dist/core/completion/shells/fish.mjs +2 -0
- package/dist/core/completion/shells/powershell.d.mts +14 -0
- package/dist/core/completion/shells/powershell.mjs +2 -0
- package/dist/core/completion/shells/shared.d.mts +139 -0
- package/dist/core/completion/shells/shared.mjs +1 -0
- package/dist/core/completion/shells/zsh.d.mts +24 -0
- package/dist/core/completion/shells/zsh.mjs +2 -0
- package/dist/core/config/index.d.mts +161 -0
- package/dist/core/config/index.mjs +1 -0
- package/dist/core/config/package-json.d.mts +68 -0
- package/dist/core/config/package-json.mjs +1 -0
- package/dist/core/errors/index.d.mts +126 -0
- package/dist/core/errors/index.mjs +1 -0
- package/dist/core/execution/index.d.mts +49 -0
- package/dist/core/execution/index.mjs +1 -0
- package/dist/core/help/index.d.mts +49 -0
- package/dist/core/help/index.mjs +8 -0
- package/dist/core/json-schema/index.d.mts +109 -0
- package/dist/core/json-schema/index.mjs +55 -0
- package/dist/core/json-schema/meta-descriptions.generated.d.mts +185 -0
- package/dist/core/json-schema/meta-descriptions.generated.mjs +28 -0
- package/dist/core/output/activity.d.mts +194 -0
- package/dist/core/output/activity.mjs +1 -0
- package/dist/core/output/contracts.d.mts +88 -0
- package/dist/core/output/contracts.mjs +1 -0
- package/dist/core/output/display-value.d.mts +19 -0
- package/dist/core/output/display-value.mjs +1 -0
- package/dist/core/output/index.d.mts +280 -0
- package/dist/core/output/index.mjs +2 -0
- package/dist/core/output/renderers.d.mts +46 -0
- package/dist/core/output/renderers.mjs +1 -0
- package/dist/core/output/writer.d.mts +40 -0
- package/dist/core/output/writer.mjs +1 -0
- package/dist/core/parse/index.d.mts +79 -0
- package/dist/core/parse/index.mjs +1 -0
- package/dist/core/prompt/index.d.mts +205 -0
- package/dist/core/prompt/index.mjs +6 -0
- package/dist/core/resolve/args.d.mts +8 -0
- package/dist/core/resolve/args.mjs +1 -0
- package/dist/core/resolve/coerce.d.mts +22 -0
- package/dist/core/resolve/coerce.mjs +1 -0
- package/dist/core/resolve/config.d.mts +10 -0
- package/dist/core/resolve/config.mjs +1 -0
- package/dist/core/resolve/contracts.d.mts +112 -0
- package/dist/core/resolve/errors.d.mts +11 -0
- package/dist/core/resolve/errors.mjs +2 -0
- package/dist/core/resolve/flags.d.mts +10 -0
- package/dist/core/resolve/flags.mjs +1 -0
- package/dist/core/resolve/index.d.mts +42 -0
- package/dist/core/resolve/index.mjs +1 -0
- package/dist/core/resolve/property.d.mts +47 -0
- package/dist/core/resolve/property.mjs +1 -0
- package/dist/core/schema/activity.d.mts +169 -0
- package/dist/core/schema/arg.d.mts +559 -0
- package/dist/core/schema/arg.mjs +1 -0
- package/dist/core/schema/command.d.mts +960 -0
- package/dist/core/schema/command.mjs +1 -0
- package/dist/core/schema/flag.d.mts +491 -0
- package/dist/core/schema/flag.mjs +1 -0
- package/dist/core/schema/index.d.mts +8 -0
- package/dist/core/schema/middleware.d.mts +122 -0
- package/dist/core/schema/middleware.mjs +1 -0
- package/dist/core/schema/prompt.d.mts +117 -0
- package/dist/core/schema/run.d.mts +171 -0
- package/dist/core/schema-dsl/runtime.d.mts +144 -0
- package/dist/core/schema-dsl/runtime.mjs +2 -0
- package/dist/core/schema-dsl/to-json-schema.d.mts +30 -0
- package/dist/core/schema-dsl/to-json-schema.mjs +1 -0
- package/dist/core/testkit/index.d.mts +54 -0
- package/dist/core/testkit/index.mjs +1 -0
- package/dist/index.d.mts +28 -920
- package/dist/index.mjs +1 -10
- package/dist/runtime/adapter.d.mts +212 -0
- package/dist/{adapter-B37d9oia.mjs → runtime/adapter.mjs} +1 -1
- package/dist/runtime/auto.d.mts +26 -0
- package/dist/runtime/auto.mjs +1 -0
- package/dist/runtime/bun.d.mts +28 -0
- package/dist/runtime/bun.mjs +1 -0
- package/dist/runtime/deno.d.mts +76 -0
- package/dist/runtime/deno.mjs +2 -0
- package/dist/runtime/detect.d.mts +86 -0
- package/dist/runtime/detect.mjs +1 -0
- package/dist/runtime/index.d.mts +7 -0
- package/dist/runtime/node.d.mts +65 -0
- package/dist/runtime/node.mjs +2 -0
- package/dist/runtime/paths.d.mts +31 -0
- package/dist/runtime/paths.mjs +1 -0
- package/dist/runtime/support.d.mts +42 -0
- package/dist/runtime/support.mjs +1 -0
- package/dist/runtime.d.mts +6 -252
- package/dist/runtime.mjs +1 -1
- package/dist/testkit.d.mts +5 -24
- package/dist/testkit.mjs +1 -1
- package/dist/version.d.mts +16 -0
- package/dist/version.mjs +1 -0
- package/dreamcli.schema.json +338 -0
- package/examples/basic.ts +2 -2
- package/examples/gh/package.json +7 -11
- package/examples/gh/src/commands/auth.ts +2 -2
- package/examples/gh/src/commands/issue.ts +4 -4
- package/examples/gh/src/commands/pr.ts +4 -4
- package/examples/gh/src/docs.ts +11 -0
- package/examples/gh/src/lib/auth.ts +1 -1
- package/examples/gh/src/main.test.ts +6 -5
- package/examples/gh/src/main.ts +8 -6
- package/examples/interactive.ts +3 -3
- package/examples/json-mode.ts +4 -6
- package/examples/middleware.ts +3 -3
- package/examples/multi-command.ts +3 -3
- package/examples/spinner-progress.ts +5 -5
- package/examples/testing.ts +8 -6
- package/package.json +70 -44
- package/src/core/cli/dispatch.ts +4 -0
- package/src/core/cli/index.ts +185 -632
- package/src/core/cli/planner.ts +413 -0
- package/src/core/cli/plugin.ts +7 -1
- package/src/core/cli/propagate.ts +1 -1
- package/src/core/cli/runtime-preflight.ts +409 -0
- package/src/core/completion/index.ts +15 -9
- package/src/core/completion/shells/bash.ts +6 -3
- package/src/core/completion/shells/fish.ts +245 -10
- package/src/core/completion/shells/powershell.ts +390 -9
- package/src/core/completion/shells/shared.ts +4 -4
- package/src/core/completion/shells/zsh.ts +12 -7
- package/src/core/config/index.ts +7 -4
- package/src/core/config/package-json.ts +6 -2
- package/src/core/errors/index.ts +23 -8
- package/src/core/execution/index.ts +270 -0
- package/src/core/help/index.ts +13 -15
- package/src/core/json-schema/index.ts +193 -7
- package/src/core/json-schema/meta-descriptions.generated.ts +202 -0
- package/src/core/output/contracts.ts +194 -0
- package/src/core/output/index.ts +47 -82
- package/src/core/output/renderers.ts +125 -0
- package/src/core/parse/index.ts +39 -21
- package/src/core/prompt/index.ts +44 -27
- package/src/core/resolve/args.ts +144 -0
- package/src/core/resolve/coerce.ts +432 -0
- package/src/core/resolve/config.ts +26 -0
- package/src/core/resolve/contracts.ts +135 -0
- package/src/core/resolve/errors.ts +227 -0
- package/src/core/resolve/flags.ts +205 -0
- package/src/core/resolve/index.ts +41 -1264
- package/src/core/resolve/property.ts +99 -0
- package/src/core/schema/activity.ts +3 -0
- package/src/core/schema/arg.ts +26 -14
- package/src/core/schema/command.ts +265 -38
- package/src/core/schema/flag.ts +120 -15
- package/src/core/schema/index.ts +11 -2
- package/src/core/schema/middleware.ts +6 -6
- package/src/core/schema/prompt.ts +9 -1
- package/src/core/schema/run.ts +25 -17
- package/src/core/schema-dsl/index.ts +109 -0
- package/src/core/schema-dsl/parse.ts +262 -0
- package/src/core/schema-dsl/runtime.ts +609 -0
- package/src/core/schema-dsl/to-json-schema.ts +102 -0
- package/src/core/testkit/index.ts +55 -297
- package/src/index.ts +10 -4
- package/src/runtime/adapter.ts +13 -10
- package/src/runtime/auto.ts +3 -3
- package/src/runtime/bun.ts +4 -4
- package/src/runtime/deno.ts +43 -29
- package/src/runtime/detect.ts +4 -1
- package/src/runtime/index.ts +1 -1
- package/src/runtime/node.ts +43 -21
- package/src/runtime/paths.ts +9 -3
- package/src/runtime/support.ts +20 -4
- package/src/runtime.ts +1 -1
- package/src/testkit.ts +10 -8
- package/dist/adapter-CCSh5juX.cjs +0 -1
- package/dist/adapter-DIwO-q7K.d.mts +0 -3447
- package/dist/adapter-DQnxxLiE.d.cts +0 -3447
- package/dist/auto-5cjZaF9V.mjs +0 -3
- package/dist/auto-B0eSgoDw.cjs +0 -3
- package/dist/index.cjs +0 -10
- package/dist/index.d.cts +0 -920
- package/dist/runtime.cjs +0 -1
- package/dist/runtime.d.cts +0 -253
- package/dist/testkit-CEDYPvhF.mjs +0 -14
- package/dist/testkit-ClIKijxC.cjs +0 -14
- package/dist/testkit.cjs +0 -1
- package/dist/testkit.d.cts +0 -25
- package/examples/gh/tsconfig.json +0 -37
- package/examples/tsconfig.json +0 -17
- package/src/core/cli/AGENTS.md +0 -72
- package/src/core/completion/AGENTS.md +0 -72
- package/src/core/completion/completion-test-helpers.ts +0 -48
- package/src/core/output/AGENTS.md +0 -69
- package/src/core/resolve/AGENTS.md +0 -69
- package/src/core/schema/AGENTS.md +0 -61
- package/src/core/testkit/AGENTS.md +0 -55
- package/src/runtime/AGENTS.md +0 -74
- package/src/runtime/test-helpers.ts +0 -40
package/src/core/output/index.ts
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
* raw `console.log`. The channel respects TTY vs piped contexts and
|
|
6
6
|
* supports verbosity levels (quiet mode suppresses `info`).
|
|
7
7
|
*
|
|
8
|
-
* The
|
|
8
|
+
* The {@linkcode Out} interface (the contract handlers depend on) lives in
|
|
9
9
|
* `src/core/schema/command.ts`. This module provides the implementation.
|
|
10
10
|
*
|
|
11
11
|
* @module dreamcli/core/output
|
|
@@ -31,20 +31,25 @@ import {
|
|
|
31
31
|
TTYProgressHandle,
|
|
32
32
|
TTYSpinnerHandle,
|
|
33
33
|
} from './activity.ts';
|
|
34
|
+
import type { OutputPolicy, Verbosity } from './contracts.ts';
|
|
35
|
+
import {
|
|
36
|
+
resolveOutputPolicy,
|
|
37
|
+
resolveProgressPolicy,
|
|
38
|
+
resolveSpinnerPolicy,
|
|
39
|
+
resolveTableFormat,
|
|
40
|
+
resolveTableTextStream,
|
|
41
|
+
resolveTextStream,
|
|
42
|
+
shouldEmitInfo,
|
|
43
|
+
} from './contracts.ts';
|
|
34
44
|
import { formatDisplayValue } from './display-value.ts';
|
|
45
|
+
import {
|
|
46
|
+
createProgressHandleFromPolicy,
|
|
47
|
+
createSpinnerHandleFromPolicy,
|
|
48
|
+
resolveWriterForStream,
|
|
49
|
+
} from './renderers.ts';
|
|
35
50
|
import type { WriteFn } from './writer.ts';
|
|
36
51
|
import { writeLine } from './writer.ts';
|
|
37
52
|
|
|
38
|
-
// --- Verbosity
|
|
39
|
-
|
|
40
|
-
/**
|
|
41
|
-
* Controls which messages are emitted.
|
|
42
|
-
*
|
|
43
|
-
* - `'normal'` — all messages
|
|
44
|
-
* - `'quiet'` — suppresses `info`; keeps `log`, `warn`, `error`
|
|
45
|
-
*/
|
|
46
|
-
type Verbosity = 'normal' | 'quiet';
|
|
47
|
-
|
|
48
53
|
// --- Options
|
|
49
54
|
|
|
50
55
|
/**
|
|
@@ -124,13 +129,13 @@ function resolveOptions(options?: OutputOptions): ResolvedOutputOptions {
|
|
|
124
129
|
// --- OutputChannel — the concrete Out implementation
|
|
125
130
|
|
|
126
131
|
/**
|
|
127
|
-
* Concrete implementation of the
|
|
132
|
+
* Concrete implementation of the {@linkcode Out} interface.
|
|
128
133
|
*
|
|
129
134
|
* Routes messages to the appropriate writer (stdout vs stderr) and
|
|
130
135
|
* respects the configured verbosity level.
|
|
131
136
|
*
|
|
132
|
-
* Handlers interact with this via the
|
|
133
|
-
*
|
|
137
|
+
* Handlers interact with this via the {@linkcode Out} interface — they never see
|
|
138
|
+
* {@linkcode OutputChannel} directly, which keeps the coupling minimal.
|
|
134
139
|
* Prefer {@link createOutput} unless you are extending the output layer.
|
|
135
140
|
*
|
|
136
141
|
* @internal
|
|
@@ -139,6 +144,9 @@ class OutputChannel implements Out {
|
|
|
139
144
|
/** @internal Resolved configuration. */
|
|
140
145
|
readonly options: ResolvedOutputOptions;
|
|
141
146
|
|
|
147
|
+
/** Semantic output policy snapshot shared by routing helpers. */
|
|
148
|
+
readonly policy: OutputPolicy;
|
|
149
|
+
|
|
142
150
|
/** Whether JSON output mode is active. */
|
|
143
151
|
readonly jsonMode: boolean;
|
|
144
152
|
|
|
@@ -153,6 +161,7 @@ class OutputChannel implements Out {
|
|
|
153
161
|
|
|
154
162
|
constructor(options: ResolvedOutputOptions) {
|
|
155
163
|
this.options = options;
|
|
164
|
+
this.policy = resolveOutputPolicy(options);
|
|
156
165
|
this.jsonMode = options.jsonMode;
|
|
157
166
|
this.isTTY = options.isTTY;
|
|
158
167
|
}
|
|
@@ -164,7 +173,7 @@ class OutputChannel implements Out {
|
|
|
164
173
|
* structured `json()` output only.
|
|
165
174
|
*/
|
|
166
175
|
log(message: string): void {
|
|
167
|
-
const writer = this.options
|
|
176
|
+
const writer = resolveWriterForStream(this.options, resolveTextStream(this.policy));
|
|
168
177
|
writeLine(writer, message);
|
|
169
178
|
}
|
|
170
179
|
|
|
@@ -175,8 +184,8 @@ class OutputChannel implements Out {
|
|
|
175
184
|
* In JSON mode, redirected to stderr.
|
|
176
185
|
*/
|
|
177
186
|
info(message: string): void {
|
|
178
|
-
if (this.
|
|
179
|
-
const writer = this.options
|
|
187
|
+
if (!shouldEmitInfo(this.policy)) return;
|
|
188
|
+
const writer = resolveWriterForStream(this.options, resolveTextStream(this.policy));
|
|
180
189
|
writeLine(writer, message);
|
|
181
190
|
}
|
|
182
191
|
|
|
@@ -222,7 +231,7 @@ class OutputChannel implements Out {
|
|
|
222
231
|
options?: TableOptions,
|
|
223
232
|
): void {
|
|
224
233
|
const { columns, options: tableOptions } = resolveTableArgs(columnsOrOptions, options);
|
|
225
|
-
const format = resolveTableFormat(this.
|
|
234
|
+
const format = resolveTableFormat(this.policy, tableOptions);
|
|
226
235
|
const resolved = columns ?? inferColumns(rows);
|
|
227
236
|
|
|
228
237
|
if (format === 'json') {
|
|
@@ -233,7 +242,7 @@ class OutputChannel implements Out {
|
|
|
233
242
|
const text = formatTable(rows, resolved);
|
|
234
243
|
if (text.length === 0) return;
|
|
235
244
|
|
|
236
|
-
writeLine(resolveTextTableWriter(this.options, format, tableOptions), text);
|
|
245
|
+
writeLine(resolveTextTableWriter(this.options, this.policy, format, tableOptions), text);
|
|
237
246
|
}
|
|
238
247
|
|
|
239
248
|
// ----- Active handle tracking -----
|
|
@@ -285,31 +294,17 @@ class OutputChannel implements Out {
|
|
|
285
294
|
* @virtual
|
|
286
295
|
*/
|
|
287
296
|
spinner(text: string, options?: SpinnerOptions): SpinnerHandle {
|
|
288
|
-
const
|
|
297
|
+
const policy = resolveSpinnerPolicy(this.policy, options?.fallback ?? 'silent');
|
|
298
|
+
const rendered = createSpinnerHandleFromPolicy(policy, text, this.options.stderr);
|
|
289
299
|
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
return noopSpinnerHandle;
|
|
293
|
-
}
|
|
294
|
-
|
|
295
|
-
// Non-TTY, silent fallback: noop.
|
|
296
|
-
if (!this.options.isTTY && fallback === 'silent') {
|
|
297
|
-
return noopSpinnerHandle;
|
|
300
|
+
if (rendered.cleanup === undefined) {
|
|
301
|
+
return rendered.handle;
|
|
298
302
|
}
|
|
299
303
|
|
|
300
304
|
// Stop any active handle before creating a new one.
|
|
301
305
|
this.stopActive();
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
const handle = new TTYSpinnerHandle(text, this.options.stderr);
|
|
305
|
-
this.activeCleanup = () => handle.stop();
|
|
306
|
-
return handle;
|
|
307
|
-
}
|
|
308
|
-
|
|
309
|
-
// Non-TTY, static fallback.
|
|
310
|
-
const handle = new StaticSpinnerHandle(text, this.options.stderr);
|
|
311
|
-
this.activeCleanup = () => handle.stop();
|
|
312
|
-
return handle;
|
|
306
|
+
this.activeCleanup = rendered.cleanup;
|
|
307
|
+
return rendered.handle;
|
|
313
308
|
}
|
|
314
309
|
|
|
315
310
|
/**
|
|
@@ -330,31 +325,17 @@ class OutputChannel implements Out {
|
|
|
330
325
|
* @virtual
|
|
331
326
|
*/
|
|
332
327
|
progress(opts: ProgressOptions): ProgressHandle {
|
|
333
|
-
const
|
|
334
|
-
|
|
335
|
-
// JSON mode: always suppress activity indicators.
|
|
336
|
-
if (this.options.jsonMode) {
|
|
337
|
-
return noopProgressHandle;
|
|
338
|
-
}
|
|
328
|
+
const policy = resolveProgressPolicy(this.policy, opts.fallback ?? 'silent');
|
|
329
|
+
const rendered = createProgressHandleFromPolicy(policy, opts, this.options.stderr);
|
|
339
330
|
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
return noopProgressHandle;
|
|
331
|
+
if (rendered.cleanup === undefined) {
|
|
332
|
+
return rendered.handle;
|
|
343
333
|
}
|
|
344
334
|
|
|
345
335
|
// Stop any active handle before creating a new one.
|
|
346
336
|
this.stopActive();
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
const handle = new TTYProgressHandle(opts, this.options.stderr);
|
|
350
|
-
this.activeCleanup = () => handle.done();
|
|
351
|
-
return handle;
|
|
352
|
-
}
|
|
353
|
-
|
|
354
|
-
// Non-TTY, static fallback.
|
|
355
|
-
const handle = new StaticProgressHandle(opts.label, this.options.stderr);
|
|
356
|
-
this.activeCleanup = () => handle.done();
|
|
357
|
-
return handle;
|
|
337
|
+
this.activeCleanup = rendered.cleanup;
|
|
338
|
+
return rendered.handle;
|
|
358
339
|
}
|
|
359
340
|
}
|
|
360
341
|
|
|
@@ -363,8 +344,8 @@ class OutputChannel implements Out {
|
|
|
363
344
|
/**
|
|
364
345
|
* Output channel variant that returns capture handles for spinner/progress.
|
|
365
346
|
*
|
|
366
|
-
* Extends
|
|
367
|
-
* activity events into a shared
|
|
347
|
+
* Extends {@linkcode OutputChannel} to override `spinner()` and `progress()`, routing
|
|
348
|
+
* activity events into a shared {@linkcode ActivityEvent | ActivityEvent[]} for testkit assertion.
|
|
368
349
|
* Text output (log/info/warn/error) is handled by the parent class.
|
|
369
350
|
*
|
|
370
351
|
* @internal
|
|
@@ -477,23 +458,11 @@ function projectTableRows<T extends Record<string, unknown>>(
|
|
|
477
458
|
* @param options - Per-call table options (may specify an explicit format).
|
|
478
459
|
* @returns `'text'` or `'json'`.
|
|
479
460
|
*/
|
|
480
|
-
function resolveTableFormat(jsonMode: boolean, options?: TableOptions): 'text' | 'json' {
|
|
481
|
-
switch (options?.format) {
|
|
482
|
-
case 'json':
|
|
483
|
-
return 'json';
|
|
484
|
-
case 'text':
|
|
485
|
-
return 'text';
|
|
486
|
-
case 'auto':
|
|
487
|
-
case undefined:
|
|
488
|
-
return jsonMode ? 'json' : 'text';
|
|
489
|
-
}
|
|
490
|
-
}
|
|
491
|
-
|
|
492
461
|
/**
|
|
493
462
|
* Pick the {@link WriteFn} for a text-mode table.
|
|
494
463
|
*
|
|
495
464
|
* Respects `tableOptions.stream` when present; otherwise falls back to
|
|
496
|
-
* the channel's default
|
|
465
|
+
* the channel's default policy-owned text stream.
|
|
497
466
|
*
|
|
498
467
|
* @param options - Resolved channel options.
|
|
499
468
|
* @param format - Resolved table format.
|
|
@@ -502,19 +471,15 @@ function resolveTableFormat(jsonMode: boolean, options?: TableOptions): 'text' |
|
|
|
502
471
|
*/
|
|
503
472
|
function resolveTextTableWriter(
|
|
504
473
|
options: ResolvedOutputOptions,
|
|
474
|
+
policy: OutputPolicy,
|
|
505
475
|
format: 'text' | 'json',
|
|
506
476
|
tableOptions?: TableOptions,
|
|
507
477
|
): WriteFn {
|
|
508
|
-
if (
|
|
509
|
-
|
|
510
|
-
tableOptions !== undefined &&
|
|
511
|
-
'stream' in tableOptions &&
|
|
512
|
-
tableOptions.stream !== undefined
|
|
513
|
-
) {
|
|
514
|
-
return tableOptions.stream === 'stderr' ? options.stderr : options.stdout;
|
|
478
|
+
if (format === 'json') {
|
|
479
|
+
return options.stdout;
|
|
515
480
|
}
|
|
516
481
|
|
|
517
|
-
return options
|
|
482
|
+
return resolveWriterForStream(options, resolveTableTextStream(policy, tableOptions));
|
|
518
483
|
}
|
|
519
484
|
|
|
520
485
|
/**
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Internal output renderer factories.
|
|
3
|
+
*
|
|
4
|
+
* Keeps concrete writer and activity-handle construction separate from the
|
|
5
|
+
* semantic output-policy layer.
|
|
6
|
+
*
|
|
7
|
+
* @module dreamcli/core/output/renderers
|
|
8
|
+
* @internal
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import type {
|
|
12
|
+
ProgressHandle,
|
|
13
|
+
ProgressOptions,
|
|
14
|
+
SpinnerHandle,
|
|
15
|
+
} from '#internals/core/schema/activity.ts';
|
|
16
|
+
import {
|
|
17
|
+
noopProgressHandle,
|
|
18
|
+
noopSpinnerHandle,
|
|
19
|
+
StaticProgressHandle,
|
|
20
|
+
StaticSpinnerHandle,
|
|
21
|
+
TTYProgressHandle,
|
|
22
|
+
TTYSpinnerHandle,
|
|
23
|
+
} from './activity.ts';
|
|
24
|
+
import type { ActivityPolicy, OutputStream } from './contracts.ts';
|
|
25
|
+
import type { WriteFn } from './writer.ts';
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Paired write functions for the two standard output streams.
|
|
29
|
+
*
|
|
30
|
+
* @internal
|
|
31
|
+
*/
|
|
32
|
+
interface OutputWriters {
|
|
33
|
+
/** Write function targeting standard output. */
|
|
34
|
+
readonly stdout: WriteFn;
|
|
35
|
+
/** Write function targeting standard error. */
|
|
36
|
+
readonly stderr: WriteFn;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Spinner handle paired with its optional teardown function.
|
|
41
|
+
*
|
|
42
|
+
* @internal
|
|
43
|
+
*/
|
|
44
|
+
interface SpinnerRenderResult {
|
|
45
|
+
/** The spinner handle exposed to command handlers. */
|
|
46
|
+
readonly handle: SpinnerHandle;
|
|
47
|
+
/** Defined only when the mode requires cleanup (TTY or static); `undefined` for noop. */
|
|
48
|
+
readonly cleanup: (() => void) | undefined;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Progress handle paired with its optional teardown function.
|
|
53
|
+
*
|
|
54
|
+
* @internal
|
|
55
|
+
*/
|
|
56
|
+
interface ProgressRenderResult {
|
|
57
|
+
/** The progress handle exposed to command handlers. */
|
|
58
|
+
readonly handle: ProgressHandle;
|
|
59
|
+
/** Defined only when the mode requires cleanup (TTY or static); `undefined` for noop. */
|
|
60
|
+
readonly cleanup: (() => void) | undefined;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/** Select the concrete {@linkcode WriteFn} for a given stream label. @internal */
|
|
64
|
+
function resolveWriterForStream(writers: OutputWriters, stream: OutputStream): WriteFn {
|
|
65
|
+
return stream === 'stderr' ? writers.stderr : writers.stdout;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/** Create a spinner handle matching the resolved {@linkcode ActivityPolicy} mode. @internal */
|
|
69
|
+
function createSpinnerHandleFromPolicy(
|
|
70
|
+
policy: ActivityPolicy,
|
|
71
|
+
text: string,
|
|
72
|
+
stderr: WriteFn,
|
|
73
|
+
): SpinnerRenderResult {
|
|
74
|
+
if (policy.mode === 'noop') {
|
|
75
|
+
return {
|
|
76
|
+
handle: noopSpinnerHandle,
|
|
77
|
+
cleanup: undefined,
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
if (policy.mode === 'tty') {
|
|
82
|
+
const handle = new TTYSpinnerHandle(text, stderr);
|
|
83
|
+
return {
|
|
84
|
+
handle,
|
|
85
|
+
cleanup: () => handle.stop(),
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
const handle = new StaticSpinnerHandle(text, stderr);
|
|
90
|
+
return {
|
|
91
|
+
handle,
|
|
92
|
+
cleanup: () => handle.stop(),
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/** Create a progress handle matching the resolved {@linkcode ActivityPolicy} mode. @internal */
|
|
97
|
+
function createProgressHandleFromPolicy(
|
|
98
|
+
policy: ActivityPolicy,
|
|
99
|
+
options: ProgressOptions,
|
|
100
|
+
stderr: WriteFn,
|
|
101
|
+
): ProgressRenderResult {
|
|
102
|
+
if (policy.mode === 'noop') {
|
|
103
|
+
return {
|
|
104
|
+
handle: noopProgressHandle,
|
|
105
|
+
cleanup: undefined,
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
if (policy.mode === 'tty') {
|
|
110
|
+
const handle = new TTYProgressHandle(options, stderr);
|
|
111
|
+
return {
|
|
112
|
+
handle,
|
|
113
|
+
cleanup: () => handle.done(),
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
const handle = new StaticProgressHandle(options.label, stderr);
|
|
118
|
+
return {
|
|
119
|
+
handle,
|
|
120
|
+
cleanup: () => handle.done(),
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
export type { OutputWriters, ProgressRenderResult, SpinnerRenderResult };
|
|
125
|
+
export { createProgressHandleFromPolicy, createSpinnerHandleFromPolicy, resolveWriterForStream };
|
package/src/core/parse/index.ts
CHANGED
|
@@ -6,14 +6,15 @@
|
|
|
6
6
|
* 1. **Tokenize** — split raw argv into structured tokens (long flags, short
|
|
7
7
|
* flags, positionals, separator). Schema-agnostic.
|
|
8
8
|
*
|
|
9
|
-
* 2. **Parse** — walk tokens against a
|
|
10
|
-
* values. Emits
|
|
9
|
+
* 2. **Parse** — walk tokens against a {@linkcode CommandSchema} to produce typed raw
|
|
10
|
+
* values. Emits {@linkcode ParseError} for unknown flags, type mismatches, missing
|
|
11
11
|
* values.
|
|
12
12
|
*
|
|
13
13
|
* @module dreamcli/core/parse
|
|
14
14
|
*/
|
|
15
15
|
|
|
16
16
|
import { ParseError } from '#internals/core/errors/index.ts';
|
|
17
|
+
import { getFlagAliasNames } from '#internals/core/schema/flag.ts';
|
|
17
18
|
import type {
|
|
18
19
|
ArgSchema,
|
|
19
20
|
CommandArgEntry,
|
|
@@ -131,7 +132,7 @@ function buildFlagLookup(
|
|
|
131
132
|
const lookup = new Map<string, readonly [string, FlagSchema]>();
|
|
132
133
|
for (const [name, schema] of Object.entries(flags)) {
|
|
133
134
|
lookup.set(name, [name, schema]);
|
|
134
|
-
for (const alias of schema
|
|
135
|
+
for (const alias of getFlagAliasNames(schema, { includeHidden: true })) {
|
|
135
136
|
lookup.set(alias, [name, schema]);
|
|
136
137
|
}
|
|
137
138
|
}
|
|
@@ -159,7 +160,12 @@ function flagExpectsValue(schema: FlagSchema): boolean {
|
|
|
159
160
|
* @returns Coerced value matching the schema's kind
|
|
160
161
|
* @throws ParseError on type mismatch
|
|
161
162
|
*/
|
|
162
|
-
function coerceFlagValue(
|
|
163
|
+
function coerceFlagValue(
|
|
164
|
+
flagName: string,
|
|
165
|
+
raw: string,
|
|
166
|
+
schema: FlagSchema,
|
|
167
|
+
displayName = `--${flagName}`,
|
|
168
|
+
): unknown {
|
|
163
169
|
switch (schema.kind) {
|
|
164
170
|
case 'string':
|
|
165
171
|
return raw;
|
|
@@ -167,9 +173,9 @@ function coerceFlagValue(flagName: string, raw: string, schema: FlagSchema): unk
|
|
|
167
173
|
case 'number': {
|
|
168
174
|
const n = Number(raw);
|
|
169
175
|
if (Number.isNaN(n)) {
|
|
170
|
-
throw new ParseError(`Invalid number value '${raw}' for flag
|
|
176
|
+
throw new ParseError(`Invalid number value '${raw}' for flag ${displayName}`, {
|
|
171
177
|
code: 'INVALID_VALUE',
|
|
172
|
-
details: { flag: flagName, value: raw, expected: 'number' },
|
|
178
|
+
details: { flag: flagName, input: displayName, value: raw, expected: 'number' },
|
|
173
179
|
});
|
|
174
180
|
}
|
|
175
181
|
return n;
|
|
@@ -180,10 +186,10 @@ function coerceFlagValue(flagName: string, raw: string, schema: FlagSchema): unk
|
|
|
180
186
|
if (raw === 'true' || raw === '1') return true;
|
|
181
187
|
if (raw === 'false' || raw === '0') return false;
|
|
182
188
|
throw new ParseError(
|
|
183
|
-
`Invalid boolean value '${raw}' for flag
|
|
189
|
+
`Invalid boolean value '${raw}' for flag ${displayName}. Use true/false or 1/0`,
|
|
184
190
|
{
|
|
185
191
|
code: 'INVALID_VALUE',
|
|
186
|
-
details: { flag: flagName, value: raw, expected: 'boolean' },
|
|
192
|
+
details: { flag: flagName, input: displayName, value: raw, expected: 'boolean' },
|
|
187
193
|
},
|
|
188
194
|
);
|
|
189
195
|
|
|
@@ -200,10 +206,10 @@ function coerceFlagValue(flagName: string, raw: string, schema: FlagSchema): unk
|
|
|
200
206
|
}
|
|
201
207
|
if (!allowed.includes(raw)) {
|
|
202
208
|
throw new ParseError(
|
|
203
|
-
`Invalid value '${raw}' for flag
|
|
209
|
+
`Invalid value '${raw}' for flag ${displayName}. Allowed: ${allowed.join(', ')}`,
|
|
204
210
|
{
|
|
205
211
|
code: 'INVALID_VALUE',
|
|
206
|
-
details: { flag: flagName, value: raw, allowed },
|
|
212
|
+
details: { flag: flagName, input: displayName, value: raw, allowed },
|
|
207
213
|
},
|
|
208
214
|
);
|
|
209
215
|
}
|
|
@@ -226,9 +232,9 @@ function coerceFlagValue(flagName: string, raw: string, schema: FlagSchema): unk
|
|
|
226
232
|
} catch (err) {
|
|
227
233
|
if (err instanceof ParseError) throw err;
|
|
228
234
|
const message = err instanceof Error ? err.message : String(err);
|
|
229
|
-
throw new ParseError(`Failed to parse flag
|
|
235
|
+
throw new ParseError(`Failed to parse flag ${displayName}: ${message}`, {
|
|
230
236
|
code: 'INVALID_VALUE',
|
|
231
|
-
details: { flag: flagName, value: raw },
|
|
237
|
+
details: { flag: flagName, input: displayName, value: raw },
|
|
232
238
|
cause: err,
|
|
233
239
|
});
|
|
234
240
|
}
|
|
@@ -399,7 +405,12 @@ function parseLongFlag(
|
|
|
399
405
|
if (!flagExpectsValue(flagSchema)) {
|
|
400
406
|
// Boolean flag
|
|
401
407
|
if (token.value !== undefined) {
|
|
402
|
-
flags[canonicalName] = coerceFlagValue(
|
|
408
|
+
flags[canonicalName] = coerceFlagValue(
|
|
409
|
+
canonicalName,
|
|
410
|
+
token.value,
|
|
411
|
+
flagSchema,
|
|
412
|
+
`--${token.name}`,
|
|
413
|
+
);
|
|
403
414
|
} else {
|
|
404
415
|
flags[canonicalName] = true;
|
|
405
416
|
}
|
|
@@ -415,12 +426,12 @@ function parseLongFlag(
|
|
|
415
426
|
// --flag value (next token is the value)
|
|
416
427
|
const nextToken = tokens[startIdx + 1];
|
|
417
428
|
if (!nextToken || nextToken.kind !== 'positional') {
|
|
418
|
-
throw new ParseError(`Flag --${
|
|
429
|
+
throw new ParseError(`Flag --${token.name} requires a value`, {
|
|
419
430
|
code: 'MISSING_VALUE',
|
|
420
|
-
details: { flag: canonicalName, kind: flagSchema.kind },
|
|
431
|
+
details: { flag: canonicalName, input: token.name, kind: flagSchema.kind },
|
|
421
432
|
});
|
|
422
433
|
}
|
|
423
|
-
setFlagValue(flags, canonicalName, flagSchema, nextToken.value);
|
|
434
|
+
setFlagValue(flags, canonicalName, flagSchema, nextToken.value, `--${token.name}`);
|
|
424
435
|
return startIdx + 2;
|
|
425
436
|
}
|
|
426
437
|
|
|
@@ -468,7 +479,7 @@ function parseShortFlags(
|
|
|
468
479
|
// Value-expecting flag in the middle of combined shorts:
|
|
469
480
|
// -oFile → -o with value "File" (rest of chars is the value)
|
|
470
481
|
const inlineValue = chars.slice(ci + 1);
|
|
471
|
-
setFlagValue(flags, canonicalName, flagSchema, inlineValue);
|
|
482
|
+
setFlagValue(flags, canonicalName, flagSchema, inlineValue, `-${ch}`);
|
|
472
483
|
break; // consumed all remaining chars
|
|
473
484
|
}
|
|
474
485
|
|
|
@@ -477,10 +488,10 @@ function parseShortFlags(
|
|
|
477
488
|
if (!nextToken || nextToken.kind !== 'positional') {
|
|
478
489
|
throw new ParseError(`Flag -${ch} requires a value`, {
|
|
479
490
|
code: 'MISSING_VALUE',
|
|
480
|
-
details: { flag:
|
|
491
|
+
details: { flag: canonicalName, input: ch, kind: flagSchema.kind },
|
|
481
492
|
});
|
|
482
493
|
}
|
|
483
|
-
setFlagValue(flags, canonicalName, flagSchema, nextToken.value);
|
|
494
|
+
setFlagValue(flags, canonicalName, flagSchema, nextToken.value, `-${ch}`);
|
|
484
495
|
nextIdx++;
|
|
485
496
|
}
|
|
486
497
|
|
|
@@ -502,8 +513,9 @@ function setFlagValue(
|
|
|
502
513
|
name: string,
|
|
503
514
|
schema: FlagSchema,
|
|
504
515
|
rawValue: string,
|
|
516
|
+
displayName = `--${name}`,
|
|
505
517
|
): void {
|
|
506
|
-
const coerced = coerceFlagValue(name, rawValue, schema);
|
|
518
|
+
const coerced = coerceFlagValue(name, rawValue, schema, displayName);
|
|
507
519
|
|
|
508
520
|
if (schema.kind === 'array') {
|
|
509
521
|
const existing = flags[name];
|
|
@@ -584,8 +596,14 @@ function suggestFlag(
|
|
|
584
596
|
const threshold = Math.max(2, Math.floor(input.length / 2));
|
|
585
597
|
|
|
586
598
|
for (const key of lookup.keys()) {
|
|
587
|
-
|
|
599
|
+
const entry = lookup.get(key);
|
|
600
|
+
if (entry === undefined) continue;
|
|
601
|
+
const [canonicalName, schema] = entry;
|
|
602
|
+
const alias = schema.aliases.find((candidate) => candidate.name === key);
|
|
603
|
+
|
|
604
|
+
// Only suggest canonical names or visible long aliases.
|
|
588
605
|
if (key.length < 2) continue;
|
|
606
|
+
if (key !== canonicalName && alias?.hidden) continue;
|
|
589
607
|
|
|
590
608
|
const dist = levenshtein(input, key);
|
|
591
609
|
if (dist < bestDist && dist <= threshold) {
|