diff options
author | Gar <gar+gh@danger.computer> | 2021-03-19 17:33:40 +0300 |
---|---|---|
committer | Ruy Adorno <ruyadorno@hotmail.com> | 2021-03-19 18:36:06 +0300 |
commit | e94a45851dcfa08d5ce92b6f17cd868acae94ee4 (patch) | |
tree | bd2aa35afa648119478d74b25c0ea8a8cd41e991 /lib | |
parent | d98edd124a61d2331f45599ba6f6dfc42f07cbce (diff) |
fix(suggestions): clarify Unknown command output
Base commands and `npm run` need different outputs
PR-URL: https://github.com/npm/cli/pull/2906
Credit: @wraithgar
Close: #2906
Reviewed-by: @ruyadorno
Diffstat (limited to 'lib')
-rw-r--r-- | lib/cli.js | 4 | ||||
-rw-r--r-- | lib/run-script.js | 4 | ||||
-rw-r--r-- | lib/utils/did-you-mean.js | 9 |
3 files changed, 8 insertions, 9 deletions
diff --git a/lib/cli.js b/lib/cli.js index 086335725..087922465 100644 --- a/lib/cli.js +++ b/lib/cli.js @@ -66,14 +66,12 @@ module.exports = (process) => { npm.log.level = 'silent' if (cmd) { const didYouMean = require('./utils/did-you-mean.js') - console.error(npm.localPrefix) const suggestions = await didYouMean(npm, npm.localPrefix, cmd) - npm.output(suggestions) + npm.output(`Unknown command: "${cmd}"${suggestions}`) } else npm.output(npm.usage) process.exitCode = 1 } catch (err) { - console.error(err) errorHandler(err) } } diff --git a/lib/run-script.js b/lib/run-script.js index e59340225..0f4c40b0d 100644 --- a/lib/run-script.js +++ b/lib/run-script.js @@ -92,7 +92,7 @@ class RunScript extends BaseCommand { return const suggestions = await didYouMean(this.npm, path, event) - throw new Error(suggestions) + throw new Error(`Missing script: "${event}"${suggestions}`) } // positional args only added to the main event, not pre/post @@ -228,7 +228,7 @@ class RunScript extends BaseCommand { log.error(` in workspace: ${pkg._id || pkg.name}`) log.error(` at location: ${workspacePath}`) - const scriptMissing = err.message.startsWith('Unknown command') + const scriptMissing = err.message.startsWith('Missing script') // avoids exiting with error code in case there's scripts missing // in some workspaces since other scripts might have succeeded diff --git a/lib/utils/did-you-mean.js b/lib/utils/did-you-mean.js index 5e41af67a..98133196e 100644 --- a/lib/utils/did-you-mean.js +++ b/lib/utils/did-you-mean.js @@ -23,10 +23,11 @@ const didYouMean = async (npm, path, scmd) => { const best = [...bestCmd, ...bestRun, ...bestBin] - const suggestion = best.length === 0 ? '' - : best.length === 1 ? `\n\nDid you mean this?\n${best[0]}` + if (best.length === 0) + return '' + + const suggestion = 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')}` - const result = `Unknown command: "${scmd}"${suggestion}` - return result + return suggestion } module.exports = didYouMean |