@kjanat/dreamcli 3.0.0-rc.1 → 3.0.0-rc.3

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 (137) hide show
  1. package/CHANGELOG.md +37 -1
  2. package/dist/core/cli/dispatch.mjs +214 -1
  3. package/dist/core/cli/help-links.mjs +41 -1
  4. package/dist/core/cli/index.mjs +901 -1
  5. package/dist/core/cli/planner.mjs +300 -1
  6. package/dist/core/cli/plugin.mjs +41 -1
  7. package/dist/core/cli/propagate.mjs +57 -1
  8. package/dist/core/cli/root-help.mjs +208 -4
  9. package/dist/core/cli/root-surface.mjs +27 -1
  10. package/dist/core/cli/runtime-preflight.mjs +168 -1
  11. package/dist/core/completion/index.mjs +90 -1
  12. package/dist/core/completion/shells/bash.mjs +304 -3
  13. package/dist/core/completion/shells/fish.mjs +159 -2
  14. package/dist/core/completion/shells/powershell.mjs +353 -2
  15. package/dist/core/completion/shells/shared.mjs +154 -1
  16. package/dist/core/completion/shells/zsh.mjs +244 -2
  17. package/dist/core/config/index.mjs +246 -1
  18. package/dist/core/config/package-json.mjs +385 -3
  19. package/dist/core/errors/index.mjs +87 -1
  20. package/dist/core/execution/index.mjs +189 -1
  21. package/dist/core/help/ansi.mjs +122 -2
  22. package/dist/core/help/index.mjs +329 -7
  23. package/dist/core/json-schema/index.mjs +487 -9
  24. package/dist/core/json-schema/meta-descriptions.generated.mjs +93 -28
  25. package/dist/core/output/activity.mjs +452 -1
  26. package/dist/core/output/bind.mjs +56 -1
  27. package/dist/core/output/contracts.mjs +57 -1
  28. package/dist/core/output/display-value.mjs +32 -1
  29. package/dist/core/output/index.d.mts +21 -0
  30. package/dist/core/output/index.mjs +442 -2
  31. package/dist/core/output/renderers.mjs +46 -1
  32. package/dist/core/output/writer.mjs +12 -1
  33. package/dist/core/parse/index.mjs +555 -1
  34. package/dist/core/prompt/index.d.mts +0 -1
  35. package/dist/core/prompt/index.mjs +348 -6
  36. package/dist/core/resolve/args.mjs +105 -1
  37. package/dist/core/resolve/coerce.mjs +279 -1
  38. package/dist/core/resolve/config.mjs +19 -1
  39. package/dist/core/resolve/errors.mjs +140 -2
  40. package/dist/core/resolve/flags.mjs +196 -1
  41. package/dist/core/resolve/index.mjs +85 -1
  42. package/dist/core/resolve/property.d.mts +0 -1
  43. package/dist/core/resolve/property.mjs +45 -1
  44. package/dist/core/schema/arg.mjs +463 -1
  45. package/dist/core/schema/command.d.mts +18 -0
  46. package/dist/core/schema/command.mjs +712 -1
  47. package/dist/core/schema/flag.mjs +453 -1
  48. package/dist/core/schema/middleware.mjs +41 -1
  49. package/dist/core/schema/number-constraints.mjs +60 -1
  50. package/dist/core/schema-dsl/runtime.mjs +271 -2
  51. package/dist/core/schema-dsl/to-json-schema.mjs +74 -1
  52. package/dist/core/testkit/index.mjs +83 -1
  53. package/dist/index.d.mts +2 -1
  54. package/dist/index.mjs +22 -1
  55. package/dist/runtime/adapter.mjs +82 -1
  56. package/dist/runtime/auto.mjs +46 -1
  57. package/dist/runtime/bun.mjs +28 -1
  58. package/dist/runtime/deno.mjs +217 -2
  59. package/dist/runtime/detect.mjs +45 -1
  60. package/dist/runtime/node.mjs +169 -2
  61. package/dist/runtime/paths.mjs +48 -1
  62. package/dist/runtime.mjs +7 -1
  63. package/dist/strings.mjs +22 -1
  64. package/dist/testkit.mjs +5 -1
  65. package/dist/version.d.mts +7 -3
  66. package/dist/version.mjs +7 -1
  67. package/package.json +21 -26
  68. package/src/core/cli/dispatch.ts +0 -355
  69. package/src/core/cli/help-links.ts +0 -82
  70. package/src/core/cli/index.ts +0 -1614
  71. package/src/core/cli/planner.ts +0 -656
  72. package/src/core/cli/plugin.ts +0 -117
  73. package/src/core/cli/propagate.ts +0 -97
  74. package/src/core/cli/root-help.ts +0 -342
  75. package/src/core/cli/root-surface.ts +0 -87
  76. package/src/core/cli/runtime-preflight.ts +0 -456
  77. package/src/core/completion/index.ts +0 -138
  78. package/src/core/completion/shells/bash.ts +0 -459
  79. package/src/core/completion/shells/fish.ts +0 -259
  80. package/src/core/completion/shells/powershell.ts +0 -439
  81. package/src/core/completion/shells/shared.ts +0 -310
  82. package/src/core/completion/shells/zsh.ts +0 -381
  83. package/src/core/config/index.ts +0 -417
  84. package/src/core/config/package-json.ts +0 -560
  85. package/src/core/errors/index.ts +0 -193
  86. package/src/core/execution/index.ts +0 -265
  87. package/src/core/help/ansi.ts +0 -144
  88. package/src/core/help/index.ts +0 -563
  89. package/src/core/json-schema/index.ts +0 -910
  90. package/src/core/json-schema/meta-descriptions.generated.ts +0 -202
  91. package/src/core/output/activity.ts +0 -575
  92. package/src/core/output/bind.ts +0 -59
  93. package/src/core/output/contracts.ts +0 -194
  94. package/src/core/output/display-value.ts +0 -48
  95. package/src/core/output/index.ts +0 -704
  96. package/src/core/output/renderers.ts +0 -125
  97. package/src/core/output/writer.ts +0 -46
  98. package/src/core/parse/index.ts +0 -742
  99. package/src/core/prompt/index.ts +0 -623
  100. package/src/core/resolve/args.ts +0 -144
  101. package/src/core/resolve/coerce.ts +0 -500
  102. package/src/core/resolve/config.ts +0 -26
  103. package/src/core/resolve/contracts.ts +0 -135
  104. package/src/core/resolve/errors.ts +0 -227
  105. package/src/core/resolve/flags.ts +0 -269
  106. package/src/core/resolve/index.ts +0 -108
  107. package/src/core/resolve/property.ts +0 -99
  108. package/src/core/schema/activity.ts +0 -240
  109. package/src/core/schema/arg.ts +0 -841
  110. package/src/core/schema/command.ts +0 -1500
  111. package/src/core/schema/flag.ts +0 -917
  112. package/src/core/schema/index.ts +0 -96
  113. package/src/core/schema/middleware.ts +0 -171
  114. package/src/core/schema/number-constraints.ts +0 -153
  115. package/src/core/schema/prompt.ts +0 -171
  116. package/src/core/schema/run.ts +0 -198
  117. package/src/core/schema-dsl/index.ts +0 -109
  118. package/src/core/schema-dsl/parse.ts +0 -262
  119. package/src/core/schema-dsl/runtime.ts +0 -609
  120. package/src/core/schema-dsl/to-json-schema.ts +0 -102
  121. package/src/core/testkit/index.ts +0 -147
  122. package/src/index.ts +0 -181
  123. package/src/runtime/adapter.ts +0 -303
  124. package/src/runtime/auto.ts +0 -86
  125. package/src/runtime/bun.ts +0 -48
  126. package/src/runtime/deno.ts +0 -367
  127. package/src/runtime/detect.ts +0 -105
  128. package/src/runtime/index.ts +0 -21
  129. package/src/runtime/node-builtins.d.ts +0 -27
  130. package/src/runtime/node.ts +0 -289
  131. package/src/runtime/paths.ts +0 -64
  132. package/src/runtime/support.ts +0 -77
  133. package/src/runtime.ts +0 -31
  134. package/src/schema.ts +0 -16
  135. package/src/strings.ts +0 -34
  136. package/src/testkit.ts +0 -25
  137. 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
- };
@@ -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 };