Skip to content

Commit

Permalink
Set NODE_ENV=production if 'dev' is on the omit list
Browse files Browse the repository at this point in the history
Fix: #2021

PR-URL: #2331
Credit: @isaacs
Close: #2331
Reviewed-by: @ruyadorno @darcyclarke
  • Loading branch information
isaacs committed Dec 11, 2020
1 parent e340cf6 commit 3154491
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
7 changes: 7 additions & 0 deletions lib/utils/flat-options.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]
}

Expand Down
22 changes: 22 additions & 0 deletions test/lib/utils/flat-options.js
Original file line number Diff line number Diff line change
@@ -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')
Expand Down Expand Up @@ -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 => {
Expand All @@ -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()
})

Expand Down

0 comments on commit 3154491

Please sign in to comment.