Skip to content

Commit d98edd1

Browse files
isaacsruyadorno
authored andcommitted
restore npm.flatOptions.npmCommand
This is used by npm-registry-fetch to set the 'npm-command' HTTP header. Will be cleaned up in a less ugly way in config refactor part 2. PR-URL: #2903 Credit: @isaacs Close: #2903 Reviewed-by: @wraithgar
1 parent 33c4189 commit d98edd1

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

lib/npm.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,10 @@ const npm = module.exports = new class extends EventEmitter {
213213
}
214214

215215
get flatOptions () {
216-
return this.config.flat
216+
const { flat } = this.config
217+
if (this.command)
218+
flat.npmCommand = this.command
219+
return flat
217220
}
218221

219222
get lockfileVersion () {

test/lib/npm.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ const npmlog = require('npmlog')
4242

4343
const npmPath = resolve(__dirname, '..', '..')
4444
const Config = require('@npmcli/config')
45-
const { definitions, shorthands } = require('../../lib/utils/config')
45+
const { definitions, shorthands, flatten } = require('../../lib/utils/config')
4646
const freshConfig = (opts = {}) => {
4747
for (const env of Object.keys(process.env).filter(e => /^npm_/.test(e)))
4848
delete process.env[env]
@@ -55,6 +55,7 @@ const freshConfig = (opts = {}) => {
5555
npmPath,
5656
log: npmlog,
5757
...opts,
58+
flatten,
5859
})
5960
}
6061

@@ -312,6 +313,9 @@ t.test('npm.load', t => {
312313
if (er)
313314
throw er
314315

316+
t.equal(npm.command, 'll', 'command set to first npm command')
317+
t.equal(npm.flatOptions.npmCommand, 'll', 'npmCommand flatOption set')
318+
315319
t.same(consoleLogs, [[npm.commands.ll.usage]], 'print usage')
316320
consoleLogs.length = 0
317321
npm.config.set('usage', false)
@@ -323,6 +327,9 @@ t.test('npm.load', t => {
323327
if (er)
324328
throw er
325329

330+
t.strictSame([npm.command, npm.flatOptions.npmCommand], ['ll', 'll'],
331+
'does not change npm.command when another command is called')
332+
326333
t.match(logs, [
327334
[
328335
'error',

0 commit comments

Comments
 (0)