Skip to content

Commit

Permalink
Merge pull request #409 from oclif/wr/didYouMeanHelp
Browse files Browse the repository at this point in the history
fix: identify when the command is meant to be 'help'
  • Loading branch information
mshanemc authored Jul 11, 2023
2 parents 408794e + edbb088 commit 946a8cd
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,14 @@
"did you mean" for oclif

[![Version](https://img.shields.io/npm/v/@oclif/plugin-not-found.svg)](https://npmjs.org/package/@oclif/plugin-not-found)
[![CircleCI](https://circleci.com/gh/oclif/plugin-not-found/tree/main.svg?style=svg)](https://circleci.com/gh/oclif/plugin-not-found/tree/main)
[![Appveyor CI](https://ci.appveyor.com/api/projects/status/github/oclif/plugin-not-found?branch=main&svg=true)](https://ci.appveyor.com/project/heroku/plugin-not-found/branch/main)
[![Known Vulnerabilities](https://snyk.io/test/npm/@oclif/plugin-not-found/badge.svg)](https://snyk.io/test/npm/@oclif/plugin-not-found)
[![Downloads/week](https://img.shields.io/npm/dw/@oclif/plugin-not-found.svg)](https://npmjs.org/package/@oclif/plugin-not-found)
[![License](https://img.shields.io/npm/l/@oclif/plugin-not-found.svg)](https://github.com/oclif/plugin-not-found/blob/main/package.json)

## Developing

This plugin works as a hook, so it won't "fire" just from being linked (ex: `plugins link .`)

To test changes using a CLI that already contains plugin-not-found, build this and copy this repo's `lib/index.js` over your CLI's equivalent.

ex: `~/.local/share/sf/client/current/node_modules/@oclif/plugin-not-found/lib/index.js`
15 changes: 13 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
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', ...opts.id.split(':').filter(cmd => cmd !== 'help')].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)
// the command is just the word "help"
suggestion = 'help'
}

return this.config.runCommand(suggestion, argv)
}

Expand Down

0 comments on commit 946a8cd

Please sign in to comment.