From 790e92db9f039093503532a1b191b2151b39c2a7 Mon Sep 17 00:00:00 2001 From: Willie Ruemmele Date: Mon, 10 Jul 2023 14:19:16 -0600 Subject: [PATCH] fix: identify when the command is meant to be 'help' --- src/index.ts | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/index.ts b/src/index.ts index 6c440f12..a5885799 100644 --- a/src/index.ts +++ b/src/index.ts @@ -21,7 +21,9 @@ const hook: Hook.CommandNotFound = async function (opts) { binHelp = `${binHelp} ${idSplit[0]}` } - const suggestion = closest(opts.id, commandIDs) + // alter the suggestion in the help scenario so that help is the first command + // otherwise the user will be presented 'did you mean 'help'?' instead of 'did you mean "help "?' + let suggestion = /:?help:?/.test(opts.id) ? ['help', closest(opts.id.replace(/:?help:?/, ''), commandIDs)].join(':') : closest(opts.id, commandIDs) const readableSuggestion = toConfiguredId(suggestion, this.config) const originalCmd = toConfiguredId(opts.id, this.config) @@ -38,7 +40,16 @@ const hook: Hook.CommandNotFound = async function (opts) { if (response === 'y') { // this will split the original command from the suggested replacement, and gather the remaining args as varargs to help with situations like: // confit set foo-bar -> confit:set:foo-bar -> config:set:foo-bar -> config:set foo-bar - const argv = opts.argv?.length ? opts.argv : opts.id.split(':').slice(suggestion.split(':').length) + let argv = opts.argv?.length ? opts.argv : opts.id.split(':').slice(suggestion.split(':').length) + + if (suggestion.startsWith('help:')) { + // the args are the command/partial command you need help for (package:version) + // we created the suggestion variable to start with "help" so slice the first entry + argv = [...suggestion.split(':').slice(1), ...argv] + // the command is just the word "help" + suggestion = 'help' + } + return this.config.runCommand(suggestion, argv) }