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',