Skip to content

Commit d856497

Browse files
committed
smarter DOTENV_CONFIG_QUIET option that can read from source .env file
1 parent 203c4e8 commit d856497

File tree

3 files changed

+128
-97
lines changed

3 files changed

+128
-97
lines changed

lib/main.js

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ const path = require('path')
33
const os = require('os')
44
const crypto = require('crypto')
55
const packageJson = require('../package.json')
6-
const envOptions = require('./env-options')
76

87
const version = packageJson.version
98

@@ -26,6 +25,13 @@ function _getRandomTip () {
2625
return TIPS[Math.floor(Math.random() * TIPS.length)]
2726
}
2827

28+
function parseBoolean (value) {
29+
if (typeof value === 'string') {
30+
return !['false', '0', 'no', 'off', ''].includes(value.toLowerCase())
31+
}
32+
return Boolean(value)
33+
}
34+
2935
function supportsAnsi () {
3036
return process.stdout.isTTY // && process.env.TERM !== 'dumb'
3137
}
@@ -217,8 +223,8 @@ function _resolveHome (envPath) {
217223
}
218224

219225
function _configVault (options) {
220-
const debug = Boolean(options && options.debug)
221-
const quiet = Boolean(options && options.quiet)
226+
const debug = parseBoolean(process.env.DOTENV_CONFIG_DEBUG || (options && options.debug))
227+
const quiet = parseBoolean(process.env.DOTENV_CONFIG_QUIET || (options && options.quiet))
222228

223229
if (debug || !quiet) {
224230
_log('Loading env from encrypted .env.vault')
@@ -239,8 +245,8 @@ function _configVault (options) {
239245
function configDotenv (options) {
240246
const dotenvPath = path.resolve(process.cwd(), '.env')
241247
let encoding = 'utf8'
242-
const debug = Boolean(options && options.debug)
243-
const quiet = Boolean(options && options.quiet)
248+
let debug = parseBoolean(process.env.DOTENV_CONFIG_DEBUG || (options && options.debug))
249+
let quiet = parseBoolean(process.env.DOTENV_CONFIG_QUIET || (options && options.quiet))
244250

245251
if (options && options.encoding) {
246252
encoding = options.encoding
@@ -287,6 +293,10 @@ function configDotenv (options) {
287293

288294
const populated = DotenvModule.populate(processEnv, parsedAll, options)
289295

296+
// handle user settings DOTENV_CONFIG_ options inside .env file(s)
297+
debug = parseBoolean(processEnv.DOTENV_CONFIG_DEBUG || debug)
298+
quiet = parseBoolean(process.env.DOTENV_CONFIG_QUIET || quiet)
299+
290300
if (debug || !quiet) {
291301
const keysCount = Object.keys(populated).length
292302
const shortPaths = []
@@ -314,23 +324,21 @@ function configDotenv (options) {
314324

315325
// Populates process.env from .env file
316326
function config (options) {
317-
const mergedOptions = Object.assign({}, envOptions, options)
318-
319327
// fallback to original dotenv if DOTENV_KEY is not set
320-
if (_dotenvKey(mergedOptions).length === 0) {
321-
return DotenvModule.configDotenv(mergedOptions)
328+
if (_dotenvKey(options).length === 0) {
329+
return DotenvModule.configDotenv(options)
322330
}
323331

324-
const vaultPath = _vaultPath(mergedOptions)
332+
const vaultPath = _vaultPath(options)
325333

326334
// dotenvKey exists but .env.vault file does not exist
327335
if (!vaultPath) {
328336
_warn(`You set DOTENV_KEY but you are missing a .env.vault file at ${vaultPath}. Did you forget to build it?`)
329337

330-
return DotenvModule.configDotenv(mergedOptions)
338+
return DotenvModule.configDotenv(options)
331339
}
332340

333-
return DotenvModule._configVault(mergedOptions)
341+
return DotenvModule._configVault(options)
334342
}
335343

336344
function decrypt (encrypted, keyStr) {

tests/test-config-vault.js

Lines changed: 11 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -25,60 +25,6 @@ t.afterEach(() => {
2525
}
2626
})
2727

28-
t.test('logs when no path is set', ct => {
29-
ct.plan(1)
30-
31-
logStub = sinon.stub(console, 'log')
32-
33-
dotenv.config()
34-
ct.ok(logStub.called)
35-
})
36-
37-
t.test('does log by default', ct => {
38-
ct.plan(1)
39-
40-
logStub = sinon.stub(console, 'log')
41-
42-
dotenv.config({ path: testPath })
43-
ct.ok(logStub.called)
44-
})
45-
46-
t.test('does not log if quiet flag passed true', ct => {
47-
ct.plan(1)
48-
49-
logStub = sinon.stub(console, 'log')
50-
51-
dotenv.config({ path: testPath, quiet: true })
52-
ct.ok(logStub.notCalled)
53-
})
54-
55-
t.test('does log if quiet flag false', ct => {
56-
ct.plan(1)
57-
58-
logStub = sinon.stub(console, 'log')
59-
60-
dotenv.config({ path: testPath, quiet: false })
61-
ct.ok(logStub.called)
62-
})
63-
64-
t.test('does log if quiet flag present and undefined/null', ct => {
65-
ct.plan(1)
66-
67-
logStub = sinon.stub(console, 'log')
68-
69-
dotenv.config({ path: testPath, quiet: undefined })
70-
ct.ok(logStub.called)
71-
})
72-
73-
t.test('logs if debug set', ct => {
74-
ct.plan(1)
75-
76-
logStub = sinon.stub(console, 'log')
77-
78-
dotenv.config({ path: testPath, debug: true })
79-
ct.ok(logStub.called)
80-
})
81-
8228
t.test('does log when testPath calls to .env.vault directly (interpret what the user meant)', ct => {
8329
ct.plan(1)
8430

@@ -88,18 +34,10 @@ t.test('does log when testPath calls to .env.vault directly (interpret what the
8834
ct.ok(logStub.called)
8935
})
9036

91-
t.test('logs when testPath calls to .env.vault directly (interpret what the user meant) and debug true', ct => {
92-
ct.plan(1)
93-
94-
logStub = sinon.stub(console, 'log')
95-
96-
dotenv.config({ path: `${testPath}.vault`, debug: true })
97-
ct.ok(logStub.called)
98-
})
99-
10037
t.test('warns if DOTENV_KEY exists but .env.vault does not exist', ct => {
10138
ct.plan(1)
10239

40+
const testPath = 'tests/.env'
10341
logStub = sinon.stub(console, 'log')
10442

10543
const existsSync = sinon.stub(fs, 'existsSync').returns(false) // make .env.vault not exist
@@ -113,6 +51,7 @@ t.test('warns if DOTENV_KEY exists but .env.vault does not exist', ct => {
11351
t.test('warns if DOTENV_KEY exists but .env.vault does not exist (set as array)', ct => {
11452
ct.plan(1)
11553

54+
const testPath = 'tests/.env'
11655
logStub = sinon.stub(console, 'log')
11756

11857
const existsSync = sinon.stub(fs, 'existsSync').returns(false) // make .env.vault not exist
@@ -123,6 +62,15 @@ t.test('warns if DOTENV_KEY exists but .env.vault does not exist (set as array)'
12362
ct.end()
12463
})
12564

65+
t.test('logs when testPath calls to .env.vault directly (interpret what the user meant) and debug true', ct => {
66+
ct.plan(1)
67+
68+
logStub = sinon.stub(console, 'log')
69+
70+
dotenv.config({ path: `${testPath}.vault`, debug: true })
71+
ct.ok(logStub.called)
72+
})
73+
12674
t.test('returns parsed object', ct => {
12775
ct.plan(1)
12876

0 commit comments

Comments
 (0)