From 4b6f7522c1be392a0d08aa69220ee3334e9cf708 Mon Sep 17 00:00:00 2001 From: Felipe Cardoso Date: Tue, 17 Sep 2024 08:52:40 -0300 Subject: [PATCH] feat: show suggestion on not found error --- src/commands/editCommand.ts | 20 +++++++++++++++++--- src/commands/removeCommand.ts | 13 +++++++++++-- src/commands/renameCommand.ts | 13 +++++++++++-- 3 files changed, 39 insertions(+), 7 deletions(-) diff --git a/src/commands/editCommand.ts b/src/commands/editCommand.ts index c7b76c3..4a9c159 100644 --- a/src/commands/editCommand.ts +++ b/src/commands/editCommand.ts @@ -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') @@ -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; diff --git a/src/commands/removeCommand.ts b/src/commands/removeCommand.ts index 8dfbe89..cbe6e6f 100644 --- a/src/commands/removeCommand.ts +++ b/src/commands/removeCommand.ts @@ -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') @@ -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]; diff --git a/src/commands/renameCommand.ts b/src/commands/renameCommand.ts index 91af2c2..d81675e 100644 --- a/src/commands/renameCommand.ts +++ b/src/commands/renameCommand.ts @@ -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') @@ -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];