@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.
- package/CHANGELOG.md +153 -1
- package/README.md +9 -2
- package/dist/core/cli/dispatch.mjs +214 -1
- package/dist/core/cli/help-links.mjs +41 -1
- package/dist/core/cli/index.d.mts +109 -16
- package/dist/core/cli/index.mjs +901 -1
- package/dist/core/cli/planner.d.mts +27 -5
- package/dist/core/cli/planner.mjs +300 -1
- package/dist/core/cli/plugin.mjs +41 -1
- package/dist/core/cli/propagate.mjs +57 -1
- package/dist/core/cli/root-help.d.mts +16 -2
- package/dist/core/cli/root-help.mjs +208 -6
- package/dist/core/cli/root-surface.d.mts +18 -3
- package/dist/core/cli/root-surface.mjs +27 -1
- package/dist/core/cli/runtime-preflight.d.mts +16 -1
- package/dist/core/cli/runtime-preflight.mjs +168 -1
- package/dist/core/completion/index.d.mts +32 -1
- package/dist/core/completion/index.mjs +90 -1
- package/dist/core/completion/shells/bash.mjs +304 -3
- package/dist/core/completion/shells/fish.mjs +159 -2
- package/dist/core/completion/shells/powershell.mjs +353 -2
- package/dist/core/completion/shells/shared.d.mts +10 -0
- package/dist/core/completion/shells/shared.mjs +154 -1
- package/dist/core/completion/shells/zsh.mjs +244 -2
- package/dist/core/config/index.mjs +246 -1
- package/dist/core/config/package-json.mjs +385 -3
- package/dist/core/errors/index.mjs +87 -1
- package/dist/core/execution/index.mjs +189 -1
- package/dist/core/help/ansi.mjs +122 -2
- package/dist/core/help/index.d.mts +45 -1
- package/dist/core/help/index.mjs +329 -7
- package/dist/core/json-schema/index.mjs +487 -9
- package/dist/core/json-schema/meta-descriptions.generated.d.mts +38 -38
- package/dist/core/json-schema/meta-descriptions.generated.mjs +93 -28
- package/dist/core/output/activity.mjs +452 -1
- package/dist/core/output/bind.mjs +56 -1
- package/dist/core/output/contracts.mjs +57 -1
- package/dist/core/output/display-value.mjs +32 -1
- package/dist/core/output/index.d.mts +21 -0
- package/dist/core/output/index.mjs +442 -2
- package/dist/core/output/renderers.mjs +46 -1
- package/dist/core/output/writer.mjs +12 -1
- package/dist/core/parse/index.d.mts +15 -1
- package/dist/core/parse/index.mjs +555 -1
- package/dist/core/prompt/index.d.mts +11 -4
- package/dist/core/prompt/index.mjs +348 -6
- package/dist/core/resolve/args.mjs +105 -1
- package/dist/core/resolve/coerce.mjs +279 -1
- package/dist/core/resolve/config.mjs +19 -1
- package/dist/core/resolve/errors.mjs +140 -2
- package/dist/core/resolve/flags.mjs +196 -1
- package/dist/core/resolve/index.mjs +85 -1
- package/dist/core/resolve/property.d.mts +2 -1
- package/dist/core/resolve/property.mjs +45 -1
- package/dist/core/schema/arg.d.mts +90 -10
- package/dist/core/schema/arg.mjs +463 -1
- package/dist/core/schema/command.d.mts +21 -1
- package/dist/core/schema/command.mjs +712 -1
- package/dist/core/schema/flag.d.mts +92 -2
- package/dist/core/schema/flag.mjs +453 -1
- package/dist/core/schema/index.d.mts +2 -1
- package/dist/core/schema/middleware.mjs +41 -1
- package/dist/core/schema/number-constraints.d.mts +96 -0
- package/dist/core/schema/number-constraints.mjs +60 -0
- package/dist/core/schema/prompt.d.mts +15 -0
- package/dist/core/schema/run.d.mts +4 -4
- package/dist/core/schema-dsl/runtime.mjs +271 -2
- package/dist/core/schema-dsl/to-json-schema.mjs +74 -1
- package/dist/core/testkit/index.d.mts +9 -5
- package/dist/core/testkit/index.mjs +83 -1
- package/dist/index.d.mts +3 -1
- package/dist/index.mjs +22 -1
- package/dist/runtime/adapter.mjs +82 -1
- package/dist/runtime/auto.mjs +46 -1
- package/dist/runtime/bun.mjs +28 -1
- package/dist/runtime/deno.mjs +217 -2
- package/dist/runtime/detect.mjs +45 -1
- package/dist/runtime/node.mjs +169 -2
- package/dist/runtime/paths.mjs +48 -1
- package/dist/runtime.mjs +7 -1
- package/dist/strings.mjs +22 -1
- package/dist/testkit.mjs +5 -1
- package/dist/version.mjs +7 -1
- package/examples/basic.ts +5 -1
- package/examples/gh/package.json +2 -7
- package/examples/pwsh-demo/package.json +1 -7
- package/examples/pwsh-demo/src/main.test.ts +1 -4
- package/examples/pwsh-demo/src/main.ts +14 -4
- package/examples/transport-launcher.ts +85 -0
- package/package.json +23 -41
- package/dist/runtime/support.d.mts +0 -42
- package/dist/runtime/support.mjs +0 -1
- package/src/core/cli/dispatch.ts +0 -349
- package/src/core/cli/help-links.ts +0 -82
- package/src/core/cli/index.ts +0 -1413
- package/src/core/cli/planner.ts +0 -487
- package/src/core/cli/plugin.ts +0 -117
- package/src/core/cli/propagate.ts +0 -97
- package/src/core/cli/root-help.ts +0 -252
- package/src/core/cli/root-surface.ts +0 -74
- package/src/core/cli/runtime-preflight.ts +0 -438
- package/src/core/completion/index.ts +0 -88
- 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 -283
- 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 -268
- package/src/core/help/ansi.ts +0 -144
- package/src/core/help/index.ts +0 -516
- package/src/core/json-schema/index.ts +0 -891
- package/src/core/json-schema/meta-descriptions.generated.ts +0 -185
- 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 -681
- package/src/core/prompt/index.ts +0 -583
- package/src/core/resolve/args.ts +0 -144
- package/src/core/resolve/coerce.ts +0 -432
- 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 -255
- 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 -719
- package/src/core/schema/command.ts +0 -1498
- package/src/core/schema/flag.ts +0 -801
- package/src/core/schema/index.ts +0 -91
- package/src/core/schema/middleware.ts +0 -171
- package/src/core/schema/prompt.ts +0 -156
- 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 -123
- package/src/index.ts +0 -179
- package/src/runtime/adapter.ts +0 -303
- package/src/runtime/auto.ts +0 -110
- package/src/runtime/bun.ts +0 -55
- package/src/runtime/deno.ts +0 -369
- 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 -300
- package/src/runtime/paths.ts +0 -64
- package/src/runtime/support.ts +0 -135
- package/src/runtime.ts +0 -31
- package/src/strings.ts +0 -34
- package/src/testkit.ts +0 -25
- package/src/version.ts +0 -20
|
@@ -1,575 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Activity handle implementations — spinner and progress bar.
|
|
3
|
-
*
|
|
4
|
-
* Four tiers per activity type:
|
|
5
|
-
* - **Noop** — silent, all methods no-op (jsonMode / non-TTY silent)
|
|
6
|
-
* - **Static** — plain text at lifecycle boundaries (non-TTY, fallback='static')
|
|
7
|
-
* - **TTY** — animated ANSI rendering (braille spinner, bar with percentage)
|
|
8
|
-
* - **Capture** — records `ActivityEvent[]` for testkit assertion
|
|
9
|
-
*
|
|
10
|
-
* Extracted from `index.ts` to keep the main OutputChannel file focused on
|
|
11
|
-
* the `Out` implementation. Depends only on `writer.ts` (leaf) and
|
|
12
|
-
* `schema/command.ts` (types).
|
|
13
|
-
*
|
|
14
|
-
* @module dreamcli/core/output/activity
|
|
15
|
-
* @internal
|
|
16
|
-
*/
|
|
17
|
-
|
|
18
|
-
import type {
|
|
19
|
-
ActivityEvent,
|
|
20
|
-
ProgressHandle,
|
|
21
|
-
ProgressOptions,
|
|
22
|
-
SpinnerHandle,
|
|
23
|
-
} from '#internals/core/schema/activity.ts';
|
|
24
|
-
import { bindMethods } from './bind.ts';
|
|
25
|
-
import type { WriteFn } from './writer.ts';
|
|
26
|
-
import { writeLine } from './writer.ts';
|
|
27
|
-
|
|
28
|
-
// --- Timer globals — minimal declarations for setInterval/clearInterval.
|
|
29
|
-
//
|
|
30
|
-
// The project targets ES2022 without DOM or Node lib typings. Timer APIs
|
|
31
|
-
// are universally available across Node, Bun, Deno, and browsers, but the
|
|
32
|
-
// type checker doesn't know about them. Declaring the subset we need here
|
|
33
|
-
// avoids adding `@types/node` as a dependency.
|
|
34
|
-
|
|
35
|
-
declare function setInterval(callback: () => void, ms: number): unknown;
|
|
36
|
-
declare function clearInterval(handle: unknown): void;
|
|
37
|
-
|
|
38
|
-
// --- Noop activity handles — silent mode (non-TTY, fallback='silent')
|
|
39
|
-
|
|
40
|
-
/**
|
|
41
|
-
* Noop spinner handle singleton.
|
|
42
|
-
*
|
|
43
|
-
* All methods are no-ops. Used when spinners should produce no output
|
|
44
|
-
* at all — `jsonMode`, or non-TTY with `fallback: 'silent'` (default).
|
|
45
|
-
*
|
|
46
|
-
* @internal
|
|
47
|
-
*/
|
|
48
|
-
const noopSpinnerHandle: SpinnerHandle = {
|
|
49
|
-
update() {},
|
|
50
|
-
succeed() {},
|
|
51
|
-
fail() {},
|
|
52
|
-
stop() {},
|
|
53
|
-
wrap<T>(promise: Promise<T>): Promise<T> {
|
|
54
|
-
return promise;
|
|
55
|
-
},
|
|
56
|
-
};
|
|
57
|
-
|
|
58
|
-
/**
|
|
59
|
-
* Noop progress handle singleton.
|
|
60
|
-
*
|
|
61
|
-
* All methods are no-ops. Used when progress bars should produce no
|
|
62
|
-
* output at all — `jsonMode`, or non-TTY with `fallback: 'silent'`.
|
|
63
|
-
*
|
|
64
|
-
* @internal
|
|
65
|
-
*/
|
|
66
|
-
const noopProgressHandle: ProgressHandle = {
|
|
67
|
-
increment() {},
|
|
68
|
-
update() {},
|
|
69
|
-
done() {},
|
|
70
|
-
fail() {},
|
|
71
|
-
};
|
|
72
|
-
|
|
73
|
-
// --- Static activity handles — plain text mode (non-TTY, fallback='static')
|
|
74
|
-
|
|
75
|
-
/**
|
|
76
|
-
* Static spinner handle — emits plain text at lifecycle boundaries.
|
|
77
|
-
*
|
|
78
|
-
* Used in non-TTY environments with `fallback: 'static'`. No animation,
|
|
79
|
-
* no ANSI codes. Emits text on start and terminal events only.
|
|
80
|
-
*
|
|
81
|
-
* Terminal methods are idempotent — calling after stop is a no-op.
|
|
82
|
-
*
|
|
83
|
-
* @internal
|
|
84
|
-
*/
|
|
85
|
-
class StaticSpinnerHandle implements SpinnerHandle {
|
|
86
|
-
/** Whether the handle has been stopped (terminal state reached). */
|
|
87
|
-
private stopped = false;
|
|
88
|
-
|
|
89
|
-
constructor(
|
|
90
|
-
text: string,
|
|
91
|
-
private readonly write: WriteFn,
|
|
92
|
-
) {
|
|
93
|
-
writeLine(write, text);
|
|
94
|
-
bindMethods(this);
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
update(_text: string): void {
|
|
98
|
-
// Static mode — no update output (no terminal to overwrite).
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
succeed(text?: string): void {
|
|
102
|
-
if (this.stopped) return;
|
|
103
|
-
this.stopped = true;
|
|
104
|
-
if (text !== undefined) {
|
|
105
|
-
writeLine(this.write, text);
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
fail(text?: string): void {
|
|
110
|
-
if (this.stopped) return;
|
|
111
|
-
this.stopped = true;
|
|
112
|
-
if (text !== undefined) {
|
|
113
|
-
writeLine(this.write, text);
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
stop(): void {
|
|
118
|
-
if (this.stopped) return;
|
|
119
|
-
this.stopped = true;
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
async wrap<T>(
|
|
123
|
-
promise: Promise<T>,
|
|
124
|
-
options?: { readonly succeed?: string; readonly fail?: string },
|
|
125
|
-
): Promise<T> {
|
|
126
|
-
try {
|
|
127
|
-
const value = await promise;
|
|
128
|
-
this.succeed(options?.succeed);
|
|
129
|
-
return value;
|
|
130
|
-
} catch (error: unknown) {
|
|
131
|
-
this.fail(options?.fail);
|
|
132
|
-
throw error;
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
/**
|
|
138
|
-
* Static progress handle — emits plain text at lifecycle boundaries.
|
|
139
|
-
*
|
|
140
|
-
* Used in non-TTY environments with `fallback: 'static'`. No animation,
|
|
141
|
-
* no progress bar rendering. Emits label on start, text on done/fail.
|
|
142
|
-
*
|
|
143
|
-
* Terminal methods are idempotent — calling after stop is a no-op.
|
|
144
|
-
*
|
|
145
|
-
* @internal
|
|
146
|
-
*/
|
|
147
|
-
class StaticProgressHandle implements ProgressHandle {
|
|
148
|
-
/** Whether the handle has been stopped (terminal state reached). */
|
|
149
|
-
private stopped = false;
|
|
150
|
-
|
|
151
|
-
constructor(
|
|
152
|
-
label: string | undefined,
|
|
153
|
-
private readonly write: WriteFn,
|
|
154
|
-
) {
|
|
155
|
-
if (label !== undefined) {
|
|
156
|
-
writeLine(write, label);
|
|
157
|
-
}
|
|
158
|
-
bindMethods(this);
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
increment(_n?: number): void {
|
|
162
|
-
// Static mode — no incremental output.
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
update(_value: number): void {
|
|
166
|
-
// Static mode — no incremental output.
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
done(text?: string): void {
|
|
170
|
-
if (this.stopped) return;
|
|
171
|
-
this.stopped = true;
|
|
172
|
-
if (text !== undefined) {
|
|
173
|
-
writeLine(this.write, text);
|
|
174
|
-
}
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
fail(text?: string): void {
|
|
178
|
-
if (this.stopped) return;
|
|
179
|
-
this.stopped = true;
|
|
180
|
-
if (text !== undefined) {
|
|
181
|
-
writeLine(this.write, text);
|
|
182
|
-
}
|
|
183
|
-
}
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
// --- ANSI escape sequences
|
|
187
|
-
|
|
188
|
-
/** Hide the terminal cursor. @internal */
|
|
189
|
-
const HIDE_CURSOR = '\x1b[?25l';
|
|
190
|
-
|
|
191
|
-
/** Show the terminal cursor. @internal */
|
|
192
|
-
const SHOW_CURSOR = '\x1b[?25h';
|
|
193
|
-
|
|
194
|
-
/** Erase the entire current line. @internal */
|
|
195
|
-
const ERASE_LINE = '\x1b[2K';
|
|
196
|
-
|
|
197
|
-
/**
|
|
198
|
-
* Braille-dot spinner frames.
|
|
199
|
-
*
|
|
200
|
-
* Provides smooth animation across most terminal emulators. The 10-frame
|
|
201
|
-
* sequence completes a full rotation of the braille clock pattern.
|
|
202
|
-
*
|
|
203
|
-
* @internal
|
|
204
|
-
*/
|
|
205
|
-
const SPINNER_FRAMES = ['⠋', '⠙', '⠹', '⠸', '⠼', '⠴', '⠦', '⠧', '⠇', '⠏'] as const;
|
|
206
|
-
|
|
207
|
-
/** @internal */ const SPINNER_INTERVAL_MS = 80;
|
|
208
|
-
|
|
209
|
-
/** Success symbol for terminal output. @internal */
|
|
210
|
-
const CHECK = '✓';
|
|
211
|
-
|
|
212
|
-
/** Failure symbol for terminal output. @internal */
|
|
213
|
-
const CROSS = '✗';
|
|
214
|
-
|
|
215
|
-
/** Filled segment of a progress bar. @internal */
|
|
216
|
-
const BAR_FILLED = '█';
|
|
217
|
-
|
|
218
|
-
/** Empty segment of a progress bar. @internal */
|
|
219
|
-
const BAR_EMPTY = '░';
|
|
220
|
-
|
|
221
|
-
/** Width of the progress bar in characters. @internal */
|
|
222
|
-
const BAR_WIDTH = 20;
|
|
223
|
-
|
|
224
|
-
/** Pulse width for indeterminate progress (number of filled segments). @internal */
|
|
225
|
-
const PULSE_WIDTH = 3;
|
|
226
|
-
|
|
227
|
-
/** Animation interval for indeterminate progress pulse (ms). @internal */
|
|
228
|
-
const PULSE_INTERVAL_MS = 80;
|
|
229
|
-
|
|
230
|
-
// --- TTY activity handles — animated mode (isTTY && !jsonMode)
|
|
231
|
-
|
|
232
|
-
/**
|
|
233
|
-
* TTY spinner handle — animated braille frames with ANSI line overwrite.
|
|
234
|
-
*
|
|
235
|
-
* Used in TTY environments (non-JSON mode). Renders a spinning indicator
|
|
236
|
-
* with configurable text, using `\r` + erase-line to overwrite in place.
|
|
237
|
-
* Hides the cursor during animation and restores it on any terminal method.
|
|
238
|
-
*
|
|
239
|
-
* Terminal methods (`succeed`, `fail`, `stop`) are idempotent — calling any
|
|
240
|
-
* of them after the handle is already stopped is a no-op.
|
|
241
|
-
*
|
|
242
|
-
* **Note:** If the process exits abnormally (e.g. `SIGKILL`) while a spinner
|
|
243
|
-
* is active, the cursor may remain hidden. Use {@link SpinnerHandle.wrap} to
|
|
244
|
-
* ensure cleanup on both success and failure paths.
|
|
245
|
-
*
|
|
246
|
-
* @internal
|
|
247
|
-
*/
|
|
248
|
-
class TTYSpinnerHandle implements SpinnerHandle {
|
|
249
|
-
/** Whether the handle has been stopped (terminal state reached). */
|
|
250
|
-
private stopped = false;
|
|
251
|
-
/** Animation timer handle — cleared on any terminal method. */
|
|
252
|
-
private timer: unknown;
|
|
253
|
-
/** Current position in the braille frame sequence. */
|
|
254
|
-
private frameIndex = 0;
|
|
255
|
-
/** Current spinner text (mutable via `update()`). */
|
|
256
|
-
private text: string;
|
|
257
|
-
|
|
258
|
-
constructor(
|
|
259
|
-
text: string,
|
|
260
|
-
private readonly write: WriteFn,
|
|
261
|
-
) {
|
|
262
|
-
this.text = text;
|
|
263
|
-
write(HIDE_CURSOR);
|
|
264
|
-
this.render();
|
|
265
|
-
this.timer = setInterval(() => {
|
|
266
|
-
this.frameIndex = (this.frameIndex + 1) % SPINNER_FRAMES.length;
|
|
267
|
-
this.render();
|
|
268
|
-
}, SPINNER_INTERVAL_MS);
|
|
269
|
-
bindMethods(this);
|
|
270
|
-
}
|
|
271
|
-
|
|
272
|
-
/** Render the current frame + text, overwriting the current line. */
|
|
273
|
-
private render(): void {
|
|
274
|
-
const frame = SPINNER_FRAMES[this.frameIndex] ?? SPINNER_FRAMES[0];
|
|
275
|
-
this.write(`\r${ERASE_LINE}${frame} ${this.text}`);
|
|
276
|
-
}
|
|
277
|
-
|
|
278
|
-
/** Clear the animation timer, erase the line, and restore the cursor. */
|
|
279
|
-
private cleanup(): void {
|
|
280
|
-
if (this.timer !== undefined) {
|
|
281
|
-
clearInterval(this.timer);
|
|
282
|
-
this.timer = undefined;
|
|
283
|
-
}
|
|
284
|
-
this.write(`\r${ERASE_LINE}${SHOW_CURSOR}`);
|
|
285
|
-
}
|
|
286
|
-
|
|
287
|
-
update(text: string): void {
|
|
288
|
-
if (this.stopped) return;
|
|
289
|
-
this.text = text;
|
|
290
|
-
// Render immediately so the text change is visible before the next frame.
|
|
291
|
-
this.render();
|
|
292
|
-
}
|
|
293
|
-
|
|
294
|
-
succeed(text?: string): void {
|
|
295
|
-
if (this.stopped) return;
|
|
296
|
-
this.stopped = true;
|
|
297
|
-
this.cleanup();
|
|
298
|
-
if (text !== undefined) {
|
|
299
|
-
writeLine(this.write, `${CHECK} ${text}`);
|
|
300
|
-
}
|
|
301
|
-
}
|
|
302
|
-
|
|
303
|
-
fail(text?: string): void {
|
|
304
|
-
if (this.stopped) return;
|
|
305
|
-
this.stopped = true;
|
|
306
|
-
this.cleanup();
|
|
307
|
-
if (text !== undefined) {
|
|
308
|
-
writeLine(this.write, `${CROSS} ${text}`);
|
|
309
|
-
}
|
|
310
|
-
}
|
|
311
|
-
|
|
312
|
-
stop(): void {
|
|
313
|
-
if (this.stopped) return;
|
|
314
|
-
this.stopped = true;
|
|
315
|
-
this.cleanup();
|
|
316
|
-
}
|
|
317
|
-
|
|
318
|
-
async wrap<T>(
|
|
319
|
-
promise: Promise<T>,
|
|
320
|
-
options?: { readonly succeed?: string; readonly fail?: string },
|
|
321
|
-
): Promise<T> {
|
|
322
|
-
try {
|
|
323
|
-
const value = await promise;
|
|
324
|
-
this.succeed(options?.succeed);
|
|
325
|
-
return value;
|
|
326
|
-
} catch (error: unknown) {
|
|
327
|
-
this.fail(options?.fail);
|
|
328
|
-
throw error;
|
|
329
|
-
}
|
|
330
|
-
}
|
|
331
|
-
}
|
|
332
|
-
|
|
333
|
-
/**
|
|
334
|
-
* TTY progress handle — bar rendering with ANSI line overwrite.
|
|
335
|
-
*
|
|
336
|
-
* Supports two modes:
|
|
337
|
-
* - **Determinate** (`total` provided) — renders `[████░░░░░░] 40% label`,
|
|
338
|
-
* re-rendered on each `increment()` / `update()` call. No timer.
|
|
339
|
-
* - **Indeterminate** (`total` omitted) — renders a pulsing highlight that
|
|
340
|
-
* bounces across the bar via `setInterval`.
|
|
341
|
-
*
|
|
342
|
-
* Terminal methods (`done`, `fail`) are idempotent — calling any of them
|
|
343
|
-
* after the handle is already stopped is a no-op.
|
|
344
|
-
*
|
|
345
|
-
* @internal
|
|
346
|
-
*/
|
|
347
|
-
class TTYProgressHandle implements ProgressHandle {
|
|
348
|
-
/** Whether the handle has been stopped (terminal state reached). */
|
|
349
|
-
private stopped = false;
|
|
350
|
-
/** Animation timer for indeterminate mode — `undefined` in determinate mode. */
|
|
351
|
-
private timer: unknown;
|
|
352
|
-
/** Current progress value (units completed). */
|
|
353
|
-
private current = 0;
|
|
354
|
-
/** Total units of work (`undefined` = indeterminate mode). */
|
|
355
|
-
private readonly total: number | undefined;
|
|
356
|
-
/** Label displayed alongside the bar. */
|
|
357
|
-
private readonly label: string;
|
|
358
|
-
/** Pulse position for indeterminate animation (bouncing highlight). */
|
|
359
|
-
private pulsePos = 0;
|
|
360
|
-
/** Pulse direction: 1 = forward, -1 = backward (for bounce). */
|
|
361
|
-
private pulseDir: 1 | -1 = 1;
|
|
362
|
-
|
|
363
|
-
constructor(
|
|
364
|
-
opts: ProgressOptions,
|
|
365
|
-
private readonly write: WriteFn,
|
|
366
|
-
) {
|
|
367
|
-
this.total = opts.total;
|
|
368
|
-
this.label = opts.label ?? '';
|
|
369
|
-
write(HIDE_CURSOR);
|
|
370
|
-
this.render();
|
|
371
|
-
|
|
372
|
-
// Only start animation timer for indeterminate mode.
|
|
373
|
-
if (this.total === undefined) {
|
|
374
|
-
this.timer = setInterval(() => {
|
|
375
|
-
this.advancePulse();
|
|
376
|
-
this.render();
|
|
377
|
-
}, PULSE_INTERVAL_MS);
|
|
378
|
-
}
|
|
379
|
-
bindMethods(this);
|
|
380
|
-
}
|
|
381
|
-
|
|
382
|
-
/** Advance the indeterminate pulse position with bounce logic. */
|
|
383
|
-
private advancePulse(): void {
|
|
384
|
-
this.pulsePos += this.pulseDir;
|
|
385
|
-
// Bounce at boundaries: when the rightmost lit segment would exceed the bar.
|
|
386
|
-
if (this.pulsePos + PULSE_WIDTH > BAR_WIDTH) {
|
|
387
|
-
this.pulseDir = -1;
|
|
388
|
-
this.pulsePos = BAR_WIDTH - PULSE_WIDTH;
|
|
389
|
-
} else if (this.pulsePos < 0) {
|
|
390
|
-
this.pulseDir = 1;
|
|
391
|
-
this.pulsePos = 0;
|
|
392
|
-
}
|
|
393
|
-
}
|
|
394
|
-
|
|
395
|
-
/** Render the progress bar, overwriting the current line. */
|
|
396
|
-
private render(): void {
|
|
397
|
-
const bar =
|
|
398
|
-
this.total !== undefined ? this.renderDeterminate(this.total) : this.renderIndeterminate();
|
|
399
|
-
const suffix = this.label.length > 0 ? ` ${this.label}` : '';
|
|
400
|
-
this.write(`\r${ERASE_LINE}${bar}${suffix}`);
|
|
401
|
-
}
|
|
402
|
-
|
|
403
|
-
/** Render a determinate bar: `[████░░░░░░] 40%`. */
|
|
404
|
-
private renderDeterminate(total: number): string {
|
|
405
|
-
const ratio = total > 0 ? Math.max(0, Math.min(this.current / total, 1)) : 0;
|
|
406
|
-
const filled = Math.round(ratio * BAR_WIDTH);
|
|
407
|
-
const empty = BAR_WIDTH - filled;
|
|
408
|
-
const pct = Math.round(ratio * 100);
|
|
409
|
-
return `[${BAR_FILLED.repeat(filled)}${BAR_EMPTY.repeat(empty)}] ${String(pct)}%`;
|
|
410
|
-
}
|
|
411
|
-
|
|
412
|
-
/** Render an indeterminate bar: pulsing highlight bouncing across empty segments. */
|
|
413
|
-
private renderIndeterminate(): string {
|
|
414
|
-
let bar = '';
|
|
415
|
-
for (let i = 0; i < BAR_WIDTH; i++) {
|
|
416
|
-
bar += i >= this.pulsePos && i < this.pulsePos + PULSE_WIDTH ? BAR_FILLED : BAR_EMPTY;
|
|
417
|
-
}
|
|
418
|
-
return `[${bar}]`;
|
|
419
|
-
}
|
|
420
|
-
|
|
421
|
-
/** Clear the animation timer, erase the line, and restore the cursor. */
|
|
422
|
-
private cleanup(): void {
|
|
423
|
-
if (this.timer !== undefined) {
|
|
424
|
-
clearInterval(this.timer);
|
|
425
|
-
this.timer = undefined;
|
|
426
|
-
}
|
|
427
|
-
this.write(`\r${ERASE_LINE}${SHOW_CURSOR}`);
|
|
428
|
-
}
|
|
429
|
-
|
|
430
|
-
increment(n?: number): void {
|
|
431
|
-
if (this.stopped) return;
|
|
432
|
-
this.current += n ?? 1;
|
|
433
|
-
this.render();
|
|
434
|
-
}
|
|
435
|
-
|
|
436
|
-
update(value: number): void {
|
|
437
|
-
if (this.stopped) return;
|
|
438
|
-
this.current = value;
|
|
439
|
-
this.render();
|
|
440
|
-
}
|
|
441
|
-
|
|
442
|
-
done(text?: string): void {
|
|
443
|
-
if (this.stopped) return;
|
|
444
|
-
this.stopped = true;
|
|
445
|
-
this.cleanup();
|
|
446
|
-
if (text !== undefined) {
|
|
447
|
-
writeLine(this.write, `${CHECK} ${text}`);
|
|
448
|
-
}
|
|
449
|
-
}
|
|
450
|
-
|
|
451
|
-
fail(text?: string): void {
|
|
452
|
-
if (this.stopped) return;
|
|
453
|
-
this.stopped = true;
|
|
454
|
-
this.cleanup();
|
|
455
|
-
if (text !== undefined) {
|
|
456
|
-
writeLine(this.write, `${CROSS} ${text}`);
|
|
457
|
-
}
|
|
458
|
-
}
|
|
459
|
-
}
|
|
460
|
-
|
|
461
|
-
// --- Capture activity handles — testkit recording mode
|
|
462
|
-
|
|
463
|
-
/**
|
|
464
|
-
* Spinner handle that records lifecycle events to a shared `ActivityEvent[]`.
|
|
465
|
-
*
|
|
466
|
-
* Used by `createCaptureOutput()` so testkit can assert on spinner behaviour
|
|
467
|
-
* without polluting stdout/stderr arrays. Terminal methods are idempotent.
|
|
468
|
-
*
|
|
469
|
-
* @internal
|
|
470
|
-
*/
|
|
471
|
-
class CaptureSpinnerHandle implements SpinnerHandle {
|
|
472
|
-
/** Whether the handle has been stopped (terminal state reached). */
|
|
473
|
-
private stopped = false;
|
|
474
|
-
|
|
475
|
-
constructor(
|
|
476
|
-
text: string,
|
|
477
|
-
private readonly events: ActivityEvent[],
|
|
478
|
-
) {
|
|
479
|
-
events.push({ type: 'spinner:start', text });
|
|
480
|
-
bindMethods(this);
|
|
481
|
-
}
|
|
482
|
-
|
|
483
|
-
update(text: string): void {
|
|
484
|
-
if (this.stopped) return;
|
|
485
|
-
this.events.push({ type: 'spinner:update', text });
|
|
486
|
-
}
|
|
487
|
-
|
|
488
|
-
succeed(text?: string): void {
|
|
489
|
-
if (this.stopped) return;
|
|
490
|
-
this.stopped = true;
|
|
491
|
-
this.events.push({ type: 'spinner:succeed', text: text ?? '' });
|
|
492
|
-
}
|
|
493
|
-
|
|
494
|
-
fail(text?: string): void {
|
|
495
|
-
if (this.stopped) return;
|
|
496
|
-
this.stopped = true;
|
|
497
|
-
this.events.push({ type: 'spinner:fail', text: text ?? '' });
|
|
498
|
-
}
|
|
499
|
-
|
|
500
|
-
stop(): void {
|
|
501
|
-
if (this.stopped) return;
|
|
502
|
-
this.stopped = true;
|
|
503
|
-
this.events.push({ type: 'spinner:stop' });
|
|
504
|
-
}
|
|
505
|
-
|
|
506
|
-
async wrap<T>(
|
|
507
|
-
promise: Promise<T>,
|
|
508
|
-
options?: { readonly succeed?: string; readonly fail?: string },
|
|
509
|
-
): Promise<T> {
|
|
510
|
-
try {
|
|
511
|
-
const value = await promise;
|
|
512
|
-
this.succeed(options?.succeed);
|
|
513
|
-
return value;
|
|
514
|
-
} catch (error: unknown) {
|
|
515
|
-
this.fail(options?.fail);
|
|
516
|
-
throw error;
|
|
517
|
-
}
|
|
518
|
-
}
|
|
519
|
-
}
|
|
520
|
-
|
|
521
|
-
/**
|
|
522
|
-
* Progress handle that records lifecycle events to a shared `ActivityEvent[]`.
|
|
523
|
-
*
|
|
524
|
-
* Used by `createCaptureOutput()` so testkit can assert on progress behaviour
|
|
525
|
-
* without polluting stdout/stderr arrays. Terminal methods are idempotent.
|
|
526
|
-
*
|
|
527
|
-
* @internal
|
|
528
|
-
*/
|
|
529
|
-
class CaptureProgressHandle implements ProgressHandle {
|
|
530
|
-
/** Whether the handle has been stopped (terminal state reached). */
|
|
531
|
-
private stopped = false;
|
|
532
|
-
|
|
533
|
-
constructor(
|
|
534
|
-
opts: ProgressOptions,
|
|
535
|
-
private readonly events: ActivityEvent[],
|
|
536
|
-
) {
|
|
537
|
-
events.push({ type: 'progress:start', label: opts.label ?? '', total: opts.total });
|
|
538
|
-
bindMethods(this);
|
|
539
|
-
}
|
|
540
|
-
|
|
541
|
-
increment(n?: number): void {
|
|
542
|
-
if (this.stopped) return;
|
|
543
|
-
this.events.push({ type: 'progress:increment', delta: n ?? 1 });
|
|
544
|
-
}
|
|
545
|
-
|
|
546
|
-
update(value: number): void {
|
|
547
|
-
if (this.stopped) return;
|
|
548
|
-
this.events.push({ type: 'progress:update', value });
|
|
549
|
-
}
|
|
550
|
-
|
|
551
|
-
done(text?: string): void {
|
|
552
|
-
if (this.stopped) return;
|
|
553
|
-
this.stopped = true;
|
|
554
|
-
this.events.push({ type: 'progress:done', text });
|
|
555
|
-
}
|
|
556
|
-
|
|
557
|
-
fail(text?: string): void {
|
|
558
|
-
if (this.stopped) return;
|
|
559
|
-
this.stopped = true;
|
|
560
|
-
this.events.push({ type: 'progress:fail', text });
|
|
561
|
-
}
|
|
562
|
-
}
|
|
563
|
-
|
|
564
|
-
// --- Exports
|
|
565
|
-
|
|
566
|
-
export {
|
|
567
|
-
CaptureProgressHandle,
|
|
568
|
-
CaptureSpinnerHandle,
|
|
569
|
-
noopProgressHandle,
|
|
570
|
-
noopSpinnerHandle,
|
|
571
|
-
StaticProgressHandle,
|
|
572
|
-
StaticSpinnerHandle,
|
|
573
|
-
TTYProgressHandle,
|
|
574
|
-
TTYSpinnerHandle,
|
|
575
|
-
};
|
package/src/core/output/bind.ts
DELETED
|
@@ -1,59 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Method-binding helper for value objects handed to consumers.
|
|
3
|
-
*
|
|
4
|
-
* Output value objects — the `Out` channel and the spinner/progress handles
|
|
5
|
-
* — expose methods that read instance state via `this`. Consumers naturally
|
|
6
|
-
* detach those methods, either by destructuring
|
|
7
|
-
* (`const { log } = out` / `const { succeed, fail } = spinner`) or by passing
|
|
8
|
-
* them as callbacks (`promise.finally(spinner.stop)`). Detached, an unbound
|
|
9
|
-
* prototype method loses its `this` and crashes on the first field access.
|
|
10
|
-
*
|
|
11
|
-
* {@linkcode bindMethods} eliminates that footgun by installing instance-bound
|
|
12
|
-
* copies of every method, so the ergonomic usage is also the correct one.
|
|
13
|
-
*
|
|
14
|
-
* @module dreamcli/core/output/bind
|
|
15
|
-
* @internal
|
|
16
|
-
*/
|
|
17
|
-
|
|
18
|
-
/**
|
|
19
|
-
* Bind every method on an instance's prototype chain to the instance.
|
|
20
|
-
*
|
|
21
|
-
* Walks the prototype chain up to (but excluding) `Object.prototype`, binding
|
|
22
|
-
* each method name exactly once — the most-derived definition wins, so subclass
|
|
23
|
-
* overrides (e.g. `CaptureOutputChannel.spinner`) are preserved rather than
|
|
24
|
-
* clobbered by a base-class version. Bound methods are installed as
|
|
25
|
-
* non-enumerable own properties, mirroring the enumerability of the prototype
|
|
26
|
-
* methods they shadow. Getters and non-function members are left untouched.
|
|
27
|
-
*
|
|
28
|
-
* Call this once, last, in a class constructor. Because it reads the live
|
|
29
|
-
* prototype rather than a hand-maintained name list, methods added later are
|
|
30
|
-
* bound automatically — the binding can't silently fall out of date.
|
|
31
|
-
*
|
|
32
|
-
* @param instance - The object whose methods should be bound to it.
|
|
33
|
-
* @internal
|
|
34
|
-
*/
|
|
35
|
-
function bindMethods(instance: object): void {
|
|
36
|
-
const bound = instance as Record<PropertyKey, unknown>;
|
|
37
|
-
const seen = new Set<string>();
|
|
38
|
-
let proto: object | null = Object.getPrototypeOf(instance);
|
|
39
|
-
|
|
40
|
-
while (proto !== null && proto !== Object.prototype) {
|
|
41
|
-
for (const name of Object.getOwnPropertyNames(proto)) {
|
|
42
|
-
if (name === 'constructor' || seen.has(name)) continue;
|
|
43
|
-
seen.add(name);
|
|
44
|
-
|
|
45
|
-
const descriptor = Object.getOwnPropertyDescriptor(proto, name);
|
|
46
|
-
if (descriptor === undefined || typeof descriptor.value !== 'function') continue;
|
|
47
|
-
|
|
48
|
-
Object.defineProperty(bound, name, {
|
|
49
|
-
value: descriptor.value.bind(instance),
|
|
50
|
-
writable: true,
|
|
51
|
-
enumerable: false,
|
|
52
|
-
configurable: true,
|
|
53
|
-
});
|
|
54
|
-
}
|
|
55
|
-
proto = Object.getPrototypeOf(proto);
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
export { bindMethods };
|