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
  • Loading branch information
wraithgar committed Aug 11, 2021
1 parent 47a3d18 commit d49029f
Show file tree
Hide file tree
Showing 13 changed files with 314 additions and 234 deletions.
8 changes: 4 additions & 4 deletions lib/utils/did-you-mean.js
Original file line number Diff line number Diff line change
@@ -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.

Loading

0 comments on commit d49029f

Please sign in to comment.