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

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 (194) hide show
  1. package/CHANGELOG.md +217 -1
  2. package/dist/core/cli/dispatch.d.mts +6 -5
  3. package/dist/core/cli/dispatch.mjs +205 -1
  4. package/dist/core/cli/help-links.d.mts +0 -1
  5. package/dist/core/cli/help-links.mjs +41 -1
  6. package/dist/core/cli/index.d.mts +81 -8
  7. package/dist/core/cli/index.mjs +945 -1
  8. package/dist/core/cli/planner.d.mts +5 -1
  9. package/dist/core/cli/planner.mjs +300 -1
  10. package/dist/core/cli/plugin.d.mts +1 -1
  11. package/dist/core/cli/plugin.mjs +41 -1
  12. package/dist/core/cli/propagate.d.mts +1 -1
  13. package/dist/core/cli/propagate.mjs +57 -1
  14. package/dist/core/cli/root-help.d.mts +2 -1
  15. package/dist/core/cli/root-help.mjs +226 -4
  16. package/dist/core/cli/root-surface.d.mts +12 -1
  17. package/dist/core/cli/root-surface.mjs +28 -1
  18. package/dist/core/cli/runtime-preflight.d.mts +14 -2
  19. package/dist/core/cli/runtime-preflight.mjs +169 -1
  20. package/dist/core/completion/index.d.mts +0 -1
  21. package/dist/core/completion/index.mjs +90 -1
  22. package/dist/core/completion/shells/bash.d.mts +0 -1
  23. package/dist/core/completion/shells/bash.mjs +308 -3
  24. package/dist/core/completion/shells/fish.d.mts +0 -1
  25. package/dist/core/completion/shells/fish.mjs +162 -2
  26. package/dist/core/completion/shells/powershell.d.mts +0 -1
  27. package/dist/core/completion/shells/powershell.mjs +358 -2
  28. package/dist/core/completion/shells/shared.d.mts +13 -2
  29. package/dist/core/completion/shells/shared.mjs +176 -1
  30. package/dist/core/completion/shells/zsh.d.mts +0 -1
  31. package/dist/core/completion/shells/zsh.mjs +251 -2
  32. package/dist/core/config/index.d.mts +2 -3
  33. package/dist/core/config/index.mjs +246 -1
  34. package/dist/core/config/package-json.d.mts +27 -4
  35. package/dist/core/config/package-json.mjs +386 -3
  36. package/dist/core/errors/index.d.mts +3 -3
  37. package/dist/core/errors/index.mjs +87 -1
  38. package/dist/core/execution/index.d.mts +0 -1
  39. package/dist/core/execution/index.mjs +201 -1
  40. package/dist/core/help/ansi.d.mts +11 -5
  41. package/dist/core/help/ansi.mjs +121 -2
  42. package/dist/core/help/index.d.mts +22 -3
  43. package/dist/core/help/index.mjs +341 -7
  44. package/dist/core/help/theme.d.mts +68 -0
  45. package/dist/core/help/theme.mjs +46 -0
  46. package/dist/core/json-schema/index.d.mts +15 -2
  47. package/dist/core/json-schema/index.mjs +536 -12
  48. package/dist/core/json-schema/meta-descriptions.generated.d.mts +55 -38
  49. package/dist/core/json-schema/meta-descriptions.generated.mjs +102 -28
  50. package/dist/core/output/activity.d.mts +0 -1
  51. package/dist/core/output/activity.mjs +452 -1
  52. package/dist/core/output/bind.mjs +56 -1
  53. package/dist/core/output/contracts.d.mts +4 -5
  54. package/dist/core/output/contracts.mjs +57 -1
  55. package/dist/core/output/display-value.mjs +32 -1
  56. package/dist/core/output/index.d.mts +21 -1
  57. package/dist/core/output/index.mjs +442 -2
  58. package/dist/core/output/renderers.d.mts +0 -1
  59. package/dist/core/output/renderers.mjs +46 -1
  60. package/dist/core/output/writer.mjs +12 -1
  61. package/dist/core/parse/index.d.mts +40 -7
  62. package/dist/core/parse/index.mjs +727 -1
  63. package/dist/core/prompt/index.d.mts +1 -1
  64. package/dist/core/prompt/index.mjs +348 -6
  65. package/dist/core/resolve/args.d.mts +1 -1
  66. package/dist/core/resolve/args.mjs +105 -1
  67. package/dist/core/resolve/coerce.d.mts +1 -1
  68. package/dist/core/resolve/coerce.mjs +336 -1
  69. package/dist/core/resolve/config.mjs +19 -1
  70. package/dist/core/resolve/contracts.d.mts +9 -4
  71. package/dist/core/resolve/errors.d.mts +0 -1
  72. package/dist/core/resolve/errors.mjs +140 -2
  73. package/dist/core/resolve/flags.d.mts +7 -2
  74. package/dist/core/resolve/flags.mjs +251 -1
  75. package/dist/core/resolve/index.d.mts +2 -2
  76. package/dist/core/resolve/index.mjs +85 -1
  77. package/dist/core/resolve/property.d.mts +4 -2
  78. package/dist/core/resolve/property.mjs +53 -1
  79. package/dist/core/schema/activity.d.mts +31 -11
  80. package/dist/core/schema/arg.d.mts +3 -4
  81. package/dist/core/schema/arg.mjs +463 -1
  82. package/dist/core/schema/command.d.mts +26 -6
  83. package/dist/core/schema/command.mjs +726 -1
  84. package/dist/core/schema/flag.d.mts +485 -19
  85. package/dist/core/schema/flag.mjs +705 -1
  86. package/dist/core/schema/index.d.mts +4 -2
  87. package/dist/core/schema/middleware.d.mts +4 -3
  88. package/dist/core/schema/middleware.mjs +41 -1
  89. package/dist/core/schema/number-constraints.mjs +60 -1
  90. package/dist/core/schema/prompt.d.mts +6 -3
  91. package/dist/core/schema/run.d.mts +20 -1
  92. package/dist/core/schema/string-constraints.d.mts +97 -0
  93. package/dist/core/schema/string-constraints.mjs +69 -0
  94. package/dist/core/schema/value-parsers.d.mts +78 -0
  95. package/dist/core/schema/value-parsers.mjs +142 -0
  96. package/dist/core/schema-dsl/runtime.mjs +271 -2
  97. package/dist/core/schema-dsl/to-json-schema.d.mts +0 -1
  98. package/dist/core/schema-dsl/to-json-schema.mjs +74 -1
  99. package/dist/core/testkit/index.d.mts +0 -31
  100. package/dist/core/testkit/index.mjs +83 -1
  101. package/dist/index.d.mts +11 -6
  102. package/dist/index.mjs +22 -1
  103. package/dist/runtime/adapter.d.mts +38 -2
  104. package/dist/runtime/adapter.mjs +87 -1
  105. package/dist/runtime/auto.d.mts +2 -3
  106. package/dist/runtime/auto.mjs +45 -1
  107. package/dist/runtime/deno.d.mts +24 -5
  108. package/dist/runtime/deno.mjs +262 -2
  109. package/dist/runtime/detect.mjs +45 -1
  110. package/dist/runtime/index.d.mts +3 -4
  111. package/dist/runtime/node.d.mts +8 -2
  112. package/dist/runtime/node.mjs +220 -2
  113. package/dist/runtime/paths.mjs +48 -1
  114. package/dist/runtime.d.mts +4 -4
  115. package/dist/runtime.mjs +6 -1
  116. package/dist/strings.mjs +22 -1
  117. package/dist/testkit.d.mts +1 -0
  118. package/dist/testkit.mjs +5 -1
  119. package/dist/version.d.mts +7 -3
  120. package/dist/version.mjs +7 -1
  121. package/dreamcli.schema.json +36 -7
  122. package/package.json +45 -42
  123. package/dist/runtime/bun.d.mts +0 -28
  124. package/dist/runtime/bun.mjs +0 -1
  125. package/src/core/cli/dispatch.ts +0 -355
  126. package/src/core/cli/help-links.ts +0 -82
  127. package/src/core/cli/index.ts +0 -1614
  128. package/src/core/cli/planner.ts +0 -656
  129. package/src/core/cli/plugin.ts +0 -117
  130. package/src/core/cli/propagate.ts +0 -97
  131. package/src/core/cli/root-help.ts +0 -342
  132. package/src/core/cli/root-surface.ts +0 -87
  133. package/src/core/cli/runtime-preflight.ts +0 -456
  134. package/src/core/completion/index.ts +0 -138
  135. package/src/core/completion/shells/bash.ts +0 -459
  136. package/src/core/completion/shells/fish.ts +0 -259
  137. package/src/core/completion/shells/powershell.ts +0 -439
  138. package/src/core/completion/shells/shared.ts +0 -310
  139. package/src/core/completion/shells/zsh.ts +0 -381
  140. package/src/core/config/index.ts +0 -417
  141. package/src/core/config/package-json.ts +0 -560
  142. package/src/core/errors/index.ts +0 -193
  143. package/src/core/execution/index.ts +0 -265
  144. package/src/core/help/ansi.ts +0 -144
  145. package/src/core/help/index.ts +0 -563
  146. package/src/core/json-schema/index.ts +0 -910
  147. package/src/core/json-schema/meta-descriptions.generated.ts +0 -202
  148. package/src/core/output/activity.ts +0 -575
  149. package/src/core/output/bind.ts +0 -59
  150. package/src/core/output/contracts.ts +0 -194
  151. package/src/core/output/display-value.ts +0 -48
  152. package/src/core/output/index.ts +0 -704
  153. package/src/core/output/renderers.ts +0 -125
  154. package/src/core/output/writer.ts +0 -46
  155. package/src/core/parse/index.ts +0 -742
  156. package/src/core/prompt/index.ts +0 -623
  157. package/src/core/resolve/args.ts +0 -144
  158. package/src/core/resolve/coerce.ts +0 -500
  159. package/src/core/resolve/config.ts +0 -26
  160. package/src/core/resolve/contracts.ts +0 -135
  161. package/src/core/resolve/errors.ts +0 -227
  162. package/src/core/resolve/flags.ts +0 -269
  163. package/src/core/resolve/index.ts +0 -108
  164. package/src/core/resolve/property.ts +0 -99
  165. package/src/core/schema/activity.ts +0 -240
  166. package/src/core/schema/arg.ts +0 -841
  167. package/src/core/schema/command.ts +0 -1500
  168. package/src/core/schema/flag.ts +0 -917
  169. package/src/core/schema/index.ts +0 -96
  170. package/src/core/schema/middleware.ts +0 -171
  171. package/src/core/schema/number-constraints.ts +0 -153
  172. package/src/core/schema/prompt.ts +0 -171
  173. package/src/core/schema/run.ts +0 -198
  174. package/src/core/schema-dsl/index.ts +0 -109
  175. package/src/core/schema-dsl/parse.ts +0 -262
  176. package/src/core/schema-dsl/runtime.ts +0 -609
  177. package/src/core/schema-dsl/to-json-schema.ts +0 -102
  178. package/src/core/testkit/index.ts +0 -147
  179. package/src/index.ts +0 -181
  180. package/src/runtime/adapter.ts +0 -303
  181. package/src/runtime/auto.ts +0 -86
  182. package/src/runtime/bun.ts +0 -48
  183. package/src/runtime/deno.ts +0 -367
  184. package/src/runtime/detect.ts +0 -105
  185. package/src/runtime/index.ts +0 -21
  186. package/src/runtime/node-builtins.d.ts +0 -27
  187. package/src/runtime/node.ts +0 -289
  188. package/src/runtime/paths.ts +0 -64
  189. package/src/runtime/support.ts +0 -77
  190. package/src/runtime.ts +0 -31
  191. package/src/schema.ts +0 -16
  192. package/src/strings.ts +0 -34
  193. package/src/testkit.ts +0 -25
  194. package/src/version.ts +0 -20
