Skip to content

Commit

Permalink
restore npm.flatOptions.npmCommand
Browse files Browse the repository at this point in the history
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
  • Loading branch information
isaacs authored and ruyadorno committed Mar 18, 2021
1 parent 33c4189 commit d98edd1
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
5 changes: 4 additions & 1 deletion lib/npm.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,10 @@ const npm = module.exports = new class extends EventEmitter {
}

get flatOptions () {
return this.config.flat
const { flat } = this.config
if (this.command)
flat.npmCommand = this.command
return flat
}

get lockfileVersion () {
Expand Down
9 changes: 8 additions & 1 deletion test/lib/npm.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ const npmlog = require('npmlog')

const npmPath = resolve(__dirname, '..', '..')
const Config = require('@npmcli/config')
const { definitions, shorthands } = require('../../lib/utils/config')
const { definitions, shorthands, flatten } = require('../../lib/utils/config')
const freshConfig = (opts = {}) => {
for (const env of Object.keys(process.env).filter(e => /^npm_/.test(e)))
delete process.env[env]
Expand All @@ -55,6 +55,7 @@ const freshConfig = (opts = {}) => {
npmPath,
log: npmlog,
...opts,
flatten,
})
}

Expand Down Expand Up @@ -312,6 +313,9 @@ t.test('npm.load', t => {
if (er)
throw er

t.equal(npm.command, 'll', 'command set to first npm command')
t.equal(npm.flatOptions.npmCommand, 'll', 'npmCommand flatOption set')

t.same(consoleLogs, [[npm.commands.ll.usage]], 'print usage')
consoleLogs.length = 0
npm.config.set('usage', false)
Expand All @@ -323,6 +327,9 @@ t.test('npm.load', t => {
if (er)
throw er

t.strictSame([npm.command, npm.flatOptions.npmCommand], ['ll', 'll'],
'does not change npm.command when another command is called')

t.match(logs, [
[
'error',
Expand Down

0 comments on commit d98edd1

Please sign in to comment.