Skip to content

Commit a8dd25c

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

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

lib/npm.js

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

9595
process.emit('time', `command:${cmd}`)
96-
this.command = cmd
96+
if (!this.command) {
97+
process.env.npm_command = cmd
98+
this.command = cmd
99+
}
97100

98101
// Options are prefixed by a hyphen-minus (-, \u2d).
99102
// Other dash-type chars look similar but are invalid.
@@ -142,7 +145,6 @@ const npm = module.exports = new class extends EventEmitter {
142145
}
143146
if (!er && !this[_flatOptions]) {
144147
this[_flatOptions] = require('./utils/flat-options.js')(this)
145-
process.env.npm_command = this.command
146148
}
147149
process.emit('timeEnd', 'npm:load')
148150
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)