Skip to content

Commit

Permalink
fix: identify when the command is meant to be 'help'
Browse files Browse the repository at this point in the history
  • Loading branch information
WillieRuemmele committed Jul 10, 2023
1 parent 408794e commit 790e92d
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions src/index.ts
Expand Up @@ -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 <command>"?'
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)
Expand All @@ -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)
}

Expand Down

0 comments on commit 790e92d

Please sign in to comment.