Skip to content

Commit

Permalink
feat: show suggestion on not found error
Browse files Browse the repository at this point in the history
  • Loading branch information
imLymei committed Sep 17, 2024
1 parent 610e4e5 commit 4b6f752
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 7 deletions.
20 changes: 17 additions & 3 deletions src/commands/editCommand.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { Command } from 'commander';
import { Config, createConfig, getConfig } from '../lib/utils';
import { error } from 'console';
import {
Config,
createConfig,
error,
getClosestWord,
getConfig,
} from '../lib/utils';

const editCommand = new Command('edit')
.alias('e')
Expand All @@ -10,7 +15,16 @@ const editCommand = new Command('edit')
.action((name: string, command: string) => {
const newConfig: Config = getConfig();

if (!newConfig.commands[name]) error(`Command "${name}" not found`);
if (!newConfig.commands[name]) {
const closestWord = getClosestWord(name, Object.keys(newConfig.commands));

error(
`Command "${name}" not found`,
isFinite(closestWord.distance)
? `Did you mean: "${closestWord.word}"?`
: undefined
);
}

newConfig.commands[name] = command;

Expand Down
13 changes: 11 additions & 2 deletions src/commands/removeCommand.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Command } from 'commander';
import { createConfig, error, getConfig } from '../lib/utils';
import { createConfig, error, getClosestWord, getConfig } from '../lib/utils';

const removeCommand = new Command('remove')
.alias('rm')
Expand All @@ -8,7 +8,16 @@ const removeCommand = new Command('remove')
.action((name: string) => {
const config = getConfig();

if (!config.commands[name]) error(`Command "${name}" not found`);
if (!config.commands[name]) {
const closestWord = getClosestWord(name, Object.keys(config.commands));

error(
`Command "${name}" not found`,
isFinite(closestWord.distance)
? `Did you mean: "${closestWord.word}"?`
: undefined
);
}

delete config.commands[name];

Expand Down
13 changes: 11 additions & 2 deletions src/commands/renameCommand.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Command } from 'commander';
import { createConfig, error, getConfig } from '../lib/utils';
import { createConfig, error, getClosestWord, getConfig } from '../lib/utils';

const renameCommand = new Command('rename')
.alias('rn')
Expand All @@ -9,7 +9,16 @@ const renameCommand = new Command('rename')
.action((oldName: string, newName: string) => {
const config = getConfig();

if (!config.commands[oldName]) error('Command not found');
if (!config.commands[oldName]) {
const closestWord = getClosestWord(oldName, Object.keys(config.commands));

error(
`Command "${oldName}" not found`,
isFinite(closestWord.distance)
? `Did you mean: "${closestWord.word}"?`
: undefined
);
}

config.commands[newName] = config.commands[oldName];
delete config.commands[oldName];
Expand Down

0 comments on commit 4b6f752

Please sign in to comment.