Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/npm/cli.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorGar <gar+gh@danger.computer>2021-09-13 20:31:02 +0300
committerGar <gar+gh@danger.computer>2021-09-13 22:34:39 +0300
commit59743972c2ae1d2dd601aaa6c59974c686b1cb29 (patch)
tree03f67502064dd7947dcebbb9d41b00e182cbb85d /lib
parentac8e4ad18a6b726dd2c3abcb0f605701cca0ae2c (diff)
fix(did-you-mean): succeed if cwd is not a package
The did-you-mean code was trying to parse a local package.json to suggest scripts and bins, which was causing an exception if you ran npm outside of a directory with a valid package.json. This fixes that. PR-URL: https://github.com/npm/cli/pull/3747 Credit: @wraithgar Close: #3747 Reviewed-by: @nlf
Diffstat (limited to 'lib')
-rw-r--r--lib/utils/did-you-mean.js29
1 files changed, 15 insertions, 14 deletions
diff --git a/lib/utils/did-you-mean.js b/lib/utils/did-you-mean.js
index 0cfdd0352..c324253af 100644
--- a/lib/utils/did-you-mean.js
+++ b/lib/utils/did-you-mean.js
@@ -3,25 +3,26 @@ const readJson = require('read-package-json-fast')
const { cmdList } = require('./cmd-list.js')
const didYouMean = async (npm, path, scmd) => {
- const bestCmd = cmdList
+ let best = cmdList
.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 => 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 => 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]
+ try {
+ const { bin, scripts } = await readJson(`${path}/package.json`)
+ best = best.concat(
+ Object.keys(scripts || {})
+ .filter(cmd => distance(scmd, cmd) < scmd.length * 0.4 &&
+ !runScripts.includes(cmd))
+ .map(str => ` npm run ${str} # run the "${str}" package script`),
+ Object.keys(bin || {})
+ .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`)
+ )
+ } catch (_) {
+ // gracefully ignore not being in a folder w/ a package.json
+ }
if (best.length === 0)
return ''