Skip to content

Commit aa94a1c

Browse files
committed
fix(suggestions): clarify Unknown command output
Base commands and `npm run` need different outputs
1 parent d98edd1 commit aa94a1c

File tree

6 files changed

+22
-27
lines changed

6 files changed

+22
-27
lines changed

lib/cli.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,14 +66,12 @@ module.exports = (process) => {
6666
npm.log.level = 'silent'
6767
if (cmd) {
6868
const didYouMean = require('./utils/did-you-mean.js')
69-
console.error(npm.localPrefix)
7069
const suggestions = await didYouMean(npm, npm.localPrefix, cmd)
71-
npm.output(suggestions)
70+
npm.output(`Unknown command: "${cmd}"${suggestions}`)
7271
} else
7372
npm.output(npm.usage)
7473
process.exitCode = 1
7574
} catch (err) {
76-
console.error(err)
7775
errorHandler(err)
7876
}
7977
}

lib/run-script.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ class RunScript extends BaseCommand {
9292
return
9393

9494
const suggestions = await didYouMean(this.npm, path, event)
95-
throw new Error(suggestions)
95+
throw new Error(`Missing script: "${event}"${suggestions}`)
9696
}
9797

9898
// positional args only added to the main event, not pre/post
@@ -228,7 +228,7 @@ class RunScript extends BaseCommand {
228228
log.error(` in workspace: ${pkg._id || pkg.name}`)
229229
log.error(` at location: ${workspacePath}`)
230230

231-
const scriptMissing = err.message.startsWith('Unknown command')
231+
const scriptMissing = err.message.startsWith('Missing script')
232232

233233
// avoids exiting with error code in case there's scripts missing
234234
// in some workspaces since other scripts might have succeeded

lib/utils/did-you-mean.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,11 @@ const didYouMean = async (npm, path, scmd) => {
2323

2424
const best = [...bestCmd, ...bestRun, ...bestBin]
2525

26-
const suggestion = best.length === 0 ? ''
27-
: best.length === 1 ? `\n\nDid you mean this?\n${best[0]}`
26+
if (best.length === 0)
27+
return ''
28+
29+
const suggestion = best.length === 1 ? `\n\nDid you mean this?\n${best[0]}`
2830
: `\n\nDid you mean one of these?\n${best.slice(0, 3).join('\n')}`
29-
const result = `Unknown command: "${scmd}"${suggestion}`
30-
return result
31+
return suggestion
3132
}
3233
module.exports = didYouMean

test/lib/cli.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ const npmlogMock = {
4646
const requireInject = require('require-inject')
4747
const cli = requireInject.installGlobally('../../lib/cli.js', {
4848
'../../lib/npm.js': npmock,
49-
'../../lib/utils/did-you-mean.js': () => 'test did you mean',
49+
'../../lib/utils/did-you-mean.js': () => '\ntest did you mean',
5050
'../../lib/utils/unsupported.js': unsupportedMock,
5151
'../../lib/utils/error-handler.js': errorHandlerMock,
5252
npmlog: npmlogMock,
@@ -160,7 +160,7 @@ t.test('print usage if non-command param provided', t => {
160160
npmock.argv = ['asdf']
161161
npmock.output = (msg) => {
162162
if (msg) {
163-
t.match(msg, 'test did you mean', 'outputs did you mean')
163+
t.match(msg, 'Unknown command: "asdf"\ntest did you mean', 'outputs did you mean')
164164
t.end()
165165
}
166166
}

test/lib/run-script.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -290,15 +290,15 @@ t.test('try to run missing script', t => {
290290
t.test('no suggestions', t => {
291291
runScript.exec(['notevenclose'], er => {
292292
t.match(er, {
293-
message: 'Unknown command: "notevenclose"',
293+
message: 'Missing script: "notevenclose"',
294294
})
295295
t.end()
296296
})
297297
})
298298
t.test('script suggestions', t => {
299299
runScript.exec(['helo'], er => {
300300
t.match(er, {
301-
message: 'Unknown command: "helo"',
301+
message: 'Missing script: "helo"',
302302
})
303303
t.match(er, {
304304
message: 'npm run hello',
@@ -309,7 +309,7 @@ t.test('try to run missing script', t => {
309309
t.test('bin suggestions', t => {
310310
runScript.exec(['goodneght'], er => {
311311
t.match(er, {
312-
message: 'Unknown command: "goodneght"',
312+
message: 'Missing script: "goodneght"',
313313
})
314314
t.match(er, {
315315
message: 'npm exec goodnight',
@@ -896,27 +896,27 @@ t.test('workspaces', t => {
896896
t.match(RUN_SCRIPTS, [])
897897
t.strictSame(LOG.map(cleanOutput), [
898898
'Lifecycle script `missing-script` failed with error:',
899-
'Error: Unknown command: "missing-script"',
899+
'Error: Missing script: "missing-script"',
900900
' in workspace: [email protected]',
901901
' at location: {CWD}/test/lib/run-script-workspaces/packages/a',
902902
'Lifecycle script `missing-script` failed with error:',
903-
'Error: Unknown command: "missing-script"',
903+
'Error: Missing script: "missing-script"',
904904
' in workspace: [email protected]',
905905
' at location: {CWD}/test/lib/run-script-workspaces/packages/b',
906906
'Lifecycle script `missing-script` failed with error:',
907-
'Error: Unknown command: "missing-script"',
907+
'Error: Missing script: "missing-script"',
908908
' in workspace: [email protected]',
909909
' at location: {CWD}/test/lib/run-script-workspaces/packages/c',
910910
'Lifecycle script `missing-script` failed with error:',
911-
'Error: Unknown command: "missing-script"',
911+
'Error: Missing script: "missing-script"',
912912
' in workspace: [email protected]',
913913
' at location: {CWD}/test/lib/run-script-workspaces/packages/d',
914914
'Lifecycle script `missing-script` failed with error:',
915-
'Error: Unknown command: "missing-script"',
915+
'Error: Missing script: "missing-script"',
916916
' in workspace: e',
917917
' at location: {CWD}/test/lib/run-script-workspaces/packages/e',
918918
'Lifecycle script `missing-script` failed with error:',
919-
'Error: Unknown command: "missing-script"',
919+
'Error: Missing script: "missing-script"',
920920
' in workspace: [email protected]',
921921
' at location: {CWD}/test/lib/run-script-workspaces/packages/noscripts',
922922
], 'should log error msgs for each workspace script')
@@ -937,11 +937,11 @@ t.test('workspaces', t => {
937937
t.match(RUN_SCRIPTS, [])
938938
t.strictSame(LOG.map(cleanOutput), [
939939
'Lifecycle script `test` failed with error:',
940-
'Error: Unknown command: "test"',
940+
'Error: Missing script: "test"',
941941
' in workspace: [email protected]',
942942
' at location: {CWD}/test/lib/run-script-workspaces/packages/a',
943943
'Lifecycle script `test` failed with error:',
944-
'Error: Unknown command: "test"',
944+
'Error: Missing script: "test"',
945945
' in workspace: [email protected]',
946946
' at location: {CWD}/test/lib/run-script-workspaces/packages/b',
947947
], 'should log error msgs for each workspace script')

test/lib/utils/did-you-mean.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,20 @@ t.test('did-you-mean', t => {
88
t.notOk(err)
99
t.test('nistall', async t => {
1010
const result = await dym(npm, npm.localPrefix, 'nistall')
11-
t.match(result, 'Unknown command')
1211
t.match(result, 'npm install')
1312
})
1413
t.test('sttest', async t => {
1514
const result = await dym(npm, npm.localPrefix, 'sttest')
16-
t.match(result, 'Unknown command')
1715
t.match(result, 'npm test')
1816
t.match(result, 'npm run posttest')
1917
})
2018
t.test('npz', async t => {
2119
const result = await dym(npm, npm.localPrefix, 'npxx')
22-
t.match(result, 'Unknown command')
2320
t.match(result, 'npm exec npx')
2421
})
2522
t.test('qwuijbo', async t => {
2623
const result = await dym(npm, npm.localPrefix, 'qwuijbo')
27-
t.match(result, 'Unknown command')
24+
t.match(result, '')
2825
})
2926
t.end()
3027
})
@@ -38,6 +35,5 @@ t.test('missing bin and script properties', async t => {
3835
})
3936

4037
const result = await dym(npm, path, 'nistall')
41-
t.match(result, 'Unknown command')
4238
t.match(result, 'npm install')
4339
})

0 commit comments

Comments
 (0)