diff --git a/lib/utils/flat-options.js b/lib/utils/flat-options.js index 9fbdfdbb51325..bfe6316c1f10f 100644 --- a/lib/utils/flat-options.js +++ b/lib/utils/flat-options.js @@ -51,7 +51,11 @@ const flatOptions = npm => npm.flatOptions || Object.freeze({ projectScope: npm.projectScope, npmVersion: npm.version, nodeVersion: npm.config.get('node-version'), - npmCommand: npm.command, + // npm.command is not set until AFTER flatOptions are defined + // so we need to make this a getter. + get npmCommand () { + return npm.command + }, tmp: npm.tmp, cache: join(npm.config.get('cache'), '_cacache'), diff --git a/tap-snapshots/test-lib-utils-flat-options.js-TAP.test.js b/tap-snapshots/test-lib-utils-flat-options.js-TAP.test.js index 27ad10e5ed8ac..a5275eedbac5c 100644 --- a/tap-snapshots/test-lib-utils-flat-options.js-TAP.test.js +++ b/tap-snapshots/test-lib-utils-flat-options.js-TAP.test.js @@ -67,7 +67,7 @@ Object { "nodeBin": "/path/to/some/node", "nodeVersion": "1.2.3", "npmBin": "/path/to/npm/bin.js", - "npmCommand": "test", + "npmCommand": null, "npmSession": "12345", "npmVersion": "7.6.5", "offline": "offline", diff --git a/test/lib/utils/flat-options.js b/test/lib/utils/flat-options.js index 8ababa9826b71..1d97845f3c667 100644 --- a/test/lib/utils/flat-options.js +++ b/test/lib/utils/flat-options.js @@ -17,7 +17,7 @@ class Mocknpm { this.color = true this.projectScope = '@npmcli' this.tmp = '/tmp' - this.command = 'test' + this.command = null this.globalPrefix = '/usr/local' this.localPrefix = '/path/to/npm/cli' this.prefix = this.localPrefix @@ -138,6 +138,9 @@ t.test('basic', t => { cache: generatedFlat.cache.replace(/\\/g, '/') } t.matchSnapshot(clean, 'flat options') + t.equal(generatedFlat.npmCommand, null, 'command not set yet') + npm.command = 'view' + t.equal(generatedFlat.npmCommand, 'view', 'command updated via getter') t.equal(generatedFlat.npmBin, require.main.filename) // test the object is frozen generatedFlat.newField = 'asdf'