@kjanat/dreamcli 0.0.0 → 1.0.0

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 (101) hide show
  1. package/CHANGELOG.md +667 -0
  2. package/LICENSE +22 -0
  3. package/README.md +370 -0
  4. package/dist/adapter-B37d9oia.mjs +1 -0
  5. package/dist/adapter-CCSh5juX.cjs +1 -0
  6. package/dist/adapter-DIwO-q7K.d.mts +3447 -0
  7. package/dist/adapter-DQnxxLiE.d.cts +3447 -0
  8. package/dist/auto-5cjZaF9V.mjs +3 -0
  9. package/dist/auto-B0eSgoDw.cjs +3 -0
  10. package/dist/index.cjs +10 -0
  11. package/dist/index.d.cts +920 -0
  12. package/dist/index.d.mts +920 -0
  13. package/dist/index.mjs +10 -0
  14. package/dist/runtime.cjs +1 -0
  15. package/dist/runtime.d.cts +253 -0
  16. package/dist/runtime.d.mts +253 -0
  17. package/dist/runtime.mjs +1 -0
  18. package/dist/testkit-CEDYPvhF.mjs +14 -0
  19. package/dist/testkit-ClIKijxC.cjs +14 -0
  20. package/dist/testkit.cjs +1 -0
  21. package/dist/testkit.d.cts +25 -0
  22. package/dist/testkit.d.mts +25 -0
  23. package/dist/testkit.mjs +1 -0
  24. package/examples/basic.ts +33 -0
  25. package/examples/gh/package.json +48 -0
  26. package/examples/gh/src/commands/auth.ts +66 -0
  27. package/examples/gh/src/commands/issue.ts +120 -0
  28. package/examples/gh/src/commands/pr.ts +125 -0
  29. package/examples/gh/src/data/issues.yaml +5 -0
  30. package/examples/gh/src/data/issues.yaml.d.ts +17 -0
  31. package/examples/gh/src/data/pull-requests.yaml +7 -0
  32. package/examples/gh/src/data/pull-requests.yaml.d.ts +18 -0
  33. package/examples/gh/src/lib/auth.ts +37 -0
  34. package/examples/gh/src/lib/utils.ts +17 -0
  35. package/examples/gh/src/main.test.ts +306 -0
  36. package/examples/gh/src/main.ts +38 -0
  37. package/examples/gh/tsconfig.json +37 -0
  38. package/examples/interactive.ts +66 -0
  39. package/examples/json-mode.ts +93 -0
  40. package/examples/middleware.ts +71 -0
  41. package/examples/multi-command.ts +66 -0
  42. package/examples/spinner-progress.ts +49 -0
  43. package/examples/testing.ts +145 -0
  44. package/examples/tsconfig.json +17 -0
  45. package/package.json +123 -2
  46. package/src/core/cli/AGENTS.md +72 -0
  47. package/src/core/cli/dispatch.ts +282 -0
  48. package/src/core/cli/index.ts +1425 -0
  49. package/src/core/cli/plugin.ts +111 -0
  50. package/src/core/cli/propagate.ts +97 -0
  51. package/src/core/cli/root-help.ts +269 -0
  52. package/src/core/cli/root-surface.ts +74 -0
  53. package/src/core/completion/AGENTS.md +72 -0
  54. package/src/core/completion/completion-test-helpers.ts +48 -0
  55. package/src/core/completion/index.ts +82 -0
  56. package/src/core/completion/shells/bash.ts +456 -0
  57. package/src/core/completion/shells/fish.ts +24 -0
  58. package/src/core/completion/shells/powershell.ts +24 -0
  59. package/src/core/completion/shells/shared.ts +283 -0
  60. package/src/core/completion/shells/zsh.ts +376 -0
  61. package/src/core/config/index.ts +404 -0
  62. package/src/core/config/package-json.ts +216 -0
  63. package/src/core/errors/index.ts +178 -0
  64. package/src/core/help/index.ts +559 -0
  65. package/src/core/json-schema/index.ts +705 -0
  66. package/src/core/output/AGENTS.md +69 -0
  67. package/src/core/output/activity.ts +568 -0
  68. package/src/core/output/display-value.ts +48 -0
  69. package/src/core/output/index.ts +704 -0
  70. package/src/core/output/writer.ts +46 -0
  71. package/src/core/parse/index.ts +643 -0
  72. package/src/core/prompt/index.ts +566 -0
  73. package/src/core/resolve/AGENTS.md +69 -0
  74. package/src/core/resolve/index.ts +1331 -0
  75. package/src/core/schema/AGENTS.md +61 -0
  76. package/src/core/schema/activity.ts +237 -0
  77. package/src/core/schema/arg.ts +707 -0
  78. package/src/core/schema/command.ts +1263 -0
  79. package/src/core/schema/flag.ts +651 -0
  80. package/src/core/schema/index.ts +82 -0
  81. package/src/core/schema/middleware.ts +171 -0
  82. package/src/core/schema/prompt.ts +148 -0
  83. package/src/core/schema/run.ts +187 -0
  84. package/src/core/testkit/AGENTS.md +55 -0
  85. package/src/core/testkit/index.ts +365 -0
  86. package/src/index.ts +164 -0
  87. package/src/runtime/AGENTS.md +74 -0
  88. package/src/runtime/adapter.ts +300 -0
  89. package/src/runtime/auto.ts +110 -0
  90. package/src/runtime/bun.ts +55 -0
  91. package/src/runtime/deno.ts +355 -0
  92. package/src/runtime/detect.ts +102 -0
  93. package/src/runtime/index.ts +21 -0
  94. package/src/runtime/node-builtins.d.ts +23 -0
  95. package/src/runtime/node.ts +278 -0
  96. package/src/runtime/paths.ts +56 -0
  97. package/src/runtime/support.ts +119 -0
  98. package/src/runtime/test-helpers.ts +40 -0
  99. package/src/runtime.ts +31 -0
  100. package/src/testkit.ts +23 -0
  101. package/src/version.ts +20 -0
