-
Notifications
You must be signed in to change notification settings - Fork 50
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add possibility to pass variables from command line. #57
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,11 +9,13 @@ var dotenvExpand = require('dotenv-expand') | |
|
||
function printHelp () { | ||
console.log([ | ||
'Usage: dotenv [--help] [--debug] [-e <path>] [-p <variable name>] [-c [environment]] [-- command]', | ||
'Usage: dotenv [--help] [--debug] [-e <path>] [-v <name>=<value>] [-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', | ||
' -v <name>=<value> put variable <name> into environment using value <value>', | ||
' -v <name>=<value> multiple -v flags are allowed', | ||
' -p <variable> print value of <variable> to the console. If you specify this, you do not have to specify a `command`', | ||
' -c [environment] support cascading env variables from `.env`, `.env.local`, `.env.<environment>`, `.env.<environment>.local` files', | ||
' 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) { | |
), []) | ||
} | ||
|
||
|
||
function validateCmdVariable(param){ | ||
const indexOfEqualSign = param.indexOf('=') | ||
if(indexOfEqualSign === -1 || indexOfEqualSign === 0 || indexOfEqualSign === param.length - 1){ | ||
console.error('Unexpected argument ' + param + '. Expected variable in format variable=value') | ||
process.exit(1) | ||
} | ||
return param | ||
} | ||
var variables = [] | ||
if (argv.v) { | ||
if(typeof argv.v === 'string') | ||
variables.push(validateCmdVariable(argv.v)) | ||
else | ||
variables.push(...argv.v.map(validateCmdVariable)) | ||
} | ||
var parsed = dotenv.parse(Buffer.from(variables.join('\n'))) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you put this closer to line 78? and maybe name There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I renamed the variable, but I didn't want to move it down, as I believe debug option should print the variables. And it should be after line 73 to overwrite variables from env files. |
||
|
||
|
||
if (argv.debug) { | ||
console.log(paths) | ||
console.log(parsed) | ||
process.exit() | ||
} | ||
|
||
paths.forEach(function (env) { | ||
dotenvExpand(dotenv.config({ path: path.resolve(env) })) | ||
}) | ||
Object.assign(process.env, parsed) | ||
|
||
if (argv.p) { | ||
var value = process.env[argv.p] | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you follow the code style and put braces around these?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I update PR to reflect code style in project.