Skip to content

Commit a1d8d24

Browse files
Add possibility to pass variables from command line.
1 parent 88badb1 commit a1d8d24

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,21 @@ Some applications load from `.env`, `.env.local`, `.env.development` and `.env.d
3636
(see [#37](https://github.com/entropitor/dotenv-cli/issues/37) for more information).
3737
`dotenv-cli` supports this using the `-c` flag for just `.env` and `.env.local` and `-c development` for the ones above.
3838

39+
### Setting variable from command line
40+
It is possible to set variable directly from command line using the -v flag:
41+
```bash
42+
$ dotenv -v VARIABLE=somevalue <command with arguments>
43+
```
44+
45+
Multiple variables can be specified:
46+
```bash
47+
$ dotenv -v VARIABLE1=somevalue1 -v VARIABLE2=somevalue2 <command with arguments>
48+
```
49+
50+
Variables set up from command line have higher priority than from env files.
51+
52+
> Purpose of this is that standard approach `VARIABLE=somevalue <command with arguments>` doesn't work on Windows. The -v flag works on all the platforms.
53+
3954
### Check env variable
4055
If you want to check the value of an environment variable, use the `-p` flag
4156
```bash

cli.js

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,13 @@ var dotenvExpand = require('dotenv-expand')
99

1010
function printHelp () {
1111
console.log([
12-
'Usage: dotenv [--help] [--debug] [-e <path>] [-p <variable name>] [-c [environment]] [-- command]',
12+
'Usage: dotenv [--help] [--debug] [-e <path>] [-v <name>=<value>] [-p <variable name>] [-c [environment]] [-- command]',
1313
' --help print help',
1414
' --debug output the files that would be processed but don\'t actually parse them or run the `command`',
1515
' -e <path> parses the file <path> as a `.env` file and adds the variables to the environment',
1616
' -e <path> multiple -e flags are allowed',
17+
' -v <name>=<value> put variable <name> into environment using value <value>',
18+
' -v <name>=<value> multiple -v flags are allowed',
1719
' -p <variable> print value of <variable> to the console. If you specify this, you do not have to specify a `command`',
1820
' -c [environment] support cascading env variables from `.env`, `.env.local`, `.env.<environment>`, `.env.<environment>.local` files',
1921
' command `command` is the actual command you want to run. Best practice is to precede this command with ` -- `. Everything after `--` is considered to be your command. So any flags will not be parsed by this tool but be passed to your command. If you do not do it, this tool will strip those flags'
@@ -45,14 +47,35 @@ if (argv.c) {
4547
), [])
4648
}
4749

50+
51+
function validateCmdVariable(param){
52+
const indexOfEqualSign = param.indexOf('=')
53+
if(indexOfEqualSign === -1 || indexOfEqualSign === 0 || indexOfEqualSign === param.length - 1){
54+
console.error('Unexpected argument ' + param + '. Expected variable in format variable=value')
55+
process.exit(1)
56+
}
57+
return param
58+
}
59+
var variables = []
60+
if (argv.v) {
61+
if(typeof argv.v === 'string')
62+
variables.push(validateCmdVariable(argv.v))
63+
else
64+
variables.push(...argv.v.map(validateCmdVariable))
65+
}
66+
var parsed = dotenv.parse(Buffer.from(variables.join('\n')))
67+
68+
4869
if (argv.debug) {
4970
console.log(paths)
71+
console.log(parsed)
5072
process.exit()
5173
}
5274

5375
paths.forEach(function (env) {
5476
dotenvExpand(dotenv.config({ path: path.resolve(env) }))
5577
})
78+
Object.assign(process.env, parsed)
5679

5780
if (argv.p) {
5881
var value = process.env[argv.p]

0 commit comments

Comments
 (0)