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