@ghl-ai/aw 0.1.54-beta.0 → 0.1.55-beta.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/cli.mjs +49 -2
- package/package.json +1 -1
package/cli.mjs
CHANGED
|
@@ -34,6 +34,8 @@ const COMMANDS = {
|
|
|
34
34
|
'init-repo': () => import('./commands/init-repo.mjs').then(m => m.initRepoCommand),
|
|
35
35
|
};
|
|
36
36
|
|
|
37
|
+
const COMMAND_NAMES = Object.keys(COMMANDS);
|
|
38
|
+
|
|
37
39
|
function parseArgs(argv) {
|
|
38
40
|
const args = { _positional: [] };
|
|
39
41
|
let command = null;
|
|
@@ -42,7 +44,7 @@ function parseArgs(argv) {
|
|
|
42
44
|
while (i < argv.length) {
|
|
43
45
|
const arg = argv[i];
|
|
44
46
|
|
|
45
|
-
if (!command && !arg.startsWith('-')
|
|
47
|
+
if (!command && !arg.startsWith('-')) {
|
|
46
48
|
command = arg;
|
|
47
49
|
i++;
|
|
48
50
|
continue;
|
|
@@ -78,6 +80,51 @@ function parseArgs(argv) {
|
|
|
78
80
|
return { command, args };
|
|
79
81
|
}
|
|
80
82
|
|
|
83
|
+
function editDistance(a, b) {
|
|
84
|
+
const dp = Array.from({ length: a.length + 1 }, () => Array(b.length + 1).fill(0));
|
|
85
|
+
|
|
86
|
+
for (let i = 0; i <= a.length; i++) dp[i][0] = i;
|
|
87
|
+
for (let j = 0; j <= b.length; j++) dp[0][j] = j;
|
|
88
|
+
|
|
89
|
+
for (let i = 1; i <= a.length; i++) {
|
|
90
|
+
for (let j = 1; j <= b.length; j++) {
|
|
91
|
+
const substitutionCost = a[i - 1] === b[j - 1] ? 0 : 1;
|
|
92
|
+
dp[i][j] = Math.min(
|
|
93
|
+
dp[i - 1][j] + 1,
|
|
94
|
+
dp[i][j - 1] + 1,
|
|
95
|
+
dp[i - 1][j - 1] + substitutionCost,
|
|
96
|
+
);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
return dp[a.length][b.length];
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
function suggestCommand(input) {
|
|
104
|
+
const normalizedInput = input.toLowerCase();
|
|
105
|
+
let best = null;
|
|
106
|
+
|
|
107
|
+
for (const name of COMMAND_NAMES) {
|
|
108
|
+
const distance = editDistance(normalizedInput, name.toLowerCase());
|
|
109
|
+
if (!best || distance < best.distance) {
|
|
110
|
+
best = { name, distance };
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
if (!best) return null;
|
|
115
|
+
const maxDistance = Math.max(2, Math.floor(best.name.length / 3));
|
|
116
|
+
return best.distance <= maxDistance ? best.name : null;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
function formatUnknownCommand(command) {
|
|
120
|
+
const suggestion = suggestCommand(command);
|
|
121
|
+
const hint = suggestion
|
|
122
|
+
? `Did you mean: aw ${suggestion}?`
|
|
123
|
+
: 'Run aw --help to see available commands.';
|
|
124
|
+
|
|
125
|
+
return `Unknown command: ${command}\n${hint}`;
|
|
126
|
+
}
|
|
127
|
+
|
|
81
128
|
function printHelp() {
|
|
82
129
|
fmt.banner('aw', {
|
|
83
130
|
icon: '⟁',
|
|
@@ -220,5 +267,5 @@ export async function run(argv) {
|
|
|
220
267
|
process.exit(0);
|
|
221
268
|
}
|
|
222
269
|
|
|
223
|
-
fmt.cancelAndExit(
|
|
270
|
+
fmt.cancelAndExit(formatUnknownCommand(command));
|
|
224
271
|
}
|