From 59cf37962a2286e0f7d3bd37fa9c8bc3bac94218 Mon Sep 17 00:00:00 2001 From: isaacs Date: Wed, 24 Mar 2021 21:20:19 -0700 Subject: [PATCH] Always set npm.command to canonical command name This ensures that we get a npm_command env set to, eg 'run-script' instead of the shorthand 'run'. PR-URL: https://github.com/npm/cli/pull/2958 Credit: @isaacs Close: #2958 Reviewed-by: @wraithgar --- lib/npm.js | 2 +- test/lib/npm.js | 9 +++++++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/lib/npm.js b/lib/npm.js index 42541a90ffb79..5f8b2ff3d703d 100644 --- a/lib/npm.js +++ b/lib/npm.js @@ -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) diff --git a/test/lib/npm.js b/test/lib/npm.js index 57391939800b5..4d40792af8f38 100644 --- a/test/lib/npm.js +++ b/test/lib/npm.js @@ -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] } @@ -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, [