Skip to content

Commit c069622

Browse files
authored
fix: show full parent command path in subcommand usage errors (#9464)
backport of #9404
1 parent 39d034d commit c069622

4 files changed

Lines changed: 13 additions & 9 deletions

File tree

lib/base-cmd.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,14 +129,16 @@ class BaseCommand {
129129
}
130130

131131
fullUsage.push('')
132-
fullUsage.push(`Run "npm help ${name}" for more info`)
132+
const helpName = parentName ? parentName.split(' ')[0] : name
133+
fullUsage.push(`Run "npm help ${helpName}" for more info`)
133134

134135
return fullUsage.join('\n')
135136
}
136137

137138
constructor (npm) {
138139
this.npm = npm
139140
this.commandArgs = null
141+
this.parentName = null
140142

141143
const { config } = this
142144

@@ -167,7 +169,7 @@ class BaseCommand {
167169
}
168170

169171
get usage () {
170-
return this.constructor.describeUsage
172+
return this.constructor.getUsage(this.parentName)
171173
}
172174

173175
usageError (prefix = '') {

lib/npm.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,12 +252,13 @@ class Npm {
252252

253253
// Check if help is requested for the subcommand
254254
if (this.config.get('usage')) {
255-
const parentName = commandPath[0]
255+
const parentName = commandPath.join(' ')
256256
return output.standard(SubCommand.getUsage(parentName))
257257
}
258258

259259
// Create subcommand instance and recurse
260260
const subcommandInstance = new SubCommand(this)
261+
subcommandInstance.parentName = commandPath.join(' ')
261262
const subcommandArgs = args.slice(1) // Remove subcommand name from args
262263
const subcommandPath = [...commandPath, subcommandName]
263264

tap-snapshots/test/lib/commands/install.js.test.cjs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,8 @@ silly logfile done cleaning log files
134134
verbose stack Error: The developer of this package has specified the following through devEngines
135135
verbose stack Invalid devEngines.runtime
136136
verbose stack Invalid name "nondescript" does not match "node" for "runtime"
137-
verbose stack at Install.checkDevEngines ({CWD}/lib/base-cmd.js:247:27)
138-
verbose stack at MockNpm.execCommandClass ({CWD}/lib/npm.js:292:7)
137+
verbose stack at Install.checkDevEngines ({CWD}/lib/base-cmd.js:249:27)
138+
verbose stack at MockNpm.execCommandClass ({CWD}/lib/npm.js:293:7)
139139
verbose stack at MockNpm.exec ({CWD}/lib/npm.js:193:9)
140140
error code EBADDEVENGINES
141141
error EBADDEVENGINES The developer of this package has specified the following through devEngines
@@ -199,8 +199,8 @@ warn EBADDEVENGINES }
199199
verbose stack Error: The developer of this package has specified the following through devEngines
200200
verbose stack Invalid devEngines.runtime
201201
verbose stack Invalid name "nondescript" does not match "node" for "runtime"
202-
verbose stack at Install.checkDevEngines ({CWD}/lib/base-cmd.js:247:27)
203-
verbose stack at MockNpm.execCommandClass ({CWD}/lib/npm.js:292:7)
202+
verbose stack at Install.checkDevEngines ({CWD}/lib/base-cmd.js:249:27)
203+
verbose stack at MockNpm.execCommandClass ({CWD}/lib/npm.js:293:7)
204204
verbose stack at MockNpm.exec ({CWD}/lib/npm.js:193:9)
205205
error code EBADDEVENGINES
206206
error EBADDEVENGINES The developer of this package has specified the following through devEngines
@@ -225,8 +225,8 @@ silly logfile done cleaning log files
225225
verbose stack Error: The developer of this package has specified the following through devEngines
226226
verbose stack Invalid devEngines.runtime
227227
verbose stack Invalid name "nondescript" does not match "node" for "runtime"
228-
verbose stack at Install.checkDevEngines ({CWD}/lib/base-cmd.js:247:27)
229-
verbose stack at MockNpm.execCommandClass ({CWD}/lib/npm.js:292:7)
228+
verbose stack at Install.checkDevEngines ({CWD}/lib/base-cmd.js:249:27)
229+
verbose stack at MockNpm.execCommandClass ({CWD}/lib/npm.js:293:7)
230230
verbose stack at MockNpm.exec ({CWD}/lib/npm.js:193:9)
231231
error code EBADDEVENGINES
232232
error EBADDEVENGINES The developer of this package has specified the following through devEngines

test/lib/commands/stage/approve.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ t.test('throws usageError without stage-id', async t => {
2727
})
2828
await t.rejects(npm.exec('stage', ['approve']), {
2929
code: 'EUSAGE',
30+
message: /npm stage approve <stage-id>/,
3031
})
3132
})
3233

0 commit comments

Comments
 (0)