@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 +1,453 @@
1
- import{assertNumberConstraints as e}from"./number-constraints.mjs";function t(e){return typeof e==`string`?{name:e,hidden:!1}:{name:e.name,hidden:e.hidden}}function n(e){return e.map(t)}function r(e,t){let n=t?.includeHidden??!1,r=t?.kind??`all`;return e.aliases.filter(e=>n||!e.hidden).filter(e=>r===`short`?e.name.length===1:r===`long`?e.name.length>1:!0).map(e=>e.name)}function i(e,t){let r=t?.aliases===void 0?[]:n(t.aliases),{aliases:i,...a}=t??{};return{kind:e,presence:`optional`,defaultValue:void 0,envVar:void 0,configPath:void 0,description:void 0,enumValues:void 0,numberConstraints:void 0,elementSchema:void 0,prompt:void 0,parseFn:void 0,deprecated:void 0,propagate:!1,...a,aliases:r}}var a=class t{schema;constructor(e){this.schema=e}default(e){return new t({...this.schema,presence:`defaulted`,defaultValue:e})}required(){return new t({...this.schema,presence:`required`})}alias(e,n){return new t({...this.schema,aliases:[...this.schema.aliases,{name:e,hidden:n?.hidden??!1}]})}env(e){return new t({...this.schema,envVar:e})}config(e){return new t({...this.schema,configPath:e})}describe(e){return new t({...this.schema,description:e})}prompt(e){return new t({...this.schema,prompt:e})}deprecated(e){return new t({...this.schema,deprecated:e??!0})}propagate(){return new t({...this.schema,propagate:!0})}int(e=!0){return new t({...this.schema,numberConstraints:{...this.schema.numberConstraints,int:e}})}min(n){let r={...this.schema.numberConstraints,min:n};return e(r),new t({...this.schema,numberConstraints:r})}max(n){let r={...this.schema.numberConstraints,max:n};return e(r),new t({...this.schema,numberConstraints:r})}finite(e=!0){return new t({...this.schema,numberConstraints:{...this.schema.numberConstraints,finite:e}})}};const o={string(){return new a(i(`string`))},number(t){return t!==void 0&&e(t),new a(i(`number`,t===void 0?{}:{numberConstraints:t}))},boolean(){return new a(i(`boolean`,{presence:`defaulted`,defaultValue:!1}))},enum(e){return new a(i(`enum`,{enumValues:e}))},array(e){return new a(i(`array`,{elementSchema:e.schema}))},custom(e){return new a(i(`custom`,{parseFn:e}))}};export{a as FlagBuilder,i as createSchema,o as flag,r as getFlagAliasNames};
1
+ import { assertNumberConstraints } from "./number-constraints.mjs";
2
+ //#region src/core/schema/flag.ts
3
+ /**
4
+ * Flag schema builder with full type inference.
5
+ *
6
+ * Each factory (`flag.string()`, `flag.boolean()`, etc.) returns an immutable
7
+ * {@linkcode FlagBuilder} whose generic parameter tracks the value type and presence
8
+ * state. Chained modifiers (`.default()`, `.required()`, `.alias()`, …) return
9
+ * new builders with updated type-level and runtime state.
10
+ *
11
+ * @module dreamcli/core/schema/flag
12
+ */
13
+ /**
14
+ * Normalise an alias input into a full {@link FlagAlias} object.
15
+ *
16
+ * @param alias - Raw alias name or structured alias object.
17
+ * @returns Normalised alias record.
18
+ */
19
+ function normalizeFlagAlias(alias) {
20
+ if (typeof alias === "string") return {
21
+ name: alias,
22
+ hidden: false
23
+ };
24
+ return {
25
+ name: alias.name,
26
+ hidden: alias.hidden
27
+ };
28
+ }
29
+ /**
30
+ * Normalise alias input into immutable alias records.
31
+ *
32
+ * @param aliases - Alias input values.
33
+ * @returns Normalised alias objects.
34
+ */
35
+ function normalizeFlagAliases(aliases) {
36
+ return aliases.map(normalizeFlagAlias);
37
+ }
38
+ /**
39
+ * List alias names for a flag schema.
40
+ *
41
+ * @param schema - Flag schema whose aliases should be listed.
42
+ * @param options - Visibility and length filtering.
43
+ * @returns Alias names in registration order.
44
+ */
45
+ function getFlagAliasNames(schema, options) {
46
+ const includeHidden = options?.includeHidden ?? false;
47
+ const kind = options?.kind ?? "all";
48
+ return schema.aliases.filter((alias) => includeHidden || !alias.hidden).filter((alias) => {
49
+ if (kind === "short") return alias.name.length === 1;
50
+ if (kind === "long") return alias.name.length > 1;
51
+ return true;
52
+ }).map((alias) => alias.name);
53
+ }
54
+ /**
55
+ * Create a raw {@link FlagSchema} object with sensible defaults.
56
+ *
57
+ * Most consumers should prefer the higher-level {@link flag} factory,
58
+ * which returns an immutable {@link FlagBuilder} with type inference and
59
+ * safe modifier chaining. `createSchema()` is the low-level escape hatch
60
+ * for advanced schema composition, tests, or custom factories that need to
61
+ * work directly with the runtime descriptor.
62
+ *
63
+ * `overrides` are shallow-merged on top of the default shape, so callers are
64
+ * responsible for keeping the resulting schema internally consistent.
65
+ *
66
+ * @param kind - Discriminator for the value type this flag accepts.
67
+ * @param overrides - Partial {@link FlagSchema} fields merged onto defaults.
68
+ * @returns A fully populated {@link FlagSchema}.
69
+ *
70
+ * @example
71
+ * ```ts
72
+ * const schema = createSchema('enum', {
73
+ * enumValues: ['us', 'eu', 'ap'],
74
+ * description: 'Deployment region',
75
+ * });
76
+ * ```
77
+ */
78
+ function createSchema(kind, overrides) {
79
+ const aliases = overrides?.aliases !== void 0 ? normalizeFlagAliases(overrides.aliases) : [];
80
+ const { aliases: _ignoredAliases, ...rest } = overrides ?? {};
81
+ return {
82
+ kind,
83
+ presence: "optional",
84
+ defaultValue: void 0,
85
+ envVar: void 0,
86
+ configPath: void 0,
87
+ description: void 0,
88
+ enumValues: void 0,
89
+ numberConstraints: void 0,
90
+ elementSchema: void 0,
91
+ prompt: void 0,
92
+ parseFn: void 0,
93
+ deprecated: void 0,
94
+ propagate: false,
95
+ ...rest,
96
+ aliases
97
+ };
98
+ }
99
+ /**
100
+ * Immutable flag schema builder.
101
+ *
102
+ * The type parameter `C` is a phantom that tracks the value type and presence
103
+ * through the fluent chain. Each modifier returns a **new** builder — the
104
+ * original is never mutated.
105
+ *
106
+ * @example
107
+ * ```ts
108
+ * const port = flag.number().default(8080);
109
+ * type Port = InferFlag<typeof port>; // number
110
+ *
111
+ * const region = flag.enum(['us', 'eu', 'ap']);
112
+ * type Region = InferFlag<typeof region>; // 'us' | 'eu' | 'ap' | undefined
113
+ * ```
114
+ */
115
+ var FlagBuilder = class FlagBuilder {
116
+ /** @internal Runtime schema descriptor. */
117
+ schema;
118
+ /**
119
+ * Create a flag builder from a pre-built schema descriptor.
120
+ *
121
+ * @param schema - Runtime descriptor seeding this builder's state.
122
+ */
123
+ constructor(schema) {
124
+ this.schema = schema;
125
+ }
126
+ /**
127
+ * Provide a default value. The flag becomes "always present" — handlers
128
+ * will never see `undefined`.
129
+ *
130
+ * The generic constraint `V extends C['valueType']` ensures the default
131
+ * matches the flag's declared type.
132
+ *
133
+ * @param value - Fallback value used when no source provides one.
134
+ * @returns The builder (for chaining).
135
+ *
136
+ * @example
137
+ * ```ts
138
+ * flag.number().default(8080).describe('Port to listen on')
139
+ *
140
+ * // $ mycli serve → port = 8080
141
+ * // $ mycli serve --port 443 → port = 443
142
+ * ```
143
+ */
144
+ default(value) {
145
+ return new FlagBuilder({
146
+ ...this.schema,
147
+ presence: "defaulted",
148
+ defaultValue: value
149
+ });
150
+ }
151
+ /**
152
+ * Mark the flag as required. If not resolved from any source the framework
153
+ * will emit a `ValidationError` before the action handler runs.
154
+ *
155
+ * @returns The builder (for chaining).
156
+ *
157
+ * @example
158
+ * ```ts
159
+ * flag.string().required().describe('Deploy target')
160
+ *
161
+ * // $ mycli deploy
162
+ * // # → Error: Missing required flag --target
163
+ * // $ mycli deploy --target staging
164
+ * // # → target = 'staging'
165
+ * ```
166
+ */
167
+ required() {
168
+ return new FlagBuilder({
169
+ ...this.schema,
170
+ presence: "required"
171
+ });
172
+ }
173
+ /**
174
+ * Add a short or long alias (e.g. `'f'` for `--force`, `'verbose'` as an
175
+ * alternative long name).
176
+ *
177
+ * @param name - Single-char short alias or alternative long name.
178
+ * @param options - Optional alias metadata. Hidden aliases remain parseable
179
+ * but are omitted from help, completions, and suggestions.
180
+ * @returns The builder (for chaining).
181
+ *
182
+ * @example
183
+ * ```ts
184
+ * flag.boolean().alias('v').describe('Enable verbose output')
185
+ *
186
+ * // $ mycli build -v → verbose = true
187
+ * // $ mycli build --verbose → verbose = true
188
+ * ```
189
+ */
190
+ alias(name, options) {
191
+ return new FlagBuilder({
192
+ ...this.schema,
193
+ aliases: [...this.schema.aliases, {
194
+ name,
195
+ hidden: options?.hidden ?? false
196
+ }]
197
+ });
198
+ }
199
+ /**
200
+ * Bind to an environment variable (resolved in v0.2+).
201
+ *
202
+ * @param varName - Environment variable name (e.g. `'PORT'`).
203
+ * @returns The builder (for chaining).
204
+ *
205
+ * @example
206
+ * ```ts
207
+ * flag.string().env('API_KEY').describe('Service API key')
208
+ *
209
+ * // $ API_KEY=sk-123 mycli request → apiKey = 'sk-123'
210
+ * // $ mycli request --api-key sk-456 → apiKey = 'sk-456' (CLI wins)
211
+ * ```
212
+ */
213
+ env(varName) {
214
+ return new FlagBuilder({
215
+ ...this.schema,
216
+ envVar: varName
217
+ });
218
+ }
219
+ /**
220
+ * Bind to a dotted config path (resolved in v0.2+).
221
+ *
222
+ * @param path - Dotted config key (e.g. `'deploy.region'`).
223
+ * @returns The builder (for chaining).
224
+ *
225
+ * @example
226
+ * ```ts
227
+ * flag.string().config('deploy.region').default('us-east-1')
228
+ * // Config file: { "deploy": { "region": "eu-west-1" } }
229
+ * // $ mycli deploy
230
+ * // # → region = 'eu-west-1' (from config)
231
+ * // $ mycli deploy --region ap-south-1
232
+ * // # → CLI flag wins
233
+ * ```
234
+ */
235
+ config(path) {
236
+ return new FlagBuilder({
237
+ ...this.schema,
238
+ configPath: path
239
+ });
240
+ }
241
+ /**
242
+ * Human-readable description shown in help output.
243
+ *
244
+ * @param description - Text displayed next to the flag in `--help`.
245
+ * @returns The builder (for chaining).
246
+ */
247
+ describe(description) {
248
+ return new FlagBuilder({
249
+ ...this.schema,
250
+ description
251
+ });
252
+ }
253
+ /**
254
+ * Attach interactive prompt configuration for v0.3+ resolution.
255
+ *
256
+ * When a flag value is not resolved from CLI, env, or config, the
257
+ * prompt engine uses this config to interactively ask the user.
258
+ * In non-interactive contexts (CI, piped stdin) prompts are skipped
259
+ * and resolution falls through to default or required validation.
260
+ *
261
+ * @param config - {@link PromptConfig} describing the interactive prompt.
262
+ * @returns The builder (for chaining).
263
+ *
264
+ * @example
265
+ * ```ts
266
+ * flag.string().prompt({ kind: 'input', message: 'Enter value:' })
267
+ *
268
+ * // $ mycli init → prompts "Enter value:" interactively
269
+ * // $ mycli init --name foo → skips prompt, uses CLI value
270
+ * ```
271
+ */
272
+ prompt(config) {
273
+ return new FlagBuilder({
274
+ ...this.schema,
275
+ prompt: config
276
+ });
277
+ }
278
+ /**
279
+ * Mark this flag as deprecated.
280
+ *
281
+ * When used, a warning is emitted to stderr. Help text shows
282
+ * `[deprecated]` or `[deprecated: <reason>]`.
283
+ *
284
+ * Does not change the flag's type-level config — it's metadata only.
285
+ *
286
+ * @param message - Optional migration reason/guidance.
287
+ * @returns The builder (for chaining).
288
+ *
289
+ * @example
290
+ * ```ts
291
+ * flag.string().deprecated('Use --target instead')
292
+ *
293
+ * // $ mycli deploy --dest staging
294
+ * // ⚠ --dest is deprecated: Use --target instead
295
+ * ```
296
+ */
297
+ deprecated(message) {
298
+ return new FlagBuilder({
299
+ ...this.schema,
300
+ deprecated: message ?? true
301
+ });
302
+ }
303
+ /**
304
+ * Mark this flag as propagated to subcommands.
305
+ *
306
+ * Propagated flags are automatically available to all descendant
307
+ * commands in a nested command tree. A child command that defines
308
+ * a flag with the same name shadows the propagated parent flag.
309
+ *
310
+ * Does not change the flag's type-level config — it's metadata only.
311
+ *
312
+ * @returns The builder (for chaining).
313
+ *
314
+ * @example
315
+ * ```ts
316
+ * flag.boolean().alias('v').propagate().describe('Enable verbose output')
317
+ *
318
+ * // $ mycli --verbose deploy staging
319
+ * // # → verbose = true in deploy handler
320
+ * // $ mycli deploy --verbose staging
321
+ * // # → same, inherited from parent
322
+ * ```
323
+ */
324
+ propagate() {
325
+ return new FlagBuilder({
326
+ ...this.schema,
327
+ propagate: true
328
+ });
329
+ }
330
+ /**
331
+ * Require an integer value. Composes with other numeric constraints.
332
+ *
333
+ * @param value - Whether to require an integer.
334
+ * @defaultValue `true`
335
+ * @returns The builder (for chaining).
336
+ *
337
+ * @example
338
+ * ```ts
339
+ * flag.number().int() // rejects 3.7, accepts 3
340
+ * flag.number({ int: true }).int(false) // re-allows non-integers
341
+ * ```
342
+ */
343
+ int(value = true) {
344
+ return new FlagBuilder({
345
+ ...this.schema,
346
+ numberConstraints: {
347
+ ...this.schema.numberConstraints,
348
+ int: value
349
+ }
350
+ });
351
+ }
352
+ /**
353
+ * Set an inclusive lower bound. Composes with other numeric constraints;
354
+ * a later call overrides an earlier `min` (including one from the options
355
+ * object).
356
+ *
357
+ * @param value - Inclusive minimum.
358
+ * @returns The builder (for chaining).
359
+ *
360
+ * @example
361
+ * ```ts
362
+ * flag.number().min(0) // rejects -1, accepts 0
363
+ * flag.number({ min: 0 }).min(5) // effective min is 5
364
+ * ```
365
+ */
366
+ min(value) {
367
+ const numberConstraints = {
368
+ ...this.schema.numberConstraints,
369
+ min: value
370
+ };
371
+ assertNumberConstraints(numberConstraints);
372
+ return new FlagBuilder({
373
+ ...this.schema,
374
+ numberConstraints
375
+ });
376
+ }
377
+ /**
378
+ * Set an inclusive upper bound. Composes with other numeric constraints;
379
+ * a later call overrides an earlier `max`.
380
+ *
381
+ * @param value - Inclusive maximum.
382
+ * @returns The builder (for chaining).
383
+ *
384
+ * @example
385
+ * ```ts
386
+ * flag.number().max(100) // rejects 101, accepts 100
387
+ * ```
388
+ */
389
+ max(value) {
390
+ const numberConstraints = {
391
+ ...this.schema.numberConstraints,
392
+ max: value
393
+ };
394
+ assertNumberConstraints(numberConstraints);
395
+ return new FlagBuilder({
396
+ ...this.schema,
397
+ numberConstraints
398
+ });
399
+ }
400
+ /**
401
+ * Require (or, with `false`, allow) a finite value. Finiteness is enforced
402
+ * by default, so this is mainly used as `.finite(false)` to re-allow
403
+ * `Infinity` / `-Infinity`.
404
+ *
405
+ * @param allow - Whether to require a finite value.
406
+ * @defaultValue `true`
407
+ * @returns The builder (for chaining).
408
+ *
409
+ * @example
410
+ * ```ts
411
+ * flag.number().finite(false) // accepts Infinity
412
+ * ```
413
+ */
414
+ finite(allow = true) {
415
+ return new FlagBuilder({
416
+ ...this.schema,
417
+ numberConstraints: {
418
+ ...this.schema.numberConstraints,
419
+ finite: allow
420
+ }
421
+ });
422
+ }
423
+ };
424
+ /**
425
+ * Flag schema factory. Call `flag.<kind>()` to create an immutable
426
+ * {@link FlagBuilder} with full type inference and safe modifier chaining.
427
+ */
428
+ const flag = {
429
+ string() {
430
+ return new FlagBuilder(createSchema("string"));
431
+ },
432
+ number(constraints) {
433
+ if (constraints !== void 0) assertNumberConstraints(constraints);
434
+ return new FlagBuilder(createSchema("number", constraints !== void 0 ? { numberConstraints: constraints } : {}));
435
+ },
436
+ boolean() {
437
+ return new FlagBuilder(createSchema("boolean", {
438
+ presence: "defaulted",
439
+ defaultValue: false
440
+ }));
441
+ },
442
+ enum(values) {
443
+ return new FlagBuilder(createSchema("enum", { enumValues: values }));
444
+ },
445
+ array(element) {
446
+ return new FlagBuilder(createSchema("array", { elementSchema: element.schema }));
447
+ },
448
+ custom(parseFn) {
449
+ return new FlagBuilder(createSchema("custom", { parseFn }));
450
+ }
451
+ };
452
+ //#endregion
453
+ export { FlagBuilder, createSchema, flag, getFlagAliasNames };
@@ -1 +1,41 @@
1
- function e(e){return{_handler:e}}export{e as middleware};
1
+ //#region src/core/schema/middleware.ts
2
+ /**
3
+ * Create a middleware definition.
4
+ *
5
+ * Middleware runs before the action handler and can add typed context,
6
+ * short-circuit execution, or wrap downstream processing.
7
+ *
8
+ * @param handler - Function receiving `{ args, flags, ctx, out, meta, next }`.
9
+ * Call `next(additions)` to continue the chain with added context.
10
+ * Omitting the `next()` call short-circuits (e.g., for auth guards).
11
+ * @returns {@linkcode Middleware} to attach via `CommandBuilder.middleware()`.
12
+ *
13
+ * @example
14
+ * ```ts
15
+ * // Auth guard — adds user to context or throws
16
+ * const auth = middleware(async ({ next }) => {
17
+ * const user = await getUser();
18
+ * if (!user) throw new CLIError('Not authenticated', { code: 'AUTH_REQUIRED' });
19
+ * return next({ user });
20
+ * });
21
+ *
22
+ * // Timing wrapper — measures downstream execution
23
+ * const timing = middleware(async ({ out, next }) => {
24
+ * const start = Date.now();
25
+ * await next({});
26
+ * out.info(`Done in ${Date.now() - start}ms`);
27
+ * });
28
+ *
29
+ * command('deploy')
30
+ * .middleware(timing)
31
+ * .middleware(auth)
32
+ * .action(({ ctx }) => {
33
+ * console.log(ctx.user.name); // typed!
34
+ * });
35
+ * ```
36
+ */
37
+ function middleware(handler) {
38
+ return { _handler: handler };
39
+ }
40
+ //#endregion
41
+ export { middleware };
@@ -1 +1,60 @@
1
- function e(e,t){if(Number.isNaN(e)||(t?.finite??!0)&&!Number.isFinite(e))return{kind:`finite`};if(t?.int===!0&&!Number.isInteger(e))return{kind:`int`};if(t?.min!==void 0&&e<t.min)return{kind:`min`,bound:t.min};if(t?.max!==void 0&&e>t.max)return{kind:`max`,bound:t.max}}function t(e){switch(e.kind){case`finite`:return`must be a finite number`;case`int`:return`must be an integer`;case`min`:return`must be >= ${e.bound}`;case`max`:return`must be <= ${e.bound}`}}function n(e){if(e.min!==void 0&&!Number.isFinite(e.min))throw RangeError(`number constraint 'min' must be a finite number (got ${e.min}); omit it for no lower bound`);if(e.max!==void 0&&!Number.isFinite(e.max))throw RangeError(`number constraint 'max' must be a finite number (got ${e.max}); omit it for no upper bound`);if(e.min!==void 0&&e.max!==void 0&&e.min>e.max)throw RangeError(`number constraint 'min' (${e.min}) must not exceed 'max' (${e.max})`)}export{n as assertNumberConstraints,t as describeNumberConstraintViolation,e as validateNumberConstraints};
1
+ /**
2
+ * Validate a numeric value against constraints.
3
+ *
4
+ * The single source of truth for numeric-constraint checks, shared by the
5
+ * parse and coerce paths. Checks run in a fixed order — **finite → int → min →
6
+ * max** — and the first failure is returned. `NaN` is always rejected as a
7
+ * `finite` violation, regardless of the `finite` option (a non-finite value can
8
+ * otherwise slip past `min`/`max`, since `NaN < x` and `NaN > x` are both false).
9
+ *
10
+ * @param value - The numeric value to validate.
11
+ * @param constraints - Constraints to enforce, or `undefined` for defaults.
12
+ * @returns The first {@link NumberConstraintViolation}, or `undefined` if valid.
13
+ */
14
+ function validateNumberConstraints(value, constraints) {
15
+ if (Number.isNaN(value)) return { kind: "finite" };
16
+ if ((constraints?.finite ?? true) && !Number.isFinite(value)) return { kind: "finite" };
17
+ if (constraints?.int === true && !Number.isInteger(value)) return { kind: "int" };
18
+ if (constraints?.min !== void 0 && value < constraints.min) return {
19
+ kind: "min",
20
+ bound: constraints.min
21
+ };
22
+ if (constraints?.max !== void 0 && value > constraints.max) return {
23
+ kind: "max",
24
+ bound: constraints.max
25
+ };
26
+ }
27
+ /**
28
+ * Render a terse, human-readable reason for a constraint violation.
29
+ *
30
+ * @param violation - The violation to describe.
31
+ * @returns A message suffix such as `must be an integer` or `must be >= 0`.
32
+ */
33
+ function describeNumberConstraintViolation(violation) {
34
+ switch (violation.kind) {
35
+ case "finite": return "must be a finite number";
36
+ case "int": return "must be an integer";
37
+ case "min": return `must be >= ${violation.bound}`;
38
+ case "max": return `must be <= ${violation.bound}`;
39
+ }
40
+ }
41
+ /**
42
+ * Assert that a constraints object is well-formed at schema-construction time.
43
+ *
44
+ * `min` / `max` must be finite — an infinite or `NaN` bound is meaningless (omit
45
+ * the field for "no bound") and would serialize to `null` in the emitted JSON
46
+ * Schema. An inverted range (`min > max`) is also rejected, since it makes the
47
+ * flag/arg permanently unsatisfiable. Throws so the misconfiguration surfaces
48
+ * where the flag/arg is declared, not at parse time.
49
+ *
50
+ * @param constraints - The constraints to validate.
51
+ * @throws RangeError if `min` or `max` is non-finite (`Infinity` / `-Infinity` /
52
+ * `NaN`), or if `min` exceeds `max`.
53
+ */
54
+ function assertNumberConstraints(constraints) {
55
+ if (constraints.min !== void 0 && !Number.isFinite(constraints.min)) throw new RangeError(`number constraint 'min' must be a finite number (got ${constraints.min}); omit it for no lower bound`);
56
+ if (constraints.max !== void 0 && !Number.isFinite(constraints.max)) throw new RangeError(`number constraint 'max' must be a finite number (got ${constraints.max}); omit it for no upper bound`);
57
+ if (constraints.min !== void 0 && constraints.max !== void 0 && constraints.min > constraints.max) throw new RangeError(`number constraint 'min' (${constraints.min}) must not exceed 'max' (${constraints.max})`);
58
+ }
59
+ //#endregion
60
+ export { assertNumberConstraints, describeNumberConstraintViolation, validateNumberConstraints };