Skip to content

Commit

Permalink
Merge pull request #597 from motdotla/override-cli-option
Browse files Browse the repository at this point in the history
Override cli option
  • Loading branch information
motdotla committed Jan 17, 2022
2 parents 18b6e1c + aeae434 commit 44281f4
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 8 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Expand Up @@ -6,6 +6,13 @@ All notable changes to this project will be documented in this file. See [standa

(place unreleased comments here)

## [14.2.0](https://github.com/motdotla/dotenv/compare/v14.1.1...v14.2.0) (2022-01-17)

### Added

- Add `dotenv_config_override` cli option
- Add `DOTENV_CONFIG_OVERRIDE` command line env option

## [14.1.1](https://github.com/motdotla/dotenv/compare/v14.1.0...v14.1.1) (2022-01-17)

### Added
Expand Down
4 changes: 2 additions & 2 deletions README.md
Expand Up @@ -203,7 +203,7 @@ $ node -r dotenv/config your_script.js
The configuration options below are supported as command line arguments in the format `dotenv_config_<option>=value`

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

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

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

## FAQ
Expand Down
2 changes: 1 addition & 1 deletion lib/cli-options.js
@@ -1,4 +1,4 @@
const re = /^dotenv_config_(encoding|path|debug)=(.+)$/
const re = /^dotenv_config_(encoding|path|debug|override)=(.+)$/

module.exports = function optionMatcher (args) {
return args.reduce(function (acc, cur) {
Expand Down
4 changes: 4 additions & 0 deletions lib/env-options.js
Expand Up @@ -13,4 +13,8 @@ if (process.env.DOTENV_CONFIG_DEBUG != null) {
options.debug = process.env.DOTENV_CONFIG_DEBUG
}

if (process.env.DOTENV_CONFIG_OVERRIDE != null) {
options.override = process.env.DOTENV_CONFIG_OVERRIDE
}

module.exports = options
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "dotenv",
"version": "14.1.1",
"version": "14.2.0",
"description": "Loads environment variables from .env file",
"main": "lib/main.js",
"types": "lib/main.d.ts",
Expand Down
7 changes: 6 additions & 1 deletion tests/test-cli-options.js
Expand Up @@ -2,7 +2,7 @@ const t = require('tap')

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

t.plan(5)
t.plan(6)

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

// matches override option
t.same(options(['node', '-e', "'console.log(testing)'", 'dotenv_config_override=true']), {
override: 'true'
})

// ignores empty values
t.same(options(['node', '-e', "'console.log(testing)'", 'dotenv_config_path=']), {})

Expand Down
8 changes: 7 additions & 1 deletion tests/test-env-options.js
Expand Up @@ -8,6 +8,7 @@ require('../lib/env-options')
const e = process.env.DOTENV_CONFIG_ENCODING
const p = process.env.DOTENV_CONFIG_PATH
const d = process.env.DOTENV_CONFIG_DEBUG
const o = process.env.DOTENV_CONFIG_OVERRIDE

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

t.plan(4)
t.plan(5)

// returns empty object when no options set in process.env
delete process.env.DOTENV_CONFIG_ENCODING
delete process.env.DOTENV_CONFIG_PATH
delete process.env.DOTENV_CONFIG_DEBUG
delete process.env.DOTENV_CONFIG_OVERRIDE

t.same(options(), {})

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

// sets override option
testOption('DOTENV_CONFIG_OVERRIDE', 'true', { override: 'true' })

// restore existing env
process.env.DOTENV_CONFIG_ENCODING = e
process.env.DOTENV_CONFIG_PATH = p
process.env.DOTENV_CONFIG_DEBUG = d
process.env.DOTENV_CONFIG_OVERRIDE = o

0 comments on commit 44281f4

Please sign in to comment.