From 33556bd469c1b1275968a6ad4b90490bb1715c0f Mon Sep 17 00:00:00 2001 From: Dave Cardwell Date: Thu, 10 Sep 2020 10:27:10 -0400 Subject: [PATCH 1/3] Add a --debug flag to output the paths to process --- README.md | 6 +++++- cli.js | 8 +++++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 2fdaf10..4e9ce5a 100644 --- a/README.md +++ b/README.md @@ -83,13 +83,17 @@ you might expect `dotenv echo "$SAY_HI"` to display `hello!`. In fact, this is n One possible way to get the desired result is: ``` -dotenv -- bash -c 'echo "$SAY_HI"' +$ dotenv -- bash -c 'echo "$SAY_HI"' ``` In bash, everything between `'` is not interpreted but passed as is. Since `$SAY_HI` is inside `''` brackets, it's passed as a string literal. Therefore, `dotenv-cli` will start a child process `bash -c 'echo "$SAY_HI"'` with the env variable `SAY_HI` set correctly which means bash will run `echo "$SAY_HI"` in the right environment which will print correctly `hello` +### Debugging + +You can add the `--debug` flag to output the `.env` files that would be processed and exit. + ## License [MIT](https://en.wikipedia.org/wiki/MIT_License) diff --git a/cli.js b/cli.js index 239e9e8..053884a 100755 --- a/cli.js +++ b/cli.js @@ -9,8 +9,9 @@ var dotenvExpand = require('dotenv-expand') function printHelp () { console.log([ - 'Usage: dotenv [--help] [-e ] [-p ] [-c [environment]] [-- command]', + 'Usage: dotenv [--help] [--debug] [-e ] [-p ] [-c [environment]] [-- command]', ' --help print help', + ' --debug output the files that would be processed but don\'t actually parse them or run the `command`', ' -e parses the file as a `.env` file and adds the variables to the environment', ' -e multiple -e flags are allowed', ' -p print value of to the console. If you specify this, you do not have to specify a `command`', @@ -45,6 +46,11 @@ if (paths.length === 0) { paths.push('.env') } +if (argv.debug) { + console.log(paths) + process.exit() +} + paths.forEach(function (env) { dotenvExpand(dotenv.config({ path: path.resolve(env) })) }) From a09ec0851aeb1b166e2ec01fb1ba048238f6234c Mon Sep 17 00:00:00 2001 From: Dave Cardwell Date: Thu, 10 Sep 2020 10:37:01 -0400 Subject: [PATCH 2/3] Cascade -e files when combined with -c When using `-e` and `-c` together we should load the . and .local versions of the files specified by `-e`. Note that this is a change from the behavior introduced in ba7c7bab936384c29cf281d486906f984de78b3d (v3.2.0) when combining `-e extra` with `-c` would result in processing: [ '.env.local', '.env', 'extra' ] It will now process: [ 'extra.local', 'extra' ] --- cli.js | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/cli.js b/cli.js index 053884a..96551b8 100755 --- a/cli.js +++ b/cli.js @@ -27,23 +27,22 @@ if (argv.help) { var paths = [] -if (argv.c) { - if (typeof argv.c === 'string') { - paths.push(`.env.${argv.c}.local`, `.env.${argv.c}`) - } - paths.push(".env.local", ".env") -} - if (argv.e) { if (typeof argv.e === 'string') { paths.push(argv.e) } else { paths.push(...argv.e) } +} else { + paths.push('.env') } -if (paths.length === 0) { - paths.push('.env') +if (argv.c) { + paths = paths.reduce((acc, p) => acc.concat( + typeof argv.c === 'string' + ? [`${p}.${argv.c}.local`, `${p}.${argv.c}`, `${p}.local`, p] + : [`${p}.local`, p] + ), []) } if (argv.debug) { From ef6c6df133f727f3f63dbded498000ce6250027a Mon Sep 17 00:00:00 2001 From: Dave Cardwell Date: Thu, 10 Sep 2020 22:24:17 -0400 Subject: [PATCH 3/3] Use longer variable names for clarity Co-authored-by: Jens Claes Fix variable name --- cli.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/cli.js b/cli.js index 96551b8..eb1af55 100755 --- a/cli.js +++ b/cli.js @@ -38,10 +38,10 @@ if (argv.e) { } if (argv.c) { - paths = paths.reduce((acc, p) => acc.concat( + paths = paths.reduce((accumulator, path) => accumulator.concat( typeof argv.c === 'string' - ? [`${p}.${argv.c}.local`, `${p}.${argv.c}`, `${p}.local`, p] - : [`${p}.local`, p] + ? [`${path}.${argv.c}.local`, `${path}.${argv.c}`, `${path}.local`, path] + : [`${path}.local`, path] ), []) }