@@ -0,0 +1,282 @@
1
+ /**
2
+ * Recursive command dispatch for nested command trees.
3
+ *
4
+ * Walks argv segments, matching command names at each level of the tree.
5
+ * Returns a discriminated union describing the dispatch outcome: a matched
6
+ * command with its ancestor path, a group that needs a subcommand, or an
7
+ * unknown command with suggestion candidates.
8
+ *
9
+ * @module dreamcli/core/cli/dispatch
10
+ * @internal
11
+ */
12
+
13
+ import type { CommandSchema, ErasedCommand } from '#internals/core/schema/command.ts';
14
+
15
+ // --- Dispatch result types (discriminated union)
16
+
17
+ /** Successful dispatch — target command found with argv path. */
18
+ interface DispatchMatch {
19
+ readonly kind: 'match';
20
+ /** The matched (target) command. */
21
+ readonly command: ErasedCommand;
22
+ /** Root → target (inclusive). Used for `collectPropagatedFlags()`. */
23
+ readonly commandPath: readonly CommandSchema[];
24
+ /** argv after consuming command name segments. */
25
+ readonly remainingArgv: readonly string[];
26
+ }
27
+
28
+ /** Target has subcommands but no handler and no subcommand was specified/matched. */
29
+ interface DispatchNeedsSubcommand {
30
+ readonly kind: 'needs-subcommand';
31
+ /** The group command that needs a subcommand. */
32
+ readonly command: ErasedCommand;
33
+ /** Root → group (inclusive). */
34
+ readonly commandPath: readonly CommandSchema[];
35
+ }
36
+
37
+ /** Unknown command name at this dispatch level. */
38
+ interface DispatchUnknown {
39
+ readonly kind: 'unknown';
40
+ /** The unrecognised input token. Empty string when no token present. */
41
+ readonly input: string;
42
+ /** Commands available at the level where matching failed. */
43
+ readonly candidates: readonly ErasedCommand[];
44
+ /** Ancestor path up to (but not including) the unknown level. */
45
+ readonly parentPath: readonly CommandSchema[];
46
+ }
47
+
48
+ type DispatchResult = DispatchMatch | DispatchNeedsSubcommand | DispatchUnknown;
49
+
50
+ // --- Recursive dispatch
51
+
52
+ /**
53
+ * Recursively walk argv, consuming command name segments from the front.
54
+ *
55
+ * At each level, the first non-flag token is tested as a command name
56
+ * against the provided command map. If matched and the matched command
57
+ * has subcommands, dispatch recurses into the child level with the
58
+ * remaining argv.
59
+ *
60
+ * Ambiguity resolution for commands that have both an action handler AND
61
+ * subcommands (e.g. `git remote` lists remotes, `git remote add` dispatches):
62
+ * - If the next token matches a subcommand → descend
63
+ * - If the next token is unknown and the command has a handler → match here
64
+ * - If the next token is unknown and no handler → propagate unknown error
65
+ * - If no next token and command has a handler → match here
66
+ * - If no next token and no handler → needs-subcommand
67
+ *
68
+ * @param argv - Remaining argv tokens (command names + flags + args).
69
+ * @param commands - Command map at the current tree level.
70
+ * @param ancestorPath - Schema path from root to current level (exclusive).
71
+ * @returns Discriminated dispatch result.
72
+ *
73
+ * @internal
74
+ */
75
+ function dispatch(
76
+ argv: readonly string[],
77
+ commands: ReadonlyMap<string, ErasedCommand>,
78
+ ancestorPath: readonly CommandSchema[] = [],
79
+ ): DispatchResult {
80
+ // Find first non-flag token (potential command name).
81
+ // Flags may appear before the command name (e.g. `--verbose db migrate`).
82
+ // `--` terminates flag scanning — the next token is treated as a command name.
83
+ let cmdIdx = -1;
84
+ for (let i = 0; i < argv.length; i++) {
85
+ const token = argv[i];
86
+ if (token === '--') {
87
+ // End-of-flags marker: next token (if any) is the command name.
88
+ if (i + 1 < argv.length) cmdIdx = i + 1;
89
+ break;
90
+ }
91
+ if (token !== undefined && !token.startsWith('-')) {
92
+ cmdIdx = i;
93
+ break;
94
+ }
95
+ }
96
+
97
+ if (cmdIdx === -1) {
98
+ // No command name token found — only flags or empty argv.
99
+ return {
100
+ kind: 'unknown',
101
+ input: '',
102
+ candidates: uniqueCommands(commands),
103
+ parentPath: ancestorPath,
104
+ };
105
+ }
106
+
107
+ const cmdName = argv[cmdIdx];
108
+ if (cmdName === undefined) {
109
+ return {
110
+ kind: 'unknown',
111
+ input: '',
112
+ candidates: uniqueCommands(commands),
113
+ parentPath: ancestorPath,
114
+ };
115
+ }
116
+
117
+ const matched = commands.get(cmdName);
118
+ if (matched === undefined) {
119
+ return {
120
+ kind: 'unknown',
121
+ input: cmdName,
122
+ candidates: uniqueCommands(commands),
123
+ parentPath: ancestorPath,
124
+ };
125
+ }
126
+
127
+ // Remove command name token from argv, preserving order.
128
+ const remaining = [...argv.slice(0, cmdIdx), ...argv.slice(cmdIdx + 1)];
129
+ const currentPath = [...ancestorPath, matched.schema];
130
+
131
+ // If command has subcommands, try to descend.
132
+ if (matched.subcommands.size > 0) {
133
+ const subResult = dispatch(remaining, matched.subcommands, currentPath);
134
+
135
+ switch (subResult.kind) {
136
+ case 'match':
137
+ // Successfully found deeper target.
138
+ return subResult;
139
+
140
+ case 'unknown':
141
+ if (subResult.input === '') {
142
+ // No subcommand specified.
143
+ if (matched.schema.hasAction) {
144
+ return {
145
+ kind: 'match',
146
+ command: matched,
147
+ commandPath: currentPath,
148
+ remainingArgv: remaining,
149
+ };
150
+ }
151
+ return { kind: 'needs-subcommand', command: matched, commandPath: currentPath };
152
+ }
153
+
154
+ // Unknown token — might be a positional arg for this command.
155
+ if (matched.schema.hasAction) {
156
+ return {
157
+ kind: 'match',
158
+ command: matched,
159
+ commandPath: currentPath,
160
+ remainingArgv: remaining,
161
+ };
162
+ }
163
+ // No handler — propagate the unknown error.
164
+ return subResult;
165
+
166
+ case 'needs-subcommand':
167
+ // Propagate from deeper level.
168
+ return subResult;
169
+
170
+ default: {
171
+ // Exhaustiveness guard — ensures all DispatchResult variants are handled.
172
+ const _exhaustive: never = subResult;
173
+ return _exhaustive;
174
+ }
175
+ }
176
+ }
177
+
178
+ // Leaf command (no subcommands).
179
+ return {
180
+ kind: 'match',
181
+ command: matched,
182
+ commandPath: currentPath,
183
+ remainingArgv: remaining,
184
+ };
185
+ }
186
+
187
+ // --- "Did you mean?" suggestion
188
+
189
+ /**
190
+ * Levenshtein distance between two strings.
191
+ * @internal
192
+ */
193
+ function levenshtein(a: string, b: string): number {
194
+ const m = a.length;
195
+ const n = b.length;
196
+
197
+ // Flat (m+1)×(n+1) matrix — fully initialised, no sparse access.
198
+ // Two-row rolling buffer: only the previous and current rows are needed.
199
+ const w = n + 1;
200
+ let prev = new Uint16Array(w);
201
+ let curr = new Uint16Array(w);
202
+
203
+ // Base row: distance from empty string to b[0..j]
204
+ for (let j = 0; j <= n; j++) prev[j] = j;
205
+
206
+ for (let i = 1; i <= m; i++) {
207
+ curr[0] = i;
208
+ for (let j = 1; j <= n; j++) {
209
+ const cost = a.charAt(i - 1) === b.charAt(j - 1) ? 0 : 1;
210
+ // All indices are in-bounds: j ∈ [1,n], j-1 ∈ [0,n-1], both < w.
211
+ // prev/curr are dense Uint16Arrays of length w — no holes.
212
+ const del = prev[j] ?? 0;
213
+ const ins = curr[j - 1] ?? 0;
214
+ const sub = prev[j - 1] ?? 0;
215
+ curr[j] = Math.min(del + 1, ins + 1, sub + cost);
216
+ }
217
+ [prev, curr] = [curr, prev];
218
+ }
219
+
220
+ // After the loop, `prev` holds the last computed row (swapped at loop end).
221
+ return prev[n] ?? Math.max(m, n);
222
+ }
223
+
224
+ /**
225
+ * Find the closest command name match for a "did you mean?" suggestion.
226
+ *
227
+ * Searches command names and aliases. Returns `undefined` if no
228
+ * sufficiently close match exists (threshold: 3).
229
+ *
230
+ * @internal
231
+ */
232
+ function findClosestCommand(input: string, commands: readonly ErasedCommand[]): string | undefined {
233
+ const MAX_DISTANCE = 3;
234
+ let bestName: string | undefined;
235
+ let bestDist = MAX_DISTANCE + 1;
236
+
237
+ for (const cmd of commands) {
238
+ // Check main name
239
+ const nameDist = levenshtein(input, cmd.schema.name);
240
+ if (nameDist < bestDist) {
241
+ bestDist = nameDist;
242
+ bestName = cmd.schema.name;
243
+ }
244
+ // Check aliases
245
+ for (const alias of cmd.schema.aliases) {
246
+ const aliasDist = levenshtein(input, alias);
247
+ if (aliasDist < bestDist) {
248
+ bestDist = aliasDist;
249
+ bestName = cmd.schema.name; // suggest canonical name
250
+ }
251
+ }
252
+ }
253
+
254
+ return bestDist <= MAX_DISTANCE ? bestName : undefined;
255
+ }
256
+
257
+ // --- Helpers
258
+
259
+ /**
260
+ * Deduplicate commands from a name+alias map.
261
+ *
262
+ * A map keyed by name *and* alias contains duplicate entries for aliased
263
+ * commands. This returns unique commands (by schema identity).
264
+ *
265
+ * @internal
266
+ */
267
+ function uniqueCommands(commands: ReadonlyMap<string, ErasedCommand>): readonly ErasedCommand[] {
268
+ const seen = new Set<ErasedCommand>();
269
+ const result: ErasedCommand[] = [];
270
+ for (const cmd of commands.values()) {
271
+ if (!seen.has(cmd)) {
272
+ seen.add(cmd);
273
+ result.push(cmd);
274
+ }
275
+ }
276
+ return result;
277
+ }
278
+
279
+ // --- Exports
280
+
281
+ export type { DispatchMatch, DispatchNeedsSubcommand, DispatchResult, DispatchUnknown };
282
+ export { dispatch, findClosestCommand, levenshtein, uniqueCommands };