Skip to content

Commit 78996af

Browse files
committed
Add override cli and env options
1 parent 18b6e1c commit 78996af

File tree

5 files changed

+20
-5
lines changed

5 files changed

+20
-5
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ $ node -r dotenv/config your_script.js
203203
The configuration options below are supported as command line arguments in the format `dotenv_config_<option>=value`
204204

205205
```bash
206-
$ node -r dotenv/config your_script.js dotenv_config_path=/custom/path/to/.env
206+
$ node -r dotenv/config your_script.js dotenv_config_path=/custom/path/to/.env dotenv_config_debug=true
207207
```
208208

209209
Additionally, you can use environment variables to set configuration options. Command line arguments will precede these.
@@ -213,7 +213,7 @@ $ DOTENV_CONFIG_<OPTION>=value node -r dotenv/config your_script.js
213213
```
214214

215215
```bash
216-
$ DOTENV_CONFIG_ENCODING=latin1 node -r dotenv/config your_script.js dotenv_config_path=/custom/path/to/.env
216+
$ DOTENV_CONFIG_ENCODING=latin1 DOTENV_CONFIG_DEBUG=true node -r dotenv/config your_script.js dotenv_config_path=/custom/path/to/.env
217217
```
218218

219219
## FAQ

lib/cli-options.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const re = /^dotenv_config_(encoding|path|debug)=(.+)$/
1+
const re = /^dotenv_config_(encoding|path|debug|override)=(.+)$/
22

33
module.exports = function optionMatcher (args) {
44
return args.reduce(function (acc, cur) {

lib/env-options.js

+4
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,8 @@ if (process.env.DOTENV_CONFIG_DEBUG != null) {
1313
options.debug = process.env.DOTENV_CONFIG_DEBUG
1414
}
1515

16+
if (process.env.DOTENV_CONFIG_OVERRIDE != null) {
17+
options.override = process.env.DOTENV_CONFIG_OVERRIDE
18+
}
19+
1620
module.exports = options

tests/test-cli-options.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ const t = require('tap')
22

33
const options = require('../lib/cli-options')
44

5-
t.plan(5)
5+
t.plan(6)
66

77
// matches encoding option
88
t.same(options(['node', '-e', "'console.log(testing)'", 'dotenv_config_encoding=utf8']), {
@@ -19,6 +19,11 @@ t.same(options(['node', '-e', "'console.log(testing)'", 'dotenv_config_debug=tru
1919
debug: 'true'
2020
})
2121

22+
// matches override option
23+
t.same(options(['node', '-e', "'console.log(testing)'", 'dotenv_config_override=true']), {
24+
override: 'true'
25+
})
26+
2227
// ignores empty values
2328
t.same(options(['node', '-e', "'console.log(testing)'", 'dotenv_config_path=']), {})
2429

tests/test-env-options.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ require('../lib/env-options')
88
const e = process.env.DOTENV_CONFIG_ENCODING
99
const p = process.env.DOTENV_CONFIG_PATH
1010
const d = process.env.DOTENV_CONFIG_DEBUG
11+
const o = process.env.DOTENV_CONFIG_OVERRIDE
1112

1213
// get fresh object for each test
1314
function options () {
@@ -24,12 +25,13 @@ function testOption (envVar, tmpVal, expect) {
2425
delete process.env[envVar]
2526
}
2627

27-
t.plan(4)
28+
t.plan(5)
2829

2930
// returns empty object when no options set in process.env
3031
delete process.env.DOTENV_CONFIG_ENCODING
3132
delete process.env.DOTENV_CONFIG_PATH
3233
delete process.env.DOTENV_CONFIG_DEBUG
34+
delete process.env.DOTENV_CONFIG_OVERRIDE
3335

3436
t.same(options(), {})
3537

@@ -42,7 +44,11 @@ testOption('DOTENV_CONFIG_PATH', '~/.env.test', { path: '~/.env.test' })
4244
// sets debug option
4345
testOption('DOTENV_CONFIG_DEBUG', 'true', { debug: 'true' })
4446

47+
// sets override option
48+
testOption('DOTENV_CONFIG_OVERRIDE', 'true', { override: 'true' })
49+
4550
// restore existing env
4651
process.env.DOTENV_CONFIG_ENCODING = e
4752
process.env.DOTENV_CONFIG_PATH = p
4853
process.env.DOTENV_CONFIG_DEBUG = d
54+
process.env.DOTENV_CONFIG_OVERRIDE = o

0 commit comments

Comments
 (0)