Skip to content

Commit

Permalink
Properly set npm_command environment variable.
Browse files Browse the repository at this point in the history
Fix: #2015

PR-URL: #2016
Credit: @isaacs
Close: #2016
Reviewed-by: @ruyadorno
  • Loading branch information
isaacs committed Oct 23, 2020
1 parent cc026da commit 59e8dd6
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
8 changes: 6 additions & 2 deletions lib/npm.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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)
Expand Down
9 changes: 9 additions & 0 deletions test/lib/npm.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]
}

Expand Down

0 comments on commit 59e8dd6

Please sign in to comment.