Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Properly set npm_command environment variable. #2016

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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