Skip to content

Commit

Permalink
feat(gatsby-cli) better suggestions (#12392)
Browse files Browse the repository at this point in the history
* chore(gatsby-cli): Add meant dependency (#12078)

Who uses `meant`: This package is used by NPM.

Implementation: Used to mimic the  same UX of giving command suggestions, upon failure to provide correct command

Additional Reasons to use package:
- Zero Dependencies attached
- Only 30 lines of POJO - very lightweight
- Very few issues with current versions

Please checkout: https://www.npmjs.com/package/meant

* feat: implemention of did you mean suggestion (#12078)

This code has been duplicated from NPM 6.8.0 with minor tweaks.

Tweaks:
- Style changes of using string template literals over single quotes
- newlines - \n added to the end to line 10 and line 13, to make the code more developer friendly

* Modify gatsby-cli include command suggestion (#12078)

Line 328 - alter demandCommand - message string `Pass --help ...` will not show, because cli.fail method (330 line) is utilise. Thus message string has been moved to function 285.
Another reason it has been moved there is organise al suggestions into one place

Line 318 - showHelpOnFail could not get access to `yargs` thus cli.fail function was used instead

Line 319 - `recommendCommands`, did not recommend commands were not very accurate, thus cli.fail was used configured more specific commands and show multiple command suggestions

* fix(gatsby-cli): demandCommand was changed back to original (#12078)
  • Loading branch information
anthonytranDev authored and sidharthachatterjee committed Mar 8, 2019
1 parent 3b116f6 commit 2e96660
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 2 deletions.
1 change: 1 addition & 0 deletions packages/gatsby-cli/package.json
Expand Up @@ -22,6 +22,7 @@
"fs-extra": "^4.0.1",
"hosted-git-info": "^2.6.0",
"lodash": "^4.17.10",
"meant": "^1.0.1",
"opentracing": "^0.14.3",
"pretty-error": "^2.1.1",
"resolve-cwd": "^2.0.0",
Expand Down
15 changes: 13 additions & 2 deletions packages/gatsby-cli/src/create-cli.js
Expand Up @@ -2,6 +2,7 @@ const path = require(`path`)
const resolveCwd = require(`resolve-cwd`)
const yargs = require(`yargs`)
const report = require(`./reporter`)
const didYouMean = require(`./did-you-mean`)
const envinfo = require(`envinfo`)
const existsSync = require(`fs-exists-cached`).sync

Expand Down Expand Up @@ -316,7 +317,17 @@ module.exports = argv => {
.wrap(cli.terminalWidth())
.demandCommand(1, `Pass --help to see all available commands and options.`)
.strict()
.showHelpOnFail(true)
.recommendCommands()
.fail((msg, err, yargs) => {
const availableCommands = yargs.getCommands().map(commandDescription => {
const [command] = commandDescription
return command.split(` `)[0]
})
const arg = argv.slice(2)[0]
const suggestion = arg ? didYouMean(arg, availableCommands) : ``

cli.showHelp()
report.log(suggestion)
report.log(msg)
})
.parse(argv.slice(2))
}
20 changes: 20 additions & 0 deletions packages/gatsby-cli/src/did-you-mean.js
@@ -0,0 +1,20 @@
const meant = require(`meant`)

function didYouMean(scmd, commands) {
const bestSimilarity = meant(scmd, commands).map(function(str) {
return ` ${str}`
})

if (bestSimilarity.length === 0) return ``
if (bestSimilarity.length === 1) {
return `\nDid you mean this?\n ${bestSimilarity[0]}\n`
} else {
return (
[`\nDid you mean one of these?`]
.concat(bestSimilarity.slice(0, 3))
.join(`\n`) + `\n`
)
}
}

module.exports = didYouMean
5 changes: 5 additions & 0 deletions yarn.lock
Expand Up @@ -12952,6 +12952,11 @@ mdurl@^1.0.1:
resolved "https://registry.yarnpkg.com/mdurl/-/mdurl-1.0.1.tgz#fe85b2ec75a59037f2adfec100fd6c601761152e"
integrity sha1-/oWy7HWlkDfyrf7BAP1sYBdhFS4=

meant@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/meant/-/meant-1.0.1.tgz#66044fea2f23230ec806fb515efea29c44d2115d"
integrity sha512-UakVLFjKkbbUwNWJ2frVLnnAtbb7D7DsloxRd3s/gDpI8rdv8W5Hp3NaDb+POBI1fQdeussER6NB8vpcRURvlg==

media-typer@0.3.0:
version "0.3.0"
resolved "https://registry.yarnpkg.com/media-typer/-/media-typer-0.3.0.tgz#8710d7af0aa626f8fffa1ce00168545263255748"
Expand Down

0 comments on commit 2e96660

Please sign in to comment.