2023-06-08 05:24:49 -07:00
|
|
|
const Npm = require('../npm')
|
2021-08-19 17:47:33 +00:00
|
|
|
const { distance } = require('fastest-levenshtein')
|
2022-11-07 13:02:05 -05:00
|
|
|
const { commands } = require('./cmd-list.js')
|
2017-10-26 22:35:25 -04:00
|
|
|
|
2024-05-16 05:38:49 -07:00
|
|
|
const runScripts = ['stop', 'start', 'test', 'restart']
|
|
|
|
|
|
|
|
const isClose = (scmd, cmd) => distance(scmd, cmd) < scmd.length * 0.4
|
|
|
|
|
|
|
|
const didYouMean = (pkg, scmd) => {
|
|
|
|
const { scripts = {}, bin = {} } = pkg || {}
|
|
|
|
|
|
|
|
const best = [
|
|
|
|
...commands
|
|
|
|
.filter(cmd => isClose(scmd, cmd) && scmd !== cmd)
|
|
|
|
.map(str => [str, Npm.cmd(str).description]),
|
|
|
|
...Object.keys(scripts)
|
|
|
|
// We would already be suggesting this in `npm x` so omit them here
|
|
|
|
.filter(cmd => isClose(scmd, cmd) && !runScripts.includes(cmd))
|
|
|
|
.map(str => [`run ${str}`, `run the "${str}" package script`]),
|
|
|
|
...Object.keys(bin)
|
|
|
|
.filter(cmd => isClose(scmd, cmd))
|
|
|
|
.map(str => [`exec ${str}`, `run the "${str}" command from either this or a remote npm package`]),
|
|
|
|
]
|
2021-03-23 14:58:11 -04:00
|
|
|
|
2021-11-18 20:58:02 +00:00
|
|
|
if (best.length === 0) {
|
2021-03-23 14:58:11 -04:00
|
|
|
return ''
|
2021-11-18 20:58:02 +00:00
|
|
|
}
|
2021-03-23 14:58:11 -04:00
|
|
|
|
2024-05-16 05:38:49 -07:00
|
|
|
return `\n\nDid you mean ${best.length === 1 ? 'this' : 'one of these'}?\n` +
|
|
|
|
best.slice(0, 3).map(([msg, comment]) => ` npm ${msg} # ${comment}`).join('\n')
|
2021-03-23 14:58:11 -04:00
|
|
|
}
|
2024-04-30 23:53:22 -07:00
|
|
|
|
2017-10-26 22:35:25 -04:00
|
|
|
module.exports = didYouMean
|