From 37cc8b515848238f6c781ac1ccde90bf7d58600d Mon Sep 17 00:00:00 2001 From: isaacs Date: Thu, 22 Oct 2020 09:30:36 -0700 Subject: [PATCH] Properly set npm_command environment variable. Fix: #2015 --- lib/npm.js | 8 ++++++-- test/lib/npm.js | 9 +++++++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/lib/npm.js b/lib/npm.js index d460ea674705e..c2a172cf05112 100644 --- a/lib/npm.js +++ b/lib/npm.js @@ -93,7 +93,12 @@ const npm = module.exports = new class extends EventEmitter { } process.emit('time', `command:${cmd}`) - this.command = cmd + // since 'test', 'start', 'stop', etc. commands re-enter this function + // to call the run-script command, we need to only set it one time. + if (!this.command) { + process.env.npm_command = cmd + this.command = cmd + } // Options are prefixed by a hyphen-minus (-, \u2d). // Other dash-type chars look similar but are invalid. @@ -142,7 +147,6 @@ const npm = module.exports = new class extends EventEmitter { } if (!er && !this[_flatOptions]) { this[_flatOptions] = require('./utils/flat-options.js')(this) - process.env.npm_command = this.command } process.emit('timeEnd', 'npm:load') this.emit('load', er) diff --git a/test/lib/npm.js b/test/lib/npm.js index 2968178495907..b2be5543fa1ea 100644 --- a/test/lib/npm.js +++ b/test/lib/npm.js @@ -4,7 +4,16 @@ const fs = require('fs') // delete this so that we don't have configs from the fact that it // is being run by 'npm test' +const event = process.env.npm_lifecycle_event for (const env of Object.keys(process.env).filter(e => /^npm_/.test(e))) { + if (env === 'npm_command') { + // should only be running this in the 'test' or 'run-script' command! + // if the lifecycle event is 'test', then it'll be 'test', otherwise + // it should always be run-script. Of course, it'll be missing if this + // test is just run directly, which is also acceptable. + const cmd = event === 'test' ? 'test' : 'run-script' + t.match(process.env[env], cmd) + } delete process.env[env] }