Skip to content

Commit

Permalink
Always set npm.command to canonical command name
Browse files Browse the repository at this point in the history
This ensures that we get a npm_command env set to, eg 'run-script'
instead of the shorthand 'run'.

PR-URL: #2958
Credit: @isaacs
Close: #2958
Reviewed-by: @wraithgar
  • Loading branch information
isaacs authored and ruyadorno committed Mar 25, 2021
1 parent 95ba876 commit 59cf379
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lib/npm.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const proxyCmds = new Proxy({}, {
// old way of doing things, until we can make breaking changes to the
// npm.commands[x] api
target[actual] = new Proxy(
(args, cb) => npm[_runCmd](cmd, impl, args, cb),
(args, cb) => npm[_runCmd](actual, impl, args, cb),
{
get: (target, attr, receiver) => {
return Reflect.get(impl, attr, receiver)
Expand Down
9 changes: 7 additions & 2 deletions test/lib/npm.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ for (const env of Object.keys(process.env).filter(e => /^npm_/.test(e))) {
'should match "npm test" or "npm run test"'
)
} else
t.match(process.env[env], /^(run)|(run-script)|(exec)$/)
t.match(process.env[env], /^(run-script|exec)$/)
}
delete process.env[env]
}
Expand Down Expand Up @@ -411,10 +411,15 @@ t.test('npm.load', t => {
npm.localPrefix = dir

await new Promise((res, rej) => {
npm.commands['run-script']([], er => {
// verify that calling the command with a short name still sets
// the npm.command property to the full canonical name of the cmd.
npm.command = null
npm.commands.run([], er => {
if (er)
rej(er)

t.equal(npm.command, 'run-script', 'npm.command set to canonical name')

t.match(
consoleLogs,
[
Expand Down

0 comments on commit 59cf379

Please sign in to comment.