From 207bbd4786f27dd24c31df2a3e7508443576b1d0 Mon Sep 17 00:00:00 2001 From: Andrew Blair Date: Fri, 23 Nov 2018 11:52:12 -0800 Subject: [PATCH 1/4] Set dotenv_config_* vars via env var MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Now `dotenv_config_*` can be supplied in a system env var instead of in CLI command. Helps some IDEs that get confused with the CLI option like WebStorm/IntelliJ. WebStorm’s application of those command line config options was resulting in node trying to load them as additional npm modules rather than simple argv options that dotenv would consume. --- README.md | 3 +++ config.js | 5 ++++- lib/env-options.js | 13 +++++++++++++ 3 files changed, 20 insertions(+), 1 deletion(-) create mode 100644 lib/env-options.js diff --git a/README.md b/README.md index 034fc52d..90f01f1c 100644 --- a/README.md +++ b/README.md @@ -65,6 +65,9 @@ The configuration options below are supported as command line arguments in the f $ node -r dotenv/config your_script.js dotenv_config_path=/custom/path/to/your/env/vars ``` +Note, some IDEs like Webstorm and IntellJ might have trouble properly passing `dotenv_config_path` to node. This does not impact command line use, just when configuring run options within the IDE. +As an alternative for this situation, you can also set `dotenv_config_path` as an environment variable in your IDE run configuration instead of as a command line argument + ## Config _Alias: `load`_ diff --git a/config.js b/config.js index 77641220..59da1d8a 100644 --- a/config.js +++ b/config.js @@ -2,6 +2,9 @@ (function () { require('./lib/main').config( - require('./lib/cli-options')(process.argv) + { + ...require('./lib/cli-options')(process.argv), + ...require('./lib/env-options')() + } ) })() diff --git a/lib/env-options.js b/lib/env-options.js new file mode 100644 index 00000000..fe1dfdba --- /dev/null +++ b/lib/env-options.js @@ -0,0 +1,13 @@ +/* @flow */ + +const re = /^dotenv_config_(encoding|path|debug)$/ + +module.exports = function optionMatcher () { + return Object.entries(process.env).reduce(function (acc, [key, val]) { + const matches = key.match(re) + if (matches) { + acc[matches[1]] = val + } + return acc + }, {}) +} From e0f3a96a7d37faba61c09bd1a3e57cc0be64d7ea Mon Sep 17 00:00:00 2001 From: Andrew Blair Date: Mon, 3 Dec 2018 10:08:44 -0800 Subject: [PATCH 2/4] fix to accommodate Node 6 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Node 6 didn’t support spread operator on objects, only arrays. Later versions of Node do support this FYI. Replaced with older syntax --- config.js | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/config.js b/config.js index 59da1d8a..05850a52 100644 --- a/config.js +++ b/config.js @@ -2,9 +2,6 @@ (function () { require('./lib/main').config( - { - ...require('./lib/cli-options')(process.argv), - ...require('./lib/env-options')() - } + Object.assign({}, require('./lib/env-options'), require('./lib/cli-options')(process.argv)) ) })() From b6868f4e9b823cbb3cbe466b431c1eaa0f67cfe4 Mon Sep 17 00:00:00 2001 From: Andrew Blair Date: Mon, 3 Dec 2018 10:08:55 -0800 Subject: [PATCH 3/4] replaced reducer with simple object --- lib/env-options.js | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/lib/env-options.js b/lib/env-options.js index fe1dfdba..d6fa2611 100644 --- a/lib/env-options.js +++ b/lib/env-options.js @@ -1,13 +1,7 @@ /* @flow */ -const re = /^dotenv_config_(encoding|path|debug)$/ - -module.exports = function optionMatcher () { - return Object.entries(process.env).reduce(function (acc, [key, val]) { - const matches = key.match(re) - if (matches) { - acc[matches[1]] = val - } - return acc - }, {}) +module.exports = { + encoding: process.env.dotenv_config_encoding, + path: process.env.dotenv_config_path, + debug: process.env.dotenv_config_debug } From da595e3d834369642e4d85475efe85516cbcb654 Mon Sep 17 00:00:00 2001 From: Andrew Blair Date: Mon, 3 Dec 2018 12:04:40 -0800 Subject: [PATCH 4/4] options via env vars: added tests and refactored --- README.md | 4 ++-- config.js | 2 +- lib/env-options.js | 10 ++++++---- tests/test-env-options.js | 29 +++++++++++++++++++++++++++++ 4 files changed, 38 insertions(+), 7 deletions(-) create mode 100644 tests/test-env-options.js diff --git a/README.md b/README.md index 90f01f1c..10d4376f 100644 --- a/README.md +++ b/README.md @@ -65,8 +65,8 @@ The configuration options below are supported as command line arguments in the f $ node -r dotenv/config your_script.js dotenv_config_path=/custom/path/to/your/env/vars ``` -Note, some IDEs like Webstorm and IntellJ might have trouble properly passing `dotenv_config_path` to node. This does not impact command line use, just when configuring run options within the IDE. -As an alternative for this situation, you can also set `dotenv_config_path` as an environment variable in your IDE run configuration instead of as a command line argument +If you are having trouble passing the `dotenv_config_