@kjanat/dreamcli 2.5.0 → 3.0.0-rc.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (160) hide show
  1. package/CHANGELOG.md +153 -1
  2. package/README.md +9 -2
  3. package/dist/core/cli/dispatch.mjs +214 -1
  4. package/dist/core/cli/help-links.mjs +41 -1
  5. package/dist/core/cli/index.d.mts +109 -16
  6. package/dist/core/cli/index.mjs +901 -1
  7. package/dist/core/cli/planner.d.mts +27 -5
  8. package/dist/core/cli/planner.mjs +300 -1
  9. package/dist/core/cli/plugin.mjs +41 -1
  10. package/dist/core/cli/propagate.mjs +57 -1
  11. package/dist/core/cli/root-help.d.mts +16 -2
  12. package/dist/core/cli/root-help.mjs +208 -6
  13. package/dist/core/cli/root-surface.d.mts +18 -3
  14. package/dist/core/cli/root-surface.mjs +27 -1
  15. package/dist/core/cli/runtime-preflight.d.mts +16 -1
  16. package/dist/core/cli/runtime-preflight.mjs +168 -1
  17. package/dist/core/completion/index.d.mts +32 -1
  18. package/dist/core/completion/index.mjs +90 -1
  19. package/dist/core/completion/shells/bash.mjs +304 -3
  20. package/dist/core/completion/shells/fish.mjs +159 -2
  21. package/dist/core/completion/shells/powershell.mjs +353 -2
  22. package/dist/core/completion/shells/shared.d.mts +10 -0
  23. package/dist/core/completion/shells/shared.mjs +154 -1
  24. package/dist/core/completion/shells/zsh.mjs +244 -2
  25. package/dist/core/config/index.mjs +246 -1
  26. package/dist/core/config/package-json.mjs +385 -3
  27. package/dist/core/errors/index.mjs +87 -1
  28. package/dist/core/execution/index.mjs +189 -1
  29. package/dist/core/help/ansi.mjs +122 -2
  30. package/dist/core/help/index.d.mts +45 -1
  31. package/dist/core/help/index.mjs +329 -7
  32. package/dist/core/json-schema/index.mjs +487 -9
  33. package/dist/core/json-schema/meta-descriptions.generated.d.mts +38 -38
  34. package/dist/core/json-schema/meta-descriptions.generated.mjs +93 -28
  35. package/dist/core/output/activity.mjs +452 -1
  36. package/dist/core/output/bind.mjs +56 -1
  37. package/dist/core/output/contracts.mjs +57 -1
  38. package/dist/core/output/display-value.mjs +32 -1
  39. package/dist/core/output/index.d.mts +21 -0
  40. package/dist/core/output/index.mjs +442 -2
  41. package/dist/core/output/renderers.mjs +46 -1
  42. package/dist/core/output/writer.mjs +12 -1
  43. package/dist/core/parse/index.d.mts +15 -1
  44. package/dist/core/parse/index.mjs +555 -1
  45. package/dist/core/prompt/index.d.mts +11 -4
  46. package/dist/core/prompt/index.mjs +348 -6
  47. package/dist/core/resolve/args.mjs +105 -1
  48. package/dist/core/resolve/coerce.mjs +279 -1
  49. package/dist/core/resolve/config.mjs +19 -1
  50. package/dist/core/resolve/errors.mjs +140 -2
  51. package/dist/core/resolve/flags.mjs +196 -1
  52. package/dist/core/resolve/index.mjs +85 -1
  53. package/dist/core/resolve/property.d.mts +2 -1
  54. package/dist/core/resolve/property.mjs +45 -1
  55. package/dist/core/schema/arg.d.mts +90 -10
  56. package/dist/core/schema/arg.mjs +463 -1
  57. package/dist/core/schema/command.d.mts +21 -1
  58. package/dist/core/schema/command.mjs +712 -1
  59. package/dist/core/schema/flag.d.mts +92 -2
  60. package/dist/core/schema/flag.mjs +453 -1
  61. package/dist/core/schema/index.d.mts +2 -1
  62. package/dist/core/schema/middleware.mjs +41 -1
  63. package/dist/core/schema/number-constraints.d.mts +96 -0
  64. package/dist/core/schema/number-constraints.mjs +60 -0
  65. package/dist/core/schema/prompt.d.mts +15 -0
  66. package/dist/core/schema/run.d.mts +4 -4
  67. package/dist/core/schema-dsl/runtime.mjs +271 -2
  68. package/dist/core/schema-dsl/to-json-schema.mjs +74 -1
  69. package/dist/core/testkit/index.d.mts +9 -5
  70. package/dist/core/testkit/index.mjs +83 -1
  71. package/dist/index.d.mts +3 -1
  72. package/dist/index.mjs +22 -1
  73. package/dist/runtime/adapter.mjs +82 -1
  74. package/dist/runtime/auto.mjs +46 -1
  75. package/dist/runtime/bun.mjs +28 -1
  76. package/dist/runtime/deno.mjs +217 -2
  77. package/dist/runtime/detect.mjs +45 -1
  78. package/dist/runtime/node.mjs +169 -2
  79. package/dist/runtime/paths.mjs +48 -1
  80. package/dist/runtime.mjs +7 -1
  81. package/dist/strings.mjs +22 -1
  82. package/dist/testkit.mjs +5 -1
  83. package/dist/version.mjs +7 -1
  84. package/examples/basic.ts +5 -1
  85. package/examples/gh/package.json +2 -7
  86. package/examples/pwsh-demo/package.json +1 -7
  87. package/examples/pwsh-demo/src/main.test.ts +1 -4
  88. package/examples/pwsh-demo/src/main.ts +14 -4
  89. package/examples/transport-launcher.ts +85 -0
  90. package/package.json +23 -41
  91. package/dist/runtime/support.d.mts +0 -42
  92. package/dist/runtime/support.mjs +0 -1
  93. package/src/core/cli/dispatch.ts +0 -349
  94. package/src/core/cli/help-links.ts +0 -82
  95. package/src/core/cli/index.ts +0 -1413
  96. package/src/core/cli/planner.ts +0 -487
  97. package/src/core/cli/plugin.ts +0 -117
  98. package/src/core/cli/propagate.ts +0 -97
  99. package/src/core/cli/root-help.ts +0 -252
  100. package/src/core/cli/root-surface.ts +0 -74
  101. package/src/core/cli/runtime-preflight.ts +0 -438
  102. package/src/core/completion/index.ts +0 -88
  103. package/src/core/completion/shells/bash.ts +0 -459
  104. package/src/core/completion/shells/fish.ts +0 -259
  105. package/src/core/completion/shells/powershell.ts +0 -439
  106. package/src/core/completion/shells/shared.ts +0 -283
  107. package/src/core/completion/shells/zsh.ts +0 -381
  108. package/src/core/config/index.ts +0 -417
  109. package/src/core/config/package-json.ts +0 -560
  110. package/src/core/errors/index.ts +0 -193
  111. package/src/core/execution/index.ts +0 -268
  112. package/src/core/help/ansi.ts +0 -144
  113. package/src/core/help/index.ts +0 -516
  114. package/src/core/json-schema/index.ts +0 -891
  115. package/src/core/json-schema/meta-descriptions.generated.ts +0 -185
  116. package/src/core/output/activity.ts +0 -575
  117. package/src/core/output/bind.ts +0 -59
  118. package/src/core/output/contracts.ts +0 -194
  119. package/src/core/output/display-value.ts +0 -48
  120. package/src/core/output/index.ts +0 -704
  121. package/src/core/output/renderers.ts +0 -125
  122. package/src/core/output/writer.ts +0 -46
  123. package/src/core/parse/index.ts +0 -681
  124. package/src/core/prompt/index.ts +0 -583
  125. package/src/core/resolve/args.ts +0 -144
  126. package/src/core/resolve/coerce.ts +0 -432
  127. package/src/core/resolve/config.ts +0 -26
  128. package/src/core/resolve/contracts.ts +0 -135
  129. package/src/core/resolve/errors.ts +0 -227
  130. package/src/core/resolve/flags.ts +0 -255
  131. package/src/core/resolve/index.ts +0 -108
  132. package/src/core/resolve/property.ts +0 -99
  133. package/src/core/schema/activity.ts +0 -240
  134. package/src/core/schema/arg.ts +0 -719
  135. package/src/core/schema/command.ts +0 -1498
  136. package/src/core/schema/flag.ts +0 -801
  137. package/src/core/schema/index.ts +0 -91
  138. package/src/core/schema/middleware.ts +0 -171
  139. package/src/core/schema/prompt.ts +0 -156
  140. package/src/core/schema/run.ts +0 -198
  141. package/src/core/schema-dsl/index.ts +0 -109
  142. package/src/core/schema-dsl/parse.ts +0 -262
  143. package/src/core/schema-dsl/runtime.ts +0 -609
  144. package/src/core/schema-dsl/to-json-schema.ts +0 -102
  145. package/src/core/testkit/index.ts +0 -123
  146. package/src/index.ts +0 -179
  147. package/src/runtime/adapter.ts +0 -303
  148. package/src/runtime/auto.ts +0 -110
  149. package/src/runtime/bun.ts +0 -55
  150. package/src/runtime/deno.ts +0 -369
  151. package/src/runtime/detect.ts +0 -105
  152. package/src/runtime/index.ts +0 -21
  153. package/src/runtime/node-builtins.d.ts +0 -27
  154. package/src/runtime/node.ts +0 -300
  155. package/src/runtime/paths.ts +0 -64
  156. package/src/runtime/support.ts +0 -135
  157. package/src/runtime.ts +0 -31
  158. package/src/strings.ts +0 -34
  159. package/src/testkit.ts +0 -25
  160. package/src/version.ts +0 -20
@@ -1,2 +1,442 @@
1
- import{formatDisplayValue as e}from"./display-value.mjs";import{bindMethods as t}from"./bind.mjs";import{writeLine as n}from"./writer.mjs";import{CaptureProgressHandle as r,CaptureSpinnerHandle as i}from"./activity.mjs";import{resolveOutputPolicy as a,resolveProgressPolicy as o,resolveSpinnerPolicy as s,resolveTableFormat as c,resolveTableTextStream as l,resolveTextStream as u,shouldEmitInfo as d}from"./contracts.mjs";import{createProgressHandleFromPolicy as f,createSpinnerHandleFromPolicy as p,resolveWriterForStream as m}from"./renderers.mjs";const h=()=>{},g=new WeakMap;function _(e){if(!Number.isInteger(e)||e<0||e>255)throw RangeError(`Exit code must be an integer from 0 to 255`);return e}function v(e,t){g.set(e,_(t))}function y(e){return g.get(e)}function b(e){g.delete(e)}function x(e){return{stdout:e?.stdout??h,stderr:e?.stderr??h,isTTY:e?.isTTY??!1,verbosity:e?.verbosity??`normal`,jsonMode:e?.jsonMode??!1}}var S=class{options;policy;jsonMode;isTTY;constructor(e){this.options=e,this.policy=a(e),this.jsonMode=e.jsonMode,this.isTTY=e.isTTY,t(this)}log(e){n(m(this.options,u(this.policy)),e)}info(e){d(this.policy)&&n(m(this.options,u(this.policy)),e)}warn(e){n(this.options.stderr,e)}error(e){n(this.options.stderr,e)}setExitCode(e){v(this,e)}json(e){n(this.options.stdout,JSON.stringify(e))}table(e,t,r){let{columns:i,options:a}=E(t,r),o=c(this.policy,a),s=i??w(e);if(o===`json`){this.json(D(e,s));return}let l=A(e,s);l.length!==0&&n(O(this.options,this.policy,o,a),l)}activeCleanup;stopActive(){this.activeCleanup!==void 0&&(this.activeCleanup(),this.activeCleanup=void 0)}spinner(e,t){let n=p(s(this.policy,t?.fallback??`silent`),e,this.options.stderr);return n.cleanup===void 0?n.handle:(this.stopActive(),this.activeCleanup=n.cleanup,n.handle)}progress(e){let t=f(o(this.policy,e.fallback??`silent`),e,this.options.stderr);return t.cleanup===void 0?t.handle:(this.stopActive(),this.activeCleanup=t.cleanup,t.handle)}},C=class extends S{activity;constructor(e,t){super(e),this.activity=t}spinner(e,t){return new i(e,this.activity)}progress(e){return new r(e,this.activity)}};function w(e){let t=e[0];return t===void 0?[]:Object.keys(t).map(e=>({key:e}))}function T(e){return Array.isArray(e)}function E(e,t){return t===void 0?T(e)?{columns:e,options:t}:{columns:void 0,options:e}:{columns:T(e)?e:void 0,options:t}}function D(e,t){return e.map(e=>Object.fromEntries(t.map(t=>[t.key,e[t.key]])))}function O(e,t,n,r){return n===`json`?e.stdout:m(e,l(t,r))}function k(t){return e(t)}function A(e,t){if(e.length===0||t.length===0)return``;let n=t.map(e=>e.header??e.key),r=e.map(e=>t.map(t=>k(e[t.key]))),i=n.map((e,t)=>{let n=e.length;for(let e of r){let r=e[t];r!==void 0&&r.length>n&&(n=r.length)}return n});return[n.map((e,t)=>e.padEnd(i[t]??0)).join(` `),i.map(e=>`-`.repeat(e)).join(` `),...r.map(e=>e.map((e,t)=>e.padEnd(i[t]??0)).join(` `))].join(`
2
- `)}function j(e){return new S(x(e))}function M(e){let t={stdout:[],stderr:[],activity:[]};return[new C(x({...e,stdout:e=>t.stdout.push(e),stderr:e=>t.stderr.push(e)}),t.activity),t]}export{b as clearRequestedExitCode,M as createCaptureOutput,j as createOutput,y as getRequestedExitCode};
1
+ import { formatDisplayValue } from "./display-value.mjs";
2
+ import { bindMethods } from "./bind.mjs";
3
+ import { writeLine } from "./writer.mjs";
4
+ import { CaptureProgressHandle, CaptureSpinnerHandle } from "./activity.mjs";
5
+ import { resolveOutputPolicy, resolveProgressPolicy, resolveSpinnerPolicy, resolveTableFormat, resolveTableTextStream, resolveTextStream, shouldEmitInfo } from "./contracts.mjs";
6
+ import { createProgressHandleFromPolicy, createSpinnerHandleFromPolicy, resolveWriterForStream } from "./renderers.mjs";
7
+ import { createColors, isColorSupported } from "ansispeck";
8
+ //#region src/core/output/index.ts
9
+ /**
10
+ * Output channel (out object) for command handlers.
11
+ *
12
+ * Provides a structured output abstraction that handlers use instead of
13
+ * raw `console.log`. The channel respects TTY vs piped contexts and
14
+ * supports verbosity levels (quiet mode suppresses `info`).
15
+ *
16
+ * The {@linkcode Out} interface (the contract handlers depend on) lives in
17
+ * `src/core/schema/command.ts`. This module provides the implementation.
18
+ *
19
+ * @module dreamcli/core/output
20
+ */
21
+ /**
22
+ * A noop writer used as fallback when no writer is provided and
23
+ * platform defaults aren't available. Silently discards output.
24
+ */
25
+ const noopWrite = () => {};
26
+ const requestedExitCodes = /* @__PURE__ */ new WeakMap();
27
+ function validateExitCode(code) {
28
+ if (!Number.isInteger(code) || code < 0 || code > 255) throw new RangeError("Exit code must be an integer from 0 to 255");
29
+ return code;
30
+ }
31
+ function setRequestedExitCode(out, code) {
32
+ requestedExitCodes.set(out, validateExitCode(code));
33
+ }
34
+ function getRequestedExitCode(out) {
35
+ return requestedExitCodes.get(out);
36
+ }
37
+ function clearRequestedExitCode(out) {
38
+ requestedExitCodes.delete(out);
39
+ }
40
+ /** Merge user-supplied options with defaults. */
41
+ function resolveOptions(options) {
42
+ const isTTY = options?.isTTY ?? false;
43
+ const jsonMode = options?.jsonMode ?? false;
44
+ return {
45
+ stdout: options?.stdout ?? noopWrite,
46
+ stderr: options?.stderr ?? noopWrite,
47
+ isTTY,
48
+ verbosity: options?.verbosity ?? "normal",
49
+ jsonMode,
50
+ color: options?.color ?? (isTTY && !jsonMode && isColorSupported)
51
+ };
52
+ }
53
+ /**
54
+ * Concrete implementation of the {@linkcode Out} interface.
55
+ *
56
+ * Routes messages to the appropriate writer (stdout vs stderr) and
57
+ * respects the configured verbosity level.
58
+ *
59
+ * Handlers interact with this via the {@linkcode Out} interface — they never see
60
+ * {@linkcode OutputChannel} directly, which keeps the coupling minimal.
61
+ * Prefer {@link createOutput} unless you are extending the output layer.
62
+ *
63
+ * @internal
64
+ */
65
+ var OutputChannel = class {
66
+ /** @internal Resolved configuration. */
67
+ options;
68
+ /** Semantic output policy snapshot shared by routing helpers. */
69
+ policy;
70
+ /** Whether JSON output mode is active. */
71
+ jsonMode;
72
+ /**
73
+ * Whether stdout is connected to a TTY.
74
+ *
75
+ * When `jsonMode` is active, this reflects the underlying TTY status
76
+ * but decorative output should still be suppressed (jsonMode takes
77
+ * precedence).
78
+ */
79
+ isTTY;
80
+ /**
81
+ * Context-aware ANSI color palette.
82
+ *
83
+ * Enabled per the resolved `color` option — identity functions when
84
+ * colors are off, so handlers can style unconditionally.
85
+ */
86
+ color;
87
+ constructor(options) {
88
+ this.options = options;
89
+ this.policy = resolveOutputPolicy(options);
90
+ this.jsonMode = options.jsonMode;
91
+ this.isTTY = options.isTTY;
92
+ this.color = createColors(options.color);
93
+ bindMethods(this);
94
+ }
95
+ /**
96
+ * Write to stdout (normal output). Always emitted.
97
+ *
98
+ * In JSON mode, redirected to stderr so stdout is reserved for
99
+ * structured `json()` output only.
100
+ */
101
+ log(message) {
102
+ writeLine(resolveWriterForStream(this.options, resolveTextStream(this.policy)), message);
103
+ }
104
+ /**
105
+ * Informational message to stdout.
106
+ * Suppressed when verbosity is `'quiet'`.
107
+ *
108
+ * In JSON mode, redirected to stderr.
109
+ */
110
+ info(message) {
111
+ if (!shouldEmitInfo(this.policy)) return;
112
+ writeLine(resolveWriterForStream(this.options, resolveTextStream(this.policy)), message);
113
+ }
114
+ /** Warning to stderr. Always emitted. */
115
+ warn(message) {
116
+ writeLine(this.options.stderr, message);
117
+ }
118
+ /** Error to stderr. Always emitted. */
119
+ error(message) {
120
+ writeLine(this.options.stderr, message);
121
+ }
122
+ /** Request a successful-process exit code without writing error output. */
123
+ setExitCode(code) {
124
+ setRequestedExitCode(this, code);
125
+ }
126
+ /**
127
+ * Emit a structured JSON value to stdout.
128
+ *
129
+ * Serialises `value` with `JSON.stringify` and writes to stdout
130
+ * regardless of JSON mode — `json()` always targets stdout.
131
+ */
132
+ json(value) {
133
+ writeLine(this.options.stdout, JSON.stringify(value));
134
+ }
135
+ table(rows, columnsOrOptions, options) {
136
+ const { columns, options: tableOptions } = resolveTableArgs(columnsOrOptions, options);
137
+ const format = resolveTableFormat(this.policy, tableOptions);
138
+ const resolved = columns ?? inferColumns(rows);
139
+ if (format === "json") {
140
+ this.json(projectTableRows(rows, resolved));
141
+ return;
142
+ }
143
+ const text = formatTable(rows, resolved);
144
+ if (text.length === 0) return;
145
+ writeLine(resolveTextTableWriter(this.options, this.policy, format, tableOptions), text);
146
+ }
147
+ /**
148
+ * Cleanup callback for the currently active spinner/progress handle.
149
+ *
150
+ * Set to `undefined` when no handle is active. The callback calls the
151
+ * appropriate terminal method (`.stop()` for spinners, `.done()` for
152
+ * progress) on the previous handle.
153
+ *
154
+ * @internal
155
+ */
156
+ activeCleanup;
157
+ /**
158
+ * Stop the currently active spinner/progress handle (if any).
159
+ *
160
+ * Called internally before creating a new handle to prevent overlap,
161
+ * and externally after handler execution to clean up leaked timers.
162
+ * Idempotent (safe to call when no handle is active).
163
+ */
164
+ stopActive() {
165
+ if (this.activeCleanup !== void 0) {
166
+ this.activeCleanup();
167
+ this.activeCleanup = void 0;
168
+ }
169
+ }
170
+ /**
171
+ * Create a spinner handle.
172
+ *
173
+ * Mode dispatch:
174
+ * - `jsonMode` → noop (structured output only, spinners suppressed)
175
+ * - `isTTY` → animated TTY spinner (braille frames, ANSI overwrite)
176
+ * - `!isTTY && fallback: 'static'` → plain text at lifecycle boundaries
177
+ * - `!isTTY && fallback: 'silent'` (default) → noop
178
+ *
179
+ * All activity output (transient frames, terminal messages) routes to
180
+ * stderr so stdout remains clean for structured data and piping.
181
+ *
182
+ * If another spinner or progress handle is active, it is implicitly
183
+ * stopped before the new one starts.
184
+ *
185
+ * @virtual
186
+ */
187
+ spinner(text, options) {
188
+ const rendered = createSpinnerHandleFromPolicy(resolveSpinnerPolicy(this.policy, options?.fallback ?? "silent"), text, this.options.stderr);
189
+ if (rendered.cleanup === void 0) return rendered.handle;
190
+ this.stopActive();
191
+ this.activeCleanup = rendered.cleanup;
192
+ return rendered.handle;
193
+ }
194
+ /**
195
+ * Create a progress handle.
196
+ *
197
+ * Mode dispatch:
198
+ * - `jsonMode` → noop (structured output only, progress suppressed)
199
+ * - `isTTY` → animated TTY progress bar (determinate or indeterminate)
200
+ * - `!isTTY && fallback: 'static'` → plain text at lifecycle boundaries
201
+ * - `!isTTY && fallback: 'silent'` (default) → noop
202
+ *
203
+ * All activity output (transient frames, terminal messages) routes to
204
+ * stderr so stdout remains clean for structured data and piping.
205
+ *
206
+ * If another spinner or progress handle is active, it is implicitly
207
+ * stopped before the new one starts.
208
+ *
209
+ * @virtual
210
+ */
211
+ progress(opts) {
212
+ const rendered = createProgressHandleFromPolicy(resolveProgressPolicy(this.policy, opts.fallback ?? "silent"), opts, this.options.stderr);
213
+ if (rendered.cleanup === void 0) return rendered.handle;
214
+ this.stopActive();
215
+ this.activeCleanup = rendered.cleanup;
216
+ return rendered.handle;
217
+ }
218
+ };
219
+ /**
220
+ * Output channel variant that returns capture handles for spinner/progress.
221
+ *
222
+ * Extends {@linkcode OutputChannel} to override `spinner()` and `progress()`, routing
223
+ * activity events into a shared {@linkcode ActivityEvent | ActivityEvent[]} for testkit assertion.
224
+ * Text output (log/info/warn/error) is handled by the parent class.
225
+ *
226
+ * @internal
227
+ */
228
+ var CaptureOutputChannel = class extends OutputChannel {
229
+ activity;
230
+ constructor(options, activity) {
231
+ super(options);
232
+ this.activity = activity;
233
+ }
234
+ /**
235
+ * Return a {@link CaptureSpinnerHandle} that records events into the activity log.
236
+ * @override
237
+ */
238
+ spinner(text, _options) {
239
+ return new CaptureSpinnerHandle(text, this.activity);
240
+ }
241
+ /**
242
+ * Return a {@link CaptureProgressHandle} that records events into the activity log.
243
+ * @override
244
+ */
245
+ progress(opts) {
246
+ return new CaptureProgressHandle(opts, this.activity);
247
+ }
248
+ };
249
+ /**
250
+ * Infer column descriptors from the keys of the first row.
251
+ *
252
+ * When no explicit columns are provided, all keys from the first row
253
+ * become columns in insertion order. This is a convenience — callers
254
+ * who need stable ordering should provide explicit columns.
255
+ *
256
+ * @param rows - Data rows to derive columns from.
257
+ * @returns Column descriptors, one per key of the first row (empty if `rows` is empty).
258
+ */
259
+ function inferColumns(rows) {
260
+ const first = rows[0];
261
+ if (first === void 0) return [];
262
+ return Object.keys(first).map((key) => ({ key }));
263
+ }
264
+ /**
265
+ * Type guard: is `value` a {@link TableColumn} array (not {@link TableOptions})?
266
+ *
267
+ * @param value - The ambiguous argument to discriminate.
268
+ * @returns `true` when `value` is a column descriptor array.
269
+ */
270
+ function isTableColumns(value) {
271
+ return Array.isArray(value);
272
+ }
273
+ /**
274
+ * Disambiguate the overloaded `table()` arguments.
275
+ *
276
+ * @param columnsOrOptions - Either column descriptors or table options (from the 2-arg overload).
277
+ * @param options - Explicit table options (from the 3-arg overload).
278
+ * @returns Separated `columns` and `options`, each possibly `undefined`.
279
+ */
280
+ function resolveTableArgs(columnsOrOptions, options) {
281
+ if (options !== void 0) return {
282
+ columns: isTableColumns(columnsOrOptions) ? columnsOrOptions : void 0,
283
+ options
284
+ };
285
+ if (isTableColumns(columnsOrOptions)) return {
286
+ columns: columnsOrOptions,
287
+ options
288
+ };
289
+ return {
290
+ columns: void 0,
291
+ options: columnsOrOptions
292
+ };
293
+ }
294
+ /**
295
+ * Keep only the keys listed in `columns`, preserving column order.
296
+ *
297
+ * @param rows - Source data rows.
298
+ * @param columns - Column descriptors controlling which keys to retain.
299
+ * @returns Projected rows containing only the specified keys.
300
+ */
301
+ function projectTableRows(rows, columns) {
302
+ return rows.map((row) => Object.fromEntries(columns.map((c) => [c.key, row[c.key]])));
303
+ }
304
+ /**
305
+ * Determine the concrete table format (`'text'` or `'json'`).
306
+ *
307
+ * Resolves `'auto'` / `undefined` by falling back to the channel's `jsonMode`.
308
+ *
309
+ * @param jsonMode - Whether the channel is in JSON output mode.
310
+ * @param options - Per-call table options (may specify an explicit format).
311
+ * @returns `'text'` or `'json'`.
312
+ */
313
+ /**
314
+ * Pick the {@link WriteFn} for a text-mode table.
315
+ *
316
+ * Respects `tableOptions.stream` when present; otherwise falls back to
317
+ * the channel's default policy-owned text stream.
318
+ *
319
+ * @param options - Resolved channel options.
320
+ * @param format - Resolved table format.
321
+ * @param tableOptions - Per-call table options (may specify a stream override).
322
+ * @returns The writer to use.
323
+ */
324
+ function resolveTextTableWriter(options, policy, format, tableOptions) {
325
+ if (format === "json") return options.stdout;
326
+ return resolveWriterForStream(options, resolveTableTextStream(policy, tableOptions));
327
+ }
328
+ /**
329
+ * Convert a cell value to a display string.
330
+ *
331
+ * - `null` / `undefined` → `''`
332
+ * - Primitives → `String(value)`
333
+ * - Objects / arrays → JSON
334
+ *
335
+ * @param value - The raw cell value.
336
+ * @returns Display string for the cell.
337
+ */
338
+ function cellToString(value) {
339
+ return formatDisplayValue(value);
340
+ }
341
+ /**
342
+ * Format rows as an aligned text table.
343
+ *
344
+ * Returns the full table string (without trailing newline — the caller
345
+ * adds that via `writeLine`). Returns `''` when rows is empty.
346
+ *
347
+ * Column widths are computed as the max of header and all cell values.
348
+ * Cells are left-aligned, padded with spaces. Columns are separated by
349
+ * two spaces.
350
+ *
351
+ * @param rows - Data rows to render.
352
+ * @param columns - Column descriptors controlling key, header, and order.
353
+ * @returns The formatted table string, or `''` when there is nothing to render.
354
+ */
355
+ function formatTable(rows, columns) {
356
+ if (rows.length === 0 || columns.length === 0) return "";
357
+ const SEPARATOR = " ";
358
+ const headers = columns.map((c) => c.header ?? c.key);
359
+ const cellGrid = rows.map((row) => columns.map((c) => cellToString(row[c.key])));
360
+ const widths = headers.map((h, i) => {
361
+ let max = h.length;
362
+ for (const row of cellGrid) {
363
+ const cell = row[i];
364
+ if (cell !== void 0 && cell.length > max) max = cell.length;
365
+ }
366
+ return max;
367
+ });
368
+ return [
369
+ headers.map((h, i) => h.padEnd(widths[i] ?? 0)).join(SEPARATOR),
370
+ widths.map((w) => "-".repeat(w)).join(SEPARATOR),
371
+ ...cellGrid.map((row) => row.map((cell, i) => cell.padEnd(widths[i] ?? 0)).join(SEPARATOR))
372
+ ].join("\n");
373
+ }
374
+ /**
375
+ * Create an output channel.
376
+ *
377
+ * Low-level factory: action handlers already receive `out` automatically from
378
+ * `cli()`, `.execute()`, and `runCommand()`. Call `createOutput()` when you
379
+ * are embedding DreamCLI primitives into a custom runtime or need a standalone
380
+ * output implementation outside the normal command pipeline.
381
+ *
382
+ * @param options - Optional configuration. When omitted, output is
383
+ * discarded (useful for silent test runs). Pass `stdout`/`stderr`
384
+ * writers to direct output somewhere useful.
385
+ *
386
+ * @returns An {@link Out} instance backed by an {@link OutputChannel}.
387
+ *
388
+ * @example
389
+ * ```ts
390
+ * // Production (wired by the runtime adapter)
391
+ * const out = createOutput({
392
+ * stdout: (s) => process.stdout.write(s),
393
+ * stderr: (s) => process.stderr.write(s),
394
+ * isTTY: process.stdout.isTTY === true,
395
+ * });
396
+ *
397
+ * // Test (capture output)
398
+ * const lines: string[] = [];
399
+ * const out = createOutput({
400
+ * stdout: (s) => lines.push(s),
401
+ * stderr: (s) => lines.push(s),
402
+ * });
403
+ * ```
404
+ */
405
+ function createOutput(options) {
406
+ return new OutputChannel(resolveOptions(options));
407
+ }
408
+ /**
409
+ * Create an output channel that captures all output into arrays.
410
+ *
411
+ * Useful in tests to assert on what a handler wrote without touching
412
+ * real I/O.
413
+ *
414
+ * @param options - Optional {@link OutputOptions} (minus `stdout`/`stderr`,
415
+ * which are wired to the capture buffers automatically).
416
+ *
417
+ * @returns A tuple of `[out, captured]` — the {@link Out} channel and the
418
+ * {@link CapturedOutput} buffers.
419
+ *
420
+ * @example
421
+ * ```ts
422
+ * const [out, captured] = createCaptureOutput();
423
+ * out.log('hello');
424
+ * out.warn('danger');
425
+ * expect(captured.stdout).toEqual(['hello\n']);
426
+ * expect(captured.stderr).toEqual(['danger\n']);
427
+ * ```
428
+ */
429
+ function createCaptureOutput(options) {
430
+ const captured = {
431
+ stdout: [],
432
+ stderr: [],
433
+ activity: []
434
+ };
435
+ return [new CaptureOutputChannel(resolveOptions({
436
+ ...options,
437
+ stdout: (s) => captured.stdout.push(s),
438
+ stderr: (s) => captured.stderr.push(s)
439
+ }), captured.activity), captured];
440
+ }
441
+ //#endregion
442
+ export { clearRequestedExitCode, createCaptureOutput, createOutput, getRequestedExitCode };
@@ -1 +1,46 @@
1
- import{StaticProgressHandle as e,StaticSpinnerHandle as t,TTYProgressHandle as n,TTYSpinnerHandle as r,noopProgressHandle as i,noopSpinnerHandle as a}from"./activity.mjs";function o(e,t){return t===`stderr`?e.stderr:e.stdout}function s(e,n,i){if(e.mode===`noop`)return{handle:a,cleanup:void 0};if(e.mode===`tty`){let e=new r(n,i);return{handle:e,cleanup:()=>e.stop()}}let o=new t(n,i);return{handle:o,cleanup:()=>o.stop()}}function c(t,r,a){if(t.mode===`noop`)return{handle:i,cleanup:void 0};if(t.mode===`tty`){let e=new n(r,a);return{handle:e,cleanup:()=>e.done()}}let o=new e(r.label,a);return{handle:o,cleanup:()=>o.done()}}export{c as createProgressHandleFromPolicy,s as createSpinnerHandleFromPolicy,o as resolveWriterForStream};
1
+ import { StaticProgressHandle, StaticSpinnerHandle, TTYProgressHandle, TTYSpinnerHandle, noopProgressHandle, noopSpinnerHandle } from "./activity.mjs";
2
+ //#region src/core/output/renderers.ts
3
+ /** Select the concrete {@linkcode WriteFn} for a given stream label. @internal */
4
+ function resolveWriterForStream(writers, stream) {
5
+ return stream === "stderr" ? writers.stderr : writers.stdout;
6
+ }
7
+ /** Create a spinner handle matching the resolved {@linkcode ActivityPolicy} mode. @internal */
8
+ function createSpinnerHandleFromPolicy(policy, text, stderr) {
9
+ if (policy.mode === "noop") return {
10
+ handle: noopSpinnerHandle,
11
+ cleanup: void 0
12
+ };
13
+ if (policy.mode === "tty") {
14
+ const handle = new TTYSpinnerHandle(text, stderr);
15
+ return {
16
+ handle,
17
+ cleanup: () => handle.stop()
18
+ };
19
+ }
20
+ const handle = new StaticSpinnerHandle(text, stderr);
21
+ return {
22
+ handle,
23
+ cleanup: () => handle.stop()
24
+ };
25
+ }
26
+ /** Create a progress handle matching the resolved {@linkcode ActivityPolicy} mode. @internal */
27
+ function createProgressHandleFromPolicy(policy, options, stderr) {
28
+ if (policy.mode === "noop") return {
29
+ handle: noopProgressHandle,
30
+ cleanup: void 0
31
+ };
32
+ if (policy.mode === "tty") {
33
+ const handle = new TTYProgressHandle(options, stderr);
34
+ return {
35
+ handle,
36
+ cleanup: () => handle.done()
37
+ };
38
+ }
39
+ const handle = new StaticProgressHandle(options.label, stderr);
40
+ return {
41
+ handle,
42
+ cleanup: () => handle.done()
43
+ };
44
+ }
45
+ //#endregion
46
+ export { createProgressHandleFromPolicy, createSpinnerHandleFromPolicy, resolveWriterForStream };
@@ -1 +1,12 @@
1
- function e(e,t){e(`${t}\n`)}export{e as writeLine};
1
+ //#region src/core/output/writer.ts
2
+ /**
3
+ * Write a message followed by a newline.
4
+ *
5
+ * @param write - The underlying write function.
6
+ * @param message - The text to write.
7
+ */
8
+ function writeLine(write, message) {
9
+ write(`${message}\n`);
10
+ }
11
+ //#endregion
12
+ export { writeLine };
@@ -58,6 +58,20 @@ declare function tokenize(argv: readonly string[]): readonly Token[];
58
58
  * @returns `true` if `token` occurs before any `--` separator.
59
59
  */
60
60
  declare function includesBeforeSeparator(argv: readonly string[], token: string): boolean;
61
+ /**
62
+ * Remove every occurrence of `token` that appears before the `--` end-of-options
63
+ * separator, leaving post-separator literals untouched.
64
+ *
65
+ * The strip counterpart to {@linkcode includesBeforeSeparator}: root-level flags
66
+ * (`--json`) are stripped before dispatch/parse so the command schema never sees
67
+ * them, but a literal after `--` (`-- --json`) must reach the command unchanged.
68
+ *
69
+ * @param argv - Raw argument strings.
70
+ * @param token - Exact flag token to strip (e.g. `--json`).
71
+ * @returns A new argv with pre-separator occurrences of `token` removed, or the
72
+ * original reference when `token` does not occur before the separator.
73
+ */
74
+ declare function stripBeforeSeparator(argv: readonly string[], token: string): readonly string[];
61
75
  /**
62
76
  * Raw parsed values before resolution (defaults, env, config, etc.).
63
77
  *
@@ -106,4 +120,4 @@ declare function flagExpectsValue(schema: FlagSchema): boolean;
106
120
  */
107
121
  declare function parse(schema: CommandSchema, argv: readonly string[]): ParseResult;
108
122
  //#endregion
109
- export { type ParseResult, type Token, buildFlagLookup, flagExpectsValue, includesBeforeSeparator, parse, tokenize };
123
+ export { type ParseResult, type Token, buildFlagLookup, flagExpectsValue, includesBeforeSeparator, parse, stripBeforeSeparator, tokenize };