Skip to content

Commit 37cc8b5

Browse files
committed
Properly set npm_command environment variable.
Fix: #2015
1 parent 5b88b72 commit 37cc8b5

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

lib/npm.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,12 @@ const npm = module.exports = new class extends EventEmitter {
9393
}
9494

9595
process.emit('time', `command:${cmd}`)
96-
this.command = cmd
96+
// since 'test', 'start', 'stop', etc. commands re-enter this function
97+
// to call the run-script command, we need to only set it one time.
98+
if (!this.command) {
99+
process.env.npm_command = cmd
100+
this.command = cmd
101+
}
97102

98103
// Options are prefixed by a hyphen-minus (-, \u2d).
99104
// Other dash-type chars look similar but are invalid.
@@ -142,7 +147,6 @@ const npm = module.exports = new class extends EventEmitter {
142147
}
143148
if (!er && !this[_flatOptions]) {
144149
this[_flatOptions] = require('./utils/flat-options.js')(this)
145-
process.env.npm_command = this.command
146150
}
147151
process.emit('timeEnd', 'npm:load')
148152
this.emit('load', er)

test/lib/npm.js

+9
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,16 @@ const fs = require('fs')
44

55
// delete this so that we don't have configs from the fact that it
66
// is being run by 'npm test'
7+
const event = process.env.npm_lifecycle_event
78
for (const env of Object.keys(process.env).filter(e => /^npm_/.test(e))) {
9+
if (env === 'npm_command') {
10+
// should only be running this in the 'test' or 'run-script' command!
11+
// if the lifecycle event is 'test', then it'll be 'test', otherwise
12+
// it should always be run-script. Of course, it'll be missing if this
13+
// test is just run directly, which is also acceptable.
14+
const cmd = event === 'test' ? 'test' : 'run-script'
15+
t.match(process.env[env], cmd)
16+
}
817
delete process.env[env]
918
}
1019

0 commit comments

Comments
 (0)