From d98edd124a61d2331f45599ba6f6dfc42f07cbce Mon Sep 17 00:00:00 2001 From: isaacs Date: Thu, 18 Mar 2021 14:50:50 -0700 Subject: [PATCH] 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: https://github.com/npm/cli/pull/2903 Credit: @isaacs Close: #2903 Reviewed-by: @wraithgar --- lib/npm.js | 5 ++++- test/lib/npm.js | 9 ++++++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/lib/npm.js b/lib/npm.js index bc65cc79b9717..42541a90ffb79 100644 --- a/lib/npm.js +++ b/lib/npm.js @@ -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 () { diff --git a/test/lib/npm.js b/test/lib/npm.js index de0dcaa1c198e..57391939800b5 100644 --- a/test/lib/npm.js +++ b/test/lib/npm.js @@ -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] @@ -55,6 +55,7 @@ const freshConfig = (opts = {}) => { npmPath, log: npmlog, ...opts, + flatten, }) } @@ -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) @@ -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',