Skip to content

Commit

Permalink
Merge pull request #44 from davecardwell/master
Browse files Browse the repository at this point in the history
  • Loading branch information
entropitor authored Sep 13, 2020
2 parents 565d048 + ef6c6df commit 3458d5e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 11 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
25 changes: 15 additions & 10 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ var dotenvExpand = require('dotenv-expand')

function printHelp () {
console.log([
'Usage: dotenv [--help] [-e <path>] [-p <variable name>] [-c [environment]] [-- command]',
'Usage: dotenv [--help] [--debug] [-e <path>] [-p <variable name>] [-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 <path> parses the file <path> as a `.env` file and adds the variables to the environment',
' -e <path> multiple -e flags are allowed',
' -p <variable> print value of <variable> to the console. If you specify this, you do not have to specify a `command`',
Expand All @@ -26,23 +27,27 @@ 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((accumulator, path) => accumulator.concat(
typeof argv.c === 'string'
? [`${path}.${argv.c}.local`, `${path}.${argv.c}`, `${path}.local`, path]
: [`${path}.local`, path]
), [])
}

if (argv.debug) {
console.log(paths)
process.exit()
}

paths.forEach(function (env) {
Expand Down

0 comments on commit 3458d5e

Please sign in to comment.