Skip to content

Commit

Permalink
fix(did-you-mean): switch levenshtein libraries
Browse files Browse the repository at this point in the history
`leven` dropped support for node10 and we still currently have to support
it.  Moved to https://github.com/ka-weihe/fastest-levenshtein

Originally discussed in #2403, but the
did-you-mean lib moved quite a bit since then and there were conflicts
so I made a new PR

PR-URL: #3640
Credit: @wraithgar
Close: #3640
Reviewed-by: @nlf
  • Loading branch information
wraithgar committed Aug 17, 2021
1 parent 59b9851 commit 32e88c9
Show file tree
Hide file tree
Showing 13 changed files with 314 additions and 235 deletions.
8 changes: 4 additions & 4 deletions lib/utils/did-you-mean.js
@@ -1,24 +1,24 @@
const leven = require('leven')
const { distance } = require('fastest-levenshtein')
const readJson = require('read-package-json-fast')
const { cmdList } = require('./cmd-list.js')

const didYouMean = async (npm, path, scmd) => {
const bestCmd = cmdList
.filter(cmd => leven(scmd, cmd) < scmd.length * 0.4 && scmd !== cmd)
.filter(cmd => distance(scmd, cmd) < scmd.length * 0.4 && scmd !== cmd)
.map(str => ` npm ${str} # ${npm.commands[str].description}`)

const pkg = await readJson(`${path}/package.json`)
const { scripts } = pkg
// We would already be suggesting this in `npm x` so omit them here
const runScripts = ['stop', 'start', 'test', 'restart']
const bestRun = Object.keys(scripts || {})
.filter(cmd => leven(scmd, cmd) < scmd.length * 0.4 &&
.filter(cmd => distance(scmd, cmd) < scmd.length * 0.4 &&
!runScripts.includes(cmd))
.map(str => ` npm run ${str} # run the "${str}" package script`)

const { bin } = pkg
const bestBin = Object.keys(bin || {})
.filter(cmd => leven(scmd, cmd) < scmd.length * 0.4)
.filter(cmd => distance(scmd, cmd) < scmd.length * 0.4)
.map(str => ` npm exec ${str} # run the "${str}" command from either this or a remote npm package`)

const best = [...bestCmd, ...bestRun, ...bestBin]
Expand Down
21 changes: 21 additions & 0 deletions node_modules/fastest-levenshtein/LICENSE.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions node_modules/fastest-levenshtein/index.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

147 changes: 147 additions & 0 deletions node_modules/fastest-levenshtein/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

63 changes: 63 additions & 0 deletions node_modules/fastest-levenshtein/package.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

64 changes: 64 additions & 0 deletions node_modules/fastest-levenshtein/test.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 0 additions & 21 deletions node_modules/leven/index.d.ts

This file was deleted.

0 comments on commit 32e88c9

Please sign in to comment.