From 9aa79485c211dfcef4a028b34125ee85d2493fb4 Mon Sep 17 00:00:00 2001 From: James George Date: Tue, 7 May 2019 21:12:18 +0530 Subject: [PATCH] feat:suggest matching commands if the user mistypes :construction: (#181) --- package.json | 1 + src/cli.js | 14 ++++++++++++++ 2 files changed, 15 insertions(+) diff --git a/package.json b/package.json index 89a3106..bf26862 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/cli.js b/src/cli.js index 96f0895..1ed3a39 100755 --- a/src/cli.js +++ b/src/cli.js @@ -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}`) } })