-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(refactor): create new error output primitives (#7515)
These will be used to generate normal and json error messages in the same format from both commands and the exit handler. This also does a few others things: - makes `did-you-mean` take a package so it can be sync and called more easily from the error handlers - standardize all error messages with 2 space indentation to match the rest of npm
- Loading branch information
1 parent
e40454c
commit b54cdb8
Showing
11 changed files
with
408 additions
and
387 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,34 @@ | ||
const Npm = require('../npm') | ||
const { distance } = require('fastest-levenshtein') | ||
const pkgJson = require('@npmcli/package-json') | ||
const { commands } = require('./cmd-list.js') | ||
|
||
const didYouMean = async (path, scmd) => { | ||
const close = commands.filter(cmd => distance(scmd, cmd) < scmd.length * 0.4 && scmd !== cmd) | ||
let best = [] | ||
for (const str of close) { | ||
const cmd = Npm.cmd(str) | ||
best.push(` npm ${str} # ${cmd.description}`) | ||
} | ||
// We would already be suggesting this in `npm x` so omit them here | ||
const runScripts = ['stop', 'start', 'test', 'restart'] | ||
try { | ||
const { content: { scripts, bin } } = await pkgJson.normalize(path) | ||
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) | ||
/* eslint-disable-next-line max-len */ | ||
.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 | ||
} | ||
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)) | ||
/* eslint-disable-next-line max-len */ | ||
.map(str => [`exec ${str}`, `run the "${str}" command from either this or a remote npm package`]), | ||
] | ||
|
||
if (best.length === 0) { | ||
return '' | ||
} | ||
|
||
return best.length === 1 | ||
? `\n\nDid you mean this?\n${best[0]}` | ||
: `\n\nDid you mean one of these?\n${best.slice(0, 3).join('\n')}` | ||
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') | ||
} | ||
|
||
module.exports = didYouMean |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.