Skip to content

Commit 30feaa9

Browse files
committed
Properly set npm_command environment variable.
Fix: #2015
1 parent 627cf80 commit 30feaa9

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

0 commit comments

Comments
 (0)