Skip to content

Commit ef6d195

Browse files
committed
chore: stricter checks
1 parent 7012f47 commit ef6d195

File tree

2 files changed

+35
-8
lines changed

2 files changed

+35
-8
lines changed

src/entrypoints/_shared.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -238,10 +238,23 @@ export async function runCLI(entrypoint: string) {
238238
return errorMessageSplit[1];
239239
})();
240240

241+
const closestMatches = useCommandSuggestions(`${parsed._[0]} ${errorMessageSplit[1]}`);
242+
243+
const messageParts = [
244+
chalk.gray(`Nonexistent ${nonexistentType}: ${chalk.whiteBright(nonexistentRepresentation)}`),
245+
];
246+
247+
if (closestMatches.length) {
248+
messageParts.push(
249+
chalk.gray(
250+
` Did you mean: ${closestMatches.map((cmd) => chalk.whiteBright(cmd)).join(', ')}?`,
251+
),
252+
);
253+
}
254+
241255
error({
242256
message: [
243-
`Nonexistent ${nonexistentType}: ${nonexistentRepresentation}`,
244-
` ${chalk.red('>')} See more help with --help`,
257+
...messageParts,
245258
'',
246259
selectiveRenderHelpForCommand(command, {
247260
showUsageString: true,

src/lib/hooks/useCommandSuggestions.ts

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { jaroWinkler } from '@skyra/jaro-winkler';
22
import levenshtein from 'js-levenshtein';
33

44
import { commandRegistry } from '../command-framework/apify-command.js';
5+
import { cliDebugPrint } from '../utils/cliDebugPrint.js';
56

67
export function useCommandSuggestions(inputString: string) {
78
const allCommands = [...commandRegistry.entries()];
@@ -11,17 +12,30 @@ export function useCommandSuggestions(inputString: string) {
1112
const closestMatches = allCommands
1213
.map(([cmdString, cmdClass]) => {
1314
const lowercased = cmdString.toLowerCase();
15+
const cmdStringParts = cmdString.split(' ');
16+
const lastPart = cmdStringParts[cmdStringParts.length - 1].toLowerCase();
1417

15-
const matches =
16-
levenshtein(lowercasedCommandString, lowercased) <= 2 ||
17-
jaroWinkler(lowercasedCommandString, lowercased) >= 0.95;
18+
const isAlias = cmdClass.aliases?.includes(lastPart) || cmdClass.hiddenAliases?.includes(lastPart) || false;
19+
20+
const levenshteinDistance = levenshtein(lowercasedCommandString, lowercased);
21+
const jaroWinklerDistance = jaroWinkler(lowercasedCommandString, lowercased);
22+
23+
const matches = levenshteinDistance <= 2 || jaroWinklerDistance >= 0.975;
1824

1925
if (matches) {
20-
if (cmdString === cmdClass.name) {
21-
return cmdString;
26+
cliDebugPrint('useCommandSuggestions', {
27+
inputString: lowercasedCommandString,
28+
lowercased,
29+
matches,
30+
levenshtein: levenshteinDistance,
31+
jaroWinkler: jaroWinklerDistance,
32+
});
33+
34+
if (!isAlias) {
35+
return `${lowercased}`;
2236
}
2337

24-
return `${cmdString} (alias for ${cmdClass.name})`;
38+
return `${lowercased} (alias for ${cmdClass.name})`;
2539
}
2640

2741
return null;

0 commit comments

Comments
 (0)