@@ -1,11 +1,520 @@
1
- import{getFlagAliasNames as e}from"../schema/flag.mjs";import{parseSchema as t}from"../schema-dsl/runtime.mjs";import{nodeToJsonSchema as n}from"../schema-dsl/to-json-schema.mjs";import{definitionMetaSchemaDescriptions as r}from"./meta-descriptions.generated.mjs";function i(e){return{includeHidden:e?.includeHidden??!0,includePrompts:e?.includePrompts??!0}}const a=`https://cdn.jsdelivr.net/npm/@kjanat/dreamcli/dreamcli.schema.json`,o=`https://json-schema.org/draft/2020-12/schema`;function s(e){return typeof e==`object`&&!!e&&!Array.isArray(e)}function c(e,t){let n=i(t),r={$schema:a,name:e.name};return e.version!==void 0&&(r.version=e.version),e.description!==void 0&&(r.description=e.description),e.defaultCommand!==void 0&&(n.includeHidden||!e.defaultCommand.schema.hidden)&&(r.defaultCommand=e.defaultCommand.schema.name),r.commands=e.commands.filter(e=>n.includeHidden||!e.schema.hidden).map(e=>l(e.schema,n)),r}function l(e,t){let n={name:e.name};e.description!==void 0&&(n.description=e.description),e.aliases.length>0&&(n.aliases=[...e.aliases]),e.hidden&&(n.hidden=!0),e.examples.length>0&&(n.examples=e.examples.map(m));let r={};for(let[n,i]of Object.entries(e.flags))r[n]=u(i,t);return n.flags=r,n.args=e.args.map(d),n.commands=e.commands.filter(e=>t.includeHidden||!e.hidden).map(e=>l(e,t)),n}function u(t,n){let r={kind:t.kind,presence:t.presence};t.presence===`defaulted`&&w(t.defaultValue)&&(r.defaultValue=t.defaultValue);let i=e(t);return i.length>0&&(r.aliases=[...i]),t.envVar!==void 0&&(r.envVar=t.envVar),t.configPath!==void 0&&(r.configPath=t.configPath),t.description!==void 0&&(r.description=t.description),t.enumValues!==void 0&&(r.enumValues=[...t.enumValues]),t.elementSchema!==void 0&&(r.elementSchema=u(t.elementSchema,n)),n.includePrompts&&t.prompt!==void 0&&(r.prompt=f(t.prompt)),t.deprecated!==void 0&&(r.deprecated=t.deprecated),t.propagate&&(r.propagate=!0),r}function d(e){let{name:t,schema:n}=e,r={name:t,kind:n.kind,presence:n.presence};return n.variadic&&(r.variadic=!0),n.stdinMode&&(r.stdinMode=!0),n.presence===`defaulted`&&w(n.defaultValue)&&(r.defaultValue=n.defaultValue),n.description!==void 0&&(r.description=n.description),n.envVar!==void 0&&(r.envVar=n.envVar),n.enumValues!==void 0&&(r.enumValues=[...n.enumValues]),n.deprecated!==void 0&&(r.deprecated=n.deprecated),r}function f(e){let t={kind:e.kind,message:e.message};switch(e.kind){case`input`:e.placeholder!==void 0&&(t.placeholder=e.placeholder);break;case`select`:e.choices!==void 0&&(t.choices=e.choices.map(p));break;case`multiselect`:e.choices!==void 0&&(t.choices=e.choices.map(p)),e.min!==void 0&&(t.min=e.min),e.max!==void 0&&(t.max=e.max);break;case`confirm`:break}return t}function p(e){let t={value:e.value};return e.label!==void 0&&(t.label=e.label),e.description!==void 0&&(t.description=e.description),t}function m(e){let t={command:e.command};return e.description!==void 0&&(t.description=e.description),t}function h(e,t){let n=i(t);if(g(e))return{$schema:o,...v(e)};let r=[];for(let t of e.commands)!n.includeHidden&&t.schema.hidden||_(t.schema,``,r,n);if(r.length===1)return{$schema:o,...y(r[0]??{})};if(r.length>0){let t=e.defaultCommand!==void 0&&(n.includeHidden||!e.defaultCommand.schema.hidden)?e.defaultCommand.schema.name:void 0;return{$schema:o,oneOf:t===void 0?r:r.map(e=>b(e)===t?y(e):e)}}return{$schema:o,type:`object`}}function g(e){return`flags`in e&&`args`in e&&`middleware`in e&&`hasAction`in e}function _(e,t,n,r){let i=t?`${t}.${e.name}`:e.name;e.hasAction&&n.push(v(e,i));for(let t of e.commands)!r.includeHidden&&t.hidden||_(t,i,n,r)}function v(e,t){let n={},r=[];t!==void 0&&(n.command={const:t},r.push(`command`));for(let[t,i]of Object.entries(e.flags))n[t]=x(i),i.presence===`required`&&r.push(t);for(let t of e.args)n[t.name]=S(t.schema),t.schema.presence===`required`&&r.push(t.name);let i={type:`object`,properties:n,additionalProperties:!1};return r.length>0&&(i.required=r),i}function y(e){let t=e.properties,n=e.required,r=s(t)?{...t}:void 0,i=Array.isArray(n)?n.filter(e=>typeof e==`string`):void 0;r!==void 0&&delete r.command;let a=i?.filter(e=>e!==`command`),o={...e};return r!==void 0&&(o.properties=r),a!==void 0&&a.length>0?o.required=a:delete o.required,o}function b(e){let t=e.properties;if(!s(t))return;let n=t.command;return s(n)&&typeof n.const==`string`?n.const:void 0}function x(e){let t={};switch(e.kind){case`string`:t.type=`string`;break;case`number`:{let n=e.numberConstraints;t.type=n?.int===!0?`integer`:`number`,n?.min!==void 0&&(t.minimum=n.min),n?.max!==void 0&&(t.maximum=n.max);break}case`boolean`:t.type=`boolean`;break;case`enum`:t.type=`string`,e.enumValues!==void 0&&(t.enum=[...e.enumValues]);break;case`array`:t.type=`array`,e.elementSchema!==void 0&&(t.items=x(e.elementSchema));break;case`custom`:break}return e.description!==void 0&&(t.description=e.description),e.presence===`defaulted`&&w(e.defaultValue)&&(t.default=e.defaultValue),e.deprecated!==void 0&&(t.deprecated=e.deprecated),t}function S(e){let t=C(e),n=e.variadic?{type:`array`,items:t}:{...t};return e.description!==void 0&&(n.description=e.description),e.presence===`defaulted`&&w(e.defaultValue)&&(n.default=e.defaultValue),e.deprecated!==void 0&&(n.deprecated=e.deprecated),n}function C(e){switch(e.kind){case`string`:return{type:`string`};case`number`:{let t=e.numberConstraints,n={type:t?.int===!0?`integer`:`number`};return t?.min!==void 0&&(n.minimum=t.min),t?.max!==void 0&&(n.maximum=t.max),n}case`enum`:{let t={type:`string`};return e.enumValues!==void 0&&(t.enum=[...e.enumValues]),t}case`custom`:return{}}}function w(e,t=new WeakSet){if(e===void 0)return!1;if(e===null)return!0;let n=typeof e;if(n===`string`||n===`boolean`)return!0;if(n===`number`)return Number.isFinite(e);if(n===`function`||n===`symbol`||n===`bigint`)return!1;if(Array.isArray(e)){if(t.has(e))return!1;t.add(e);try{return e.every(e=>w(e,t))}finally{t.delete(e)}}if(n===`object`){let n=e;if(!T(n)||t.has(n))return!1;t.add(n);try{return Object.values(n).every(e=>w(e,t))}finally{t.delete(n)}}return!1}function T(e){let t=Object.getPrototypeOf(e);return t===Object.prototype||t===null}function E(e){return n(t(e))}function D(e,t){let n=e.properties,r=typeof n==`object`&&n&&T(n)&&t.properties!==void 0?Object.fromEntries(Object.entries(n).map(([e,n])=>{let r=t.properties?.[e];return r===void 0||typeof n!=`object`||!n||!T(n)?[e,n]:[e,D(n,r)]})):void 0;return{...e,...t.description===void 0?{}:{description:t.description},...r===void 0?{}:{properties:r}}}function O(e,t){let n=D(e,t.root),r=n.$defs;return typeof r!=`object`||!r||!T(r)?n:{...n,$defs:Object.fromEntries(Object.entries(r).map(([e,n])=>{let r=t.defs[e];return r===void 0||typeof n!=`object`||!n||!T(n)?[e,n]:[e,D(n,r)]}))}}const k=O({$schema:o,$id:a,title:`@kjanat/dreamcli definition schema`,description:`Describes the structure of a CLI built with dreamcli — commands, flags, args, types, constraints, env bindings, and prompts.`,...E(`{
2
- $schema: '${a}';
1
+ import { getFlagAliasNames } from "../schema/flag.mjs";
2
+ import { parseSchema } from "../schema-dsl/runtime.mjs";
3
+ import { nodeToJsonSchema } from "../schema-dsl/to-json-schema.mjs";
4
+ import { definitionMetaSchemaDescriptions } from "./meta-descriptions.generated.mjs";
5
+ //#region src/core/json-schema/index.ts
6
+ /**
7
+ * Apply defaults to optional {@link JsonSchemaOptions}.
8
+ *
9
+ * @param options - User-supplied schema generation options, or `undefined` for all defaults.
10
+ * @returns Fully resolved options with defaults applied.
11
+ */
12
+ function resolveOptions(options) {
13
+ return {
14
+ includeHidden: options?.includeHidden ?? true,
15
+ includePrompts: options?.includePrompts ?? true
16
+ };
17
+ }
18
+ /**
19
+ * `$schema` URL for definition documents.
20
+ *
21
+ * Resolves via the actual file path on the CDN (`dreamcli.schema.json`).
22
+ * For offline or local-first workflows, use
23
+ * `./node_modules/@kjanat/dreamcli/dreamcli.schema.json`.
24
+ */
25
+ const DEFINITION_SCHEMA_URL = "https://cdn.jsdelivr.net/npm/@kjanat/dreamcli/dreamcli.schema.json";
26
+ /** Meta-schema URL for JSON Schema draft 2020-12 (input validation). */
27
+ const JSON_SCHEMA_DRAFT = "https://json-schema.org/draft/2020-12/schema";
28
+ function isRecord(value) {
29
+ return typeof value === "object" && value !== null && !Array.isArray(value);
30
+ }
31
+ /**
32
+ * Generate a definition metadata document describing the CLI's structure.
33
+ *
34
+ * Walks the full command tree and produces a plain JSON-serializable object
35
+ * representing all commands, subcommands, flags, args, and metadata.
36
+ * Non-serializable runtime values (parse functions, middleware handlers,
37
+ * interactive resolvers) are omitted.
38
+ *
39
+ * @param schema - The CLI schema from `CLIBuilder.schema`.
40
+ * @param options - Generation options.
41
+ * @returns A plain object suitable for `JSON.stringify()`.
42
+ *
43
+ * @example
44
+ * ```ts
45
+ * const app = cli('myapp').version('1.0.0').command(deploy);
46
+ * const definition = generateSchema(app.schema);
47
+ * writeFileSync('cli-schema.json', JSON.stringify(definition, null, 2));
48
+ * ```
49
+ */
50
+ function generateSchema(schema, options) {
51
+ const opts = resolveOptions(options);
52
+ const result = {
53
+ $schema: DEFINITION_SCHEMA_URL,
54
+ name: schema.name
55
+ };
56
+ if (schema.version !== void 0) result.version = schema.version;
57
+ if (schema.description !== void 0) result.description = schema.description;
58
+ if (schema.defaultCommand !== void 0 && (opts.includeHidden || !schema.defaultCommand.schema.hidden)) result.defaultCommand = serializeCommand(schema.defaultCommand.schema, opts);
59
+ result.commands = schema.commands.filter((cmd) => opts.includeHidden || !cmd.schema.hidden).map((cmd) => serializeCommand(cmd.schema, opts));
60
+ return result;
61
+ }
62
+ /**
63
+ * Generate the definition metadata document for a single command.
64
+ *
65
+ * The per-command counterpart of {@link generateSchema}: the same
66
+ * JSON-serializable shape as one entry of its `commands` array (flags, args,
67
+ * subcommands, examples). Powers `--help` in `--json` mode and is useful for
68
+ * embedding one command's definition into custom tooling.
69
+ *
70
+ * @param schema - The command schema to serialize.
71
+ * @param options - Generation options.
72
+ * @returns A plain object suitable for `JSON.stringify()`.
73
+ */
74
+ function generateCommandSchema(schema, options) {
75
+ return serializeCommand(schema, resolveOptions(options));
76
+ }
77
+ /**
78
+ * Serialize a single {@link CommandSchema} into a plain object.
79
+ *
80
+ * @param schema - The command schema to serialize.
81
+ * @param opts - Resolved generation options (hidden/prompt inclusion).
82
+ * @returns JSON-serializable object representing the command.
83
+ */
84
+ function serializeCommand(schema, opts) {
85
+ const result = { name: schema.name };
86
+ if (schema.description !== void 0) result.description = schema.description;
87
+ if (schema.aliases.length > 0) result.aliases = [...schema.aliases];
88
+ if (schema.hidden) result.hidden = true;
89
+ if (schema.examples.length > 0) result.examples = schema.examples.map(serializeExample);
90
+ const flags = {};
91
+ for (const [name, flagDef] of Object.entries(schema.flags)) flags[name] = serializeFlag(flagDef, opts);
92
+ result.flags = flags;
93
+ result.args = schema.args.map(serializeArgEntry);
94
+ result.commands = schema.commands.filter((cmd) => opts.includeHidden || !cmd.hidden).map((cmd) => serializeCommand(cmd, opts));
95
+ return result;
96
+ }
97
+ /**
98
+ * Serialize a {@link FlagSchema} into a plain object.
99
+ *
100
+ * @param schema - The flag schema to serialize.
101
+ * @param opts - Resolved generation options (prompt inclusion).
102
+ * @returns JSON-serializable object representing the flag.
103
+ */
104
+ function serializeFlag(schema, opts) {
105
+ const result = {
106
+ kind: schema.kind,
107
+ presence: schema.presence
108
+ };
109
+ if (schema.presence === "defaulted" && isJsonSerializable(schema.defaultValue)) result.defaultValue = schema.defaultValue;
110
+ const visibleAliases = getFlagAliasNames(schema);
111
+ if (visibleAliases.length > 0) result.aliases = [...visibleAliases];
112
+ if (schema.envVar !== void 0) result.envVar = schema.envVar;
113
+ if (schema.configPath !== void 0) result.configPath = schema.configPath;
114
+ if (schema.description !== void 0) result.description = schema.description;
115
+ if (schema.enumValues !== void 0) result.enumValues = [...schema.enumValues];
116
+ if (schema.elementSchema !== void 0) result.elementSchema = serializeFlag(schema.elementSchema, opts);
117
+ if (opts.includePrompts && schema.prompt !== void 0) result.prompt = serializePrompt(schema.prompt);
118
+ if (schema.deprecated !== void 0) result.deprecated = schema.deprecated;
119
+ if (schema.propagate) result.propagate = true;
120
+ if (schema.negation !== void 0) {
121
+ const negation = {};
122
+ if (schema.negation.alias !== void 0) negation.alias = schema.negation.alias;
123
+ if (schema.negation.hidden) negation.hidden = true;
124
+ result.negation = negation;
125
+ }
126
+ if (schema.duplicates !== "last") result.duplicates = schema.duplicates;
127
+ return result;
128
+ }
129
+ /**
130
+ * Serialize a {@link CommandArgEntry} into a plain object.
131
+ *
132
+ * @param entry - The positional arg entry (name + {@link ArgSchema}).
133
+ * @returns JSON-serializable object representing the arg.
134
+ */
135
+ function serializeArgEntry(entry) {
136
+ const { name, schema } = entry;
137
+ const result = {
138
+ name,
139
+ kind: schema.kind,
140
+ presence: schema.presence
141
+ };
142
+ if (schema.variadic) result.variadic = true;
143
+ if (schema.stdinMode) result.stdinMode = true;
144
+ if (schema.presence === "defaulted" && isJsonSerializable(schema.defaultValue)) result.defaultValue = schema.defaultValue;
145
+ if (schema.description !== void 0) result.description = schema.description;
146
+ if (schema.envVar !== void 0) result.envVar = schema.envVar;
147
+ if (schema.enumValues !== void 0) result.enumValues = [...schema.enumValues];
148
+ if (schema.deprecated !== void 0) result.deprecated = schema.deprecated;
149
+ return result;
150
+ }
151
+ /**
152
+ * Serialize a {@link PromptConfig} into a plain object.
153
+ *
154
+ * @param prompt - The prompt configuration to serialize.
155
+ * @returns JSON-serializable object representing the prompt.
156
+ */
157
+ function serializePrompt(prompt) {
158
+ const result = {
159
+ kind: prompt.kind,
160
+ message: prompt.message
161
+ };
162
+ switch (prompt.kind) {
163
+ case "input":
164
+ if (prompt.placeholder !== void 0) result.placeholder = prompt.placeholder;
165
+ break;
166
+ case "select":
167
+ if (prompt.choices !== void 0) result.choices = prompt.choices.map(serializeChoice);
168
+ break;
169
+ case "multiselect":
170
+ if (prompt.choices !== void 0) result.choices = prompt.choices.map(serializeChoice);
171
+ if (prompt.min !== void 0) result.min = prompt.min;
172
+ if (prompt.max !== void 0) result.max = prompt.max;
173
+ break;
174
+ case "confirm": break;
175
+ }
176
+ return result;
177
+ }
178
+ /**
179
+ * Serialize a {@link SelectChoice} into a plain object.
180
+ *
181
+ * @param choice - The select/multiselect choice to serialize.
182
+ * @returns JSON-serializable object with value, optional label and description.
183
+ */
184
+ function serializeChoice(choice) {
185
+ const result = { value: choice.value };
186
+ if (choice.label !== void 0) result.label = choice.label;
187
+ if (choice.description !== void 0) result.description = choice.description;
188
+ return result;
189
+ }
190
+ /**
191
+ * Serialize a {@link CommandExample} into a plain object.
192
+ *
193
+ * @param example - The example to serialize.
194
+ * @returns JSON-serializable object with command and optional description.
195
+ */
196
+ function serializeExample(example) {
197
+ const result = { command: example.command };
198
+ if (example.description !== void 0) result.description = example.description;
199
+ return result;
200
+ }
201
+ /**
202
+ * Generate a JSON Schema (draft 2020-12) for validating CLI input as JSON.
203
+ *
204
+ * Accepts either a full {@link CLISchema} (producing a discriminated union
205
+ * across all commands) or a single {@link CommandSchema} (producing a flat
206
+ * object schema for that command's flags and args).
207
+ *
208
+ * **Single command** — produces an object schema where flag/arg names are
209
+ * properties with appropriate JSON Schema types.
210
+ *
211
+ * **Multi-command CLI** — produces a `oneOf` discriminated union with a
212
+ * `command` property identifying each branch. Nested subcommands use
213
+ * dot-delimited paths (e.g. `"deploy.rollback"`).
214
+ *
215
+ * Only commands with action handlers are included (group commands without
216
+ * actions are not directly invocable and are skipped).
217
+ *
218
+ * @param schema - A CLI schema or a single command schema.
219
+ * @param options - Generation options.
220
+ * @returns A JSON Schema object suitable for `JSON.stringify()`.
221
+ *
222
+ * @example
223
+ * ```ts
224
+ * // Validate a config file against the CLI's input shape
225
+ * const inputSchema = generateInputSchema(app.schema);
226
+ * writeFileSync('input-schema.json', JSON.stringify(inputSchema, null, 2));
227
+ * ```
228
+ */
229
+ function generateInputSchema(schema, options) {
230
+ const opts = resolveOptions(options);
231
+ if (isCommandSchema(schema)) return {
232
+ $schema: JSON_SCHEMA_DRAFT,
233
+ ...commandToInputSchema(schema)
234
+ };
235
+ const branches = [];
236
+ for (const cmd of schema.commands) {
237
+ if (!opts.includeHidden && cmd.schema.hidden) continue;
238
+ collectInputBranches(cmd.schema, "", branches, opts);
239
+ }
240
+ if (branches.length === 1) {
241
+ const branch = stripCommandDiscriminator(branches[0] ?? {});
242
+ return {
243
+ $schema: JSON_SCHEMA_DRAFT,
244
+ ...branch
245
+ };
246
+ }
247
+ if (branches.length > 0) {
248
+ const defaultBranchName = schema.defaultCommand !== void 0 && (opts.includeHidden || !schema.defaultCommand.schema.hidden) ? schema.defaultCommand.schema.name : void 0;
249
+ const normalizedBranches = defaultBranchName !== void 0 ? branches.map((branch) => getBranchCommandDiscriminator(branch) === defaultBranchName ? stripCommandDiscriminator(branch) : branch) : branches;
250
+ return {
251
+ $schema: JSON_SCHEMA_DRAFT,
252
+ oneOf: normalizedBranches
253
+ };
254
+ }
255
+ return {
256
+ $schema: JSON_SCHEMA_DRAFT,
257
+ type: "object"
258
+ };
259
+ }
260
+ /**
261
+ * Discriminate between CLISchema and CommandSchema at runtime.
262
+ *
263
+ * Use a command-only field combination rather than a single shape check:
264
+ * command schemas always carry flags, args, middleware, and hasAction, while
265
+ * the CLI root schema does not expose that execution surface.
266
+ */
267
+ function isCommandSchema(schema) {
268
+ return "flags" in schema && "args" in schema && "middleware" in schema && "hasAction" in schema;
269
+ }
270
+ /** Recursively collect input schema branches for all invocable commands. */
271
+ function collectInputBranches(schema, prefix, branches, opts) {
272
+ const path = prefix ? `${prefix}.${schema.name}` : schema.name;
273
+ if (schema.hasAction) branches.push(commandToInputSchema(schema, path));
274
+ for (const sub of schema.commands) {
275
+ if (!opts.includeHidden && sub.hidden) continue;
276
+ collectInputBranches(sub, path, branches, opts);
277
+ }
278
+ }
279
+ /**
280
+ * Create a JSON Schema object for a single command's flags + args.
281
+ *
282
+ * When `commandPath` is provided, a `command` const discriminator property
283
+ * is added and required (used in multi-command oneOf schemas).
284
+ */
285
+ function commandToInputSchema(schema, commandPath) {
286
+ const properties = {};
287
+ const required = [];
288
+ if (commandPath !== void 0) {
289
+ properties.command = { const: commandPath };
290
+ required.push("command");
291
+ }
292
+ for (const [name, flagDef] of Object.entries(schema.flags)) {
293
+ properties[name] = flagToJsonSchemaType(flagDef);
294
+ if (flagDef.presence === "required") required.push(name);
295
+ }
296
+ for (const entry of schema.args) {
297
+ properties[entry.name] = argToJsonSchemaType(entry.schema);
298
+ if (entry.schema.presence === "required") required.push(entry.name);
299
+ }
300
+ const result = {
301
+ type: "object",
302
+ properties,
303
+ additionalProperties: false
304
+ };
305
+ if (required.length > 0) result.required = required;
306
+ return result;
307
+ }
308
+ /** @internal */
309
+ function stripCommandDiscriminator(branch) {
310
+ const propertiesValue = branch.properties;
311
+ const requiredValue = branch.required;
312
+ const properties = isRecord(propertiesValue) ? { ...propertiesValue } : void 0;
313
+ const required = Array.isArray(requiredValue) ? requiredValue.filter((value) => typeof value === "string") : void 0;
314
+ if (properties !== void 0) delete properties.command;
315
+ const cleanedRequired = required?.filter((value) => value !== "command");
316
+ const result = { ...branch };
317
+ if (properties !== void 0) result.properties = properties;
318
+ if (cleanedRequired !== void 0 && cleanedRequired.length > 0) result.required = cleanedRequired;
319
+ else delete result.required;
320
+ return result;
321
+ }
322
+ function getBranchCommandDiscriminator(branch) {
323
+ const propertiesValue = branch.properties;
324
+ if (!isRecord(propertiesValue)) return;
325
+ const commandValue = propertiesValue.command;
326
+ return isRecord(commandValue) && typeof commandValue.const === "string" ? commandValue.const : void 0;
327
+ }
328
+ function flagToJsonSchemaType(schema) {
329
+ const result = {};
330
+ switch (schema.kind) {
331
+ case "string": {
332
+ result.type = "string";
333
+ const constraints = schema.stringConstraints;
334
+ const minLength = constraints?.nonEmpty === true ? Math.max(constraints.minLength ?? 1, 1) : constraints?.minLength;
335
+ if (minLength !== void 0) result.minLength = minLength;
336
+ if (constraints?.maxLength !== void 0) result.maxLength = constraints.maxLength;
337
+ if (constraints?.pattern !== void 0) result.pattern = constraints.pattern.source;
338
+ break;
339
+ }
340
+ case "number": {
341
+ const constraints = schema.numberConstraints;
342
+ result.type = constraints?.int === true ? "integer" : "number";
343
+ if (constraints?.min !== void 0) result.minimum = constraints.min;
344
+ if (constraints?.max !== void 0) result.maximum = constraints.max;
345
+ break;
346
+ }
347
+ case "boolean":
348
+ result.type = "boolean";
349
+ break;
350
+ case "enum":
351
+ result.type = "string";
352
+ if (schema.enumValues !== void 0) result.enum = [...schema.enumValues];
353
+ break;
354
+ case "array":
355
+ result.type = "array";
356
+ if (schema.elementSchema !== void 0) result.items = flagToJsonSchemaType(schema.elementSchema);
357
+ break;
358
+ case "custom": break;
359
+ case "count":
360
+ result.type = "integer";
361
+ result.minimum = 0;
362
+ break;
363
+ case "keyValue":
364
+ result.type = "object";
365
+ result.additionalProperties = { type: "string" };
366
+ break;
367
+ }
368
+ if (schema.description !== void 0) result.description = schema.description;
369
+ if (schema.presence === "defaulted" && isJsonSerializable(schema.defaultValue)) result.default = schema.defaultValue;
370
+ if (schema.deprecated !== void 0) result.deprecated = schema.deprecated;
371
+ return result;
372
+ }
373
+ function argToJsonSchemaType(schema) {
374
+ const kind = argKindToType(schema);
375
+ const result = schema.variadic ? {
376
+ type: "array",
377
+ items: kind
378
+ } : { ...kind };
379
+ if (schema.description !== void 0) result.description = schema.description;
380
+ if (schema.presence === "defaulted" && isJsonSerializable(schema.defaultValue)) result.default = schema.defaultValue;
381
+ if (schema.deprecated !== void 0) result.deprecated = schema.deprecated;
382
+ return result;
383
+ }
384
+ /** Map an arg's kind to a JSON Schema type fragment. */
385
+ function argKindToType(schema) {
386
+ switch (schema.kind) {
387
+ case "string": return { type: "string" };
388
+ case "number": {
389
+ const constraints = schema.numberConstraints;
390
+ const result = { type: constraints?.int === true ? "integer" : "number" };
391
+ if (constraints?.min !== void 0) result.minimum = constraints.min;
392
+ if (constraints?.max !== void 0) result.maximum = constraints.max;
393
+ return result;
394
+ }
395
+ case "enum": {
396
+ const result = { type: "string" };
397
+ if (schema.enumValues !== void 0) result.enum = [...schema.enumValues];
398
+ return result;
399
+ }
400
+ case "custom": return {};
401
+ }
402
+ }
403
+ /**
404
+ * Check whether a value can survive a JSON round-trip.
405
+ *
406
+ * Returns `false` for functions, symbols, bigints, non-finite numbers, and
407
+ * objects containing them. Used to guard default-value inclusion in
408
+ * serialized output.
409
+ */
410
+ function isJsonSerializable(value, seen = /* @__PURE__ */ new WeakSet()) {
411
+ if (value === void 0) return false;
412
+ if (value === null) return true;
413
+ const t = typeof value;
414
+ if (t === "string" || t === "boolean") return true;
415
+ if (t === "number") return Number.isFinite(value);
416
+ if (t === "function" || t === "symbol" || t === "bigint") return false;
417
+ if (Array.isArray(value)) {
418
+ if (seen.has(value)) return false;
419
+ seen.add(value);
420
+ try {
421
+ return value.every((entry) => isJsonSerializable(entry, seen));
422
+ } finally {
423
+ seen.delete(value);
424
+ }
425
+ }
426
+ if (t === "object") {
427
+ const objectValue = value;
428
+ if (!isPlainJsonObject(objectValue)) return false;
429
+ if (seen.has(objectValue)) return false;
430
+ seen.add(objectValue);
431
+ try {
432
+ return Object.values(objectValue).every((entry) => isJsonSerializable(entry, seen));
433
+ } finally {
434
+ seen.delete(objectValue);
435
+ }
436
+ }
437
+ return false;
438
+ }
439
+ /**
440
+ * Check whether an object is a plain JSON object.
441
+ *
442
+ * Accepts objects with `Object.prototype` or a null prototype, which are the
443
+ * only object shapes that round-trip through JSON as structural objects.
444
+ *
445
+ * @internal
446
+ */
447
+ function isPlainJsonObject(value) {
448
+ const proto = Object.getPrototypeOf(value);
449
+ return proto === Object.prototype || proto === null;
450
+ }
451
+ /** Convert a DSL string to a JSON Schema object definition. */
452
+ function def(source) {
453
+ return nodeToJsonSchema(parseSchema(source));
454
+ }
455
+ function withDefinitionDescriptions(schema, descriptions) {
456
+ const propertiesValue = schema.properties;
457
+ const describedProperties = typeof propertiesValue === "object" && propertiesValue !== null && isPlainJsonObject(propertiesValue) && descriptions.properties !== void 0 ? Object.fromEntries(Object.entries(propertiesValue).map(([name, propertySchema]) => {
458
+ const propertyDescriptions = descriptions.properties?.[name];
459
+ if (propertyDescriptions === void 0 || typeof propertySchema !== "object" || propertySchema === null || !isPlainJsonObject(propertySchema)) return [name, propertySchema];
460
+ return [name, withDefinitionDescriptions(propertySchema, propertyDescriptions)];
461
+ })) : void 0;
462
+ return {
463
+ ...schema,
464
+ ...descriptions.description !== void 0 ? { description: descriptions.description } : {},
465
+ ...describedProperties !== void 0 ? { properties: describedProperties } : {}
466
+ };
467
+ }
468
+ function withDefinitionMetaSchemaDescriptions(schema, descriptions) {
469
+ const describedRoot = withDefinitionDescriptions(schema, descriptions.root);
470
+ const defsValue = describedRoot.$defs;
471
+ if (typeof defsValue !== "object" || defsValue === null || !isPlainJsonObject(defsValue)) return describedRoot;
472
+ return {
473
+ ...describedRoot,
474
+ $defs: Object.fromEntries(Object.entries(defsValue).map(([name, defSchema]) => {
475
+ const defDescriptions = descriptions.defs[name];
476
+ if (defDescriptions === void 0 || typeof defSchema !== "object" || defSchema === null || !isPlainJsonObject(defSchema)) return [name, defSchema];
477
+ return [name, withDefinitionDescriptions(defSchema, defDescriptions)];
478
+ }))
479
+ };
480
+ }
481
+ /**
482
+ * JSON Schema (draft 2020-12) that validates the output of {@link generateSchema}.
483
+ *
484
+ * Each `$defs` entry is defined once as a schema DSL string — the DSL
485
+ * parser produces a runtime AST, and {@link nodeToJsonSchema} converts
486
+ * that AST to a JSON Schema fragment. No probe fixtures, no override
487
+ * maps, no manually maintained type definitions.
488
+ *
489
+ * Hosted at {@link DEFINITION_SCHEMA_URL} for `$schema` resolution. Also
490
+ * exported so tooling can validate definition documents without a network
491
+ * round-trip.
492
+ *
493
+ * @example
494
+ * ```ts
495
+ * import Ajv from 'ajv/dist/2020';
496
+ * import { definitionMetaSchema, generateSchema } from '@kjanat/dreamcli';
497
+ *
498
+ * const ajv = new Ajv();
499
+ * const validate = ajv.compile(definitionMetaSchema);
500
+ * const valid = validate(generateSchema(myCli.schema));
501
+ * ```
502
+ */
503
+ const definitionMetaSchema = withDefinitionMetaSchemaDescriptions({
504
+ $schema: JSON_SCHEMA_DRAFT,
505
+ $id: DEFINITION_SCHEMA_URL,
506
+ title: "@kjanat/dreamcli definition schema",
507
+ description: "Describes the structure of a CLI built with dreamcli — commands, flags, args, types, constraints, env bindings, and prompts.",
508
+ ...def(`{
509
+ $schema: '${DEFINITION_SCHEMA_URL}';
3
510
  name: string;
4
511
  version?: string;
5
512
  description?: string;
6
- defaultCommand?: string;
513
+ defaultCommand?: @command;
7
514
  commands: @command[]
8
- }`),$defs:{command:E(`{
515
+ }`),
516
+ $defs: {
517
+ command: def(`{
9
518
  name: string;
10
519
  description?: string;
11
520
  aliases?: string[];
@@ -14,8 +523,9 @@ import{getFlagAliasNames as e}from"../schema/flag.mjs";import{parseSchema as t}f
14
523
  flags: Record<string, @flag>;
15
524
  args: @arg[];
16
525
  commands: @command[]
17
- }`),flag:E(`{
18
- kind: 'string' | 'number' | 'boolean' | 'enum' | 'array' | 'custom';
526
+ }`),
527
+ flag: def(`{
528
+ kind: 'string' | 'number' | 'boolean' | 'enum' | 'array' | 'custom' | 'count' | 'keyValue';
19
529
  presence: 'optional' | 'required' | 'defaulted';
20
530
  defaultValue?: unknown;
21
531
  aliases?: string[];
@@ -26,8 +536,15 @@ import{getFlagAliasNames as e}from"../schema/flag.mjs";import{parseSchema as t}f
26
536
  elementSchema?: @flag;
27
537
  prompt?: @prompt;
28
538
  deprecated?: string | true;
29
- propagate?: true
30
- }`),arg:E(`{
539
+ propagate?: true;
540
+ negation?: @negation;
541
+ duplicates?: 'last' | 'first' | 'error'
542
+ }`),
543
+ negation: def(`{
544
+ alias?: string;
545
+ hidden?: true
546
+ }`),
547
+ arg: def(`{
31
548
  name: string;
32
549
  kind: 'string' | 'number' | 'enum' | 'custom';
33
550
  presence: 'required' | 'optional' | 'defaulted';
@@ -38,18 +555,25 @@ import{getFlagAliasNames as e}from"../schema/flag.mjs";import{parseSchema as t}f
38
555
  envVar?: string;
39
556
  enumValues?: string[];
40
557
  deprecated?: string | true
41
- }`),prompt:E(`{
558
+ }`),
559
+ prompt: def(`{
42
560
  kind: 'confirm' | 'input' | 'select' | 'multiselect';
43
561
  message: string;
44
562
  placeholder?: string;
45
563
  choices?: @choice[];
46
564
  min?: integer;
47
565
  max?: integer
48
- }`),choice:E(`{
566
+ }`),
567
+ choice: def(`{
49
568
  value: string;
50
569
  label?: string;
51
570
  description?: string
52
- }`),example:E(`{
571
+ }`),
572
+ example: def(`{
53
573
  command: string;
54
574
  description?: string
55
- }`)}},r);export{k as definitionMetaSchema,h as generateInputSchema,c as generateSchema};
575
+ }`)
576
+ }
577
+ }, definitionMetaSchemaDescriptions);
578
+ //#endregion
579
+ export { definitionMetaSchema, generateCommandSchema, generateInputSchema, generateSchema };