diff --git a/lib/utils/flat-options.js b/lib/utils/flat-options.js index 9b83de8c449fa..71edca0718c78 100644 --- a/lib/utils/flat-options.js +++ b/lib/utils/flat-options.js @@ -27,6 +27,13 @@ const buildOmitList = obj => { omit.add('optional') obj.omit = [...omit] + + // it would perhaps make more sense to put this in @npmcli/config, but + // since we can set dev to be omitted in multiple various legacy ways, + // it's better to set it here once it's all resolved. + if (obj.omit.includes('dev')) + process.env.NODE_ENV = 'production' + return [...omit] } diff --git a/test/lib/utils/flat-options.js b/test/lib/utils/flat-options.js index 3cbf06a48b9ff..ee7620fa784db 100644 --- a/test/lib/utils/flat-options.js +++ b/test/lib/utils/flat-options.js @@ -1,6 +1,7 @@ const t = require('tap') process.env.NODE = '/path/to/some/node' +process.env.NODE_ENV = 'development' const logs = [] const log = require('npmlog') @@ -195,43 +196,56 @@ t.test('tag emits warning', t => { t.test('omit/include options', t => { t.test('omit explicitly', t => { + const { NODE_ENV } = process.env const npm = new Mocknpm({ omit: ['dev', 'optional', 'peer'], }) t.strictSame(flatOptions(npm).omit, ['dev', 'optional', 'peer']) + t.equal(process.env.NODE_ENV, 'production') + process.env.NODE_ENV = NODE_ENV t.end() }) t.test('omit and include some', t => { + const { NODE_ENV } = process.env const npm = new Mocknpm({ omit: ['dev', 'optional', 'peer'], include: ['peer'], }) t.strictSame(flatOptions(npm).omit, ['dev', 'optional']) + t.equal(process.env.NODE_ENV, 'production') + process.env.NODE_ENV = NODE_ENV t.end() }) t.test('dev flag', t => { + const { NODE_ENV } = process.env const npm = new Mocknpm({ omit: ['dev', 'optional', 'peer'], include: [], dev: true, }) t.strictSame(flatOptions(npm).omit, ['optional', 'peer']) + t.equal(process.env.NODE_ENV, NODE_ENV) + process.env.NODE_ENV = NODE_ENV t.end() }) t.test('production flag', t => { + const { NODE_ENV } = process.env const npm = new Mocknpm({ omit: [], include: [], production: true, }) t.strictSame(flatOptions(npm).omit, ['dev']) + t.equal(process.env.NODE_ENV, 'production') + process.env.NODE_ENV = NODE_ENV t.end() }) t.test('only', t => { + const { NODE_ENV } = process.env const cases = ['prod', 'production'] t.plan(cases.length) cases.forEach(c => t.test(c, t => { @@ -241,26 +255,34 @@ t.test('omit/include options', t => { only: c, }) t.strictSame(flatOptions(npm).omit, ['dev']) + t.equal(process.env.NODE_ENV, 'production') + process.env.NODE_ENV = NODE_ENV t.end() })) }) t.test('also dev', t => { + const { NODE_ENV } = process.env const npm = new Mocknpm({ omit: ['dev', 'optional', 'peer'], also: 'dev', }) t.strictSame(flatOptions(npm).omit, ['optional', 'peer']) + t.equal(process.env.NODE_ENV, NODE_ENV) + process.env.NODE_ENV = NODE_ENV t.end() }) t.test('no-optional', t => { + const { NODE_ENV } = process.env const npm = new Mocknpm({ optional: false, omit: null, include: null, }) t.strictSame(flatOptions(npm).omit, ['optional']) + t.equal(process.env.NODE_ENV, NODE_ENV) + process.env.NODE_ENV = NODE_ENV t.end() })