Skip to content

Commit

Permalink
Properly set npm_command environment variable.
Browse files Browse the repository at this point in the history
Fix: #2015
  • Loading branch information
isaacs committed Oct 22, 2020
1 parent 5b88b72 commit c009e0d
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 @@ -5,6 +5,15 @@ const fs = require('fs')
// delete this so that we don't have configs from the fact that it
// is being run by 'npm test'
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 = process.env.npm_lifecycle_event === 'test' ? 'test'
: 'run-script'
t.match(process.env[env], cmd)
}
delete process.env[env]
}

Expand Down

0 comments on commit c009e0d

Please sign in to comment.