feat:suggest matching commands if the user mistypes 🚧 (#181)

This commit is contained in:
James George 2019-05-07 21:12:18 +05:30 committed by Jake Bolam
parent fbbbfca396
commit 9aa79485c2
2 changed files with 15 additions and 0 deletions

View file

@ -45,6 +45,7 @@
"@babel/runtime": "^7.2.0",
"async": "^2.0.0-rc.1",
"chalk": "^2.3.0",
"didyoumean": "^1.2.1",
"inquirer": "^6.2.1",
"lodash": "^4.11.2",
"pify": "^4.0.1",

View file

@ -5,6 +5,10 @@ const path = require('path')
const yargs = require('yargs')
const chalk = require('chalk')
const inquirer = require('inquirer')
const didYouMean = require('didyoumean')
// Setting edit length to be 60% of the input string's length
didYouMean.threshold = 0.6
const init = require('./init')
const generate = require('./generate')
@ -46,6 +50,15 @@ const yargv = yargs
}
}).argv
function suggestCommands(cmd) {
const availableCommands = ['generate', 'add', 'init', 'check']
const suggestion = didYouMean(cmd, availableCommands)
if (suggestion) {
console.log(chalk.bold(`Did you mean ${suggestion}`))
}
}
function startGeneration(argv) {
return Promise.all(
argv.files.map(file => {
@ -170,6 +183,7 @@ promptForCommand(yargv)
case 'check':
return checkContributors(yargv)
default:
suggestCommands(command)
throw new Error(`Unknown command ${command}`)
}
})