diff options
author | Daijiro Wachi <daijiro.wachi@gmail.com> | 2016-09-09 01:13:36 +0300 |
---|---|---|
committer | Rebecca Turner <me@re-becca.org> | 2017-07-17 23:20:03 +0300 |
commit | 1ac470dd283cc7758dc37721dd6331d5b316dc99 (patch) | |
tree | 50c0a99a45c8257d3a58a2af07f76d845e81f644 /lib | |
parent | 6c03e5ab84268614d6b27d85e56b00814fab5e99 (diff) |
help: Implement new help message like did-you-mean
When user makes typo, npm display command list when there are similar commands.
PR-URL: https://github.com/npm/npm/pull/10382
Fixes: #4324
Credit: @watilde
Reviewed-By: @iarna
Diffstat (limited to 'lib')
-rw-r--r-- | lib/help.js | 6 | ||||
-rw-r--r-- | lib/utils/did-you-mean.js | 20 |
2 files changed, 26 insertions, 0 deletions
diff --git a/lib/help.js b/lib/help.js index 9763d5fcc..64c80f787 100644 --- a/lib/help.js +++ b/lib/help.js @@ -12,6 +12,7 @@ var npm = require('./npm.js') var log = require('npmlog') var opener = require('opener') var glob = require('glob') +var didYouMean = require('./utils/did-you-mean') var cmdList = require('./config/cmd-list').cmdList var shorthands = require('./config/cmd-list').shorthands var commands = cmdList.concat(Object.keys(shorthands)) @@ -181,6 +182,11 @@ function npmUsage (valid, cb) { '', 'npm@' + npm.version + ' ' + path.dirname(__dirname) ].join('\n')) + + if (npm.argv.length > 1) { + didYouMean(npm.argv[1], commands) + } + cb(valid) } diff --git a/lib/utils/did-you-mean.js b/lib/utils/did-you-mean.js new file mode 100644 index 000000000..8e72dde5f --- /dev/null +++ b/lib/utils/did-you-mean.js @@ -0,0 +1,20 @@ +var meant = require('meant') +var output = require('./output.js') + +function didYouMean (scmd, commands) { + var bestSimilarity = meant(scmd, commands).map(function (str) { + return ' ' + str + }) + + if (bestSimilarity.length === 0) return + if (bestSimilarity.length === 1) { + output('\nDid you mean this?\n' + bestSimilarity[0]) + } else { + output( + ['\nDid you mean one of these?'] + .concat(bestSimilarity.slice(0, 3)).join('\n') + ) + } +} + +module.exports = didYouMean |