Initial code test
CI/CD Pipeline / test (push) Has been cancelled
CI/CD Pipeline / deploy-internal (push) Has been cancelled

This commit is contained in:
chris committed 2026-08-26 11:51:58 -04:00
commit d4fae15f5c
1696 files changed
+271223

No files matched your search

+21
View File
@@ -0,0 +1,21 @@
Copyright (c) 2025 Jens Claes
The MIT License (MIT)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
+200
View File
@@ -0,0 +1,200 @@
# dotenv-cli
## Installing
NPM
```bash
$ npm install -g dotenv-cli
```
Yarn
```bash
$ yarn global add dotenv-cli
```
pnpm
```bash
pnpm add -g dotenv-cli
```
## Usage
```bash
$ dotenv -- <command with arguments>
```
This will load the variables from the .env file in the current working directory and then run the command (using the new set of environment variables).
Alternatively, if you do not need to pass arguments to the command, you can use the shorthand:
```bash
$ dotenv <command>
```
### Custom .env files
Another .env file could be specified using the -e flag (this will replace loading `.env` file):
```bash
$ dotenv -e .env2 -- <command with arguments>
```
Multiple .env files can be specified, and will be processed in order, but only sets variables if they haven't already been set. So the first one wins (existing env variables win over the first file and the first file wins over the second file):
```bash
$ dotenv -e .env3 -e .env4 -- <command with arguments>
```
### Cascading env variables
Some applications load env variables from multiple `.env` files depending on the environment:
- `.env`
- `.env.local`
- `.env.development`
- `.env.development.local`
dotenv-cli supports this using the `-c` flag:
- `-c` loads `.env` and `.env.local`
- `-c test` loads `.env`, `.env.local`, `.env.test`, and `.env.test.local`
See [#37](https://github.com/entropitor/dotenv-cli/issues/37) for more information.
The `-c` flag can be used together with the `-e` flag. The following example will cascade env files located one folder up in the directory tree (`../.env` followed by `../.env.local`):
```bash
dotenv -e ../.env -c
```
### Setting variable from command line
It is possible to set variable directly from command line using the -v flag:
```bash
$ dotenv -v VARIABLE=somevalue -- <command with arguments>
```
Multiple variables can be specified:
```bash
$ dotenv -v VARIABLE1=somevalue1 -v VARIABLE2=somevalue2 -- <command with arguments>
```
Variables set up from command line have higher priority than from env files.
> 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.
### Check env variable
If you want to check the value of an environment variable, use the `-p` flag
```bash
$ dotenv -p NODE_ENV
```
### Flags to the underlying command
If you want to pass flags to the inner command use `--` after all the flags to `dotenv-cli`.
E.g. the following command without dotenv-cli:
```bash
mvn exec:java -Dexec.args="-g -f"
```
will become the following command with dotenv-cli:
```bash
$ dotenv -- mvn exec:java -Dexec.args="-g -f"
```
or in case the env file is at `.my-env`
```bash
$ dotenv -e .my-env -- mvn exec:java -Dexec.args="-g -f"
```
### Variable expansion
We support expanding env variables inside .env files (See [dotenv-expand](https://github.com/motdotla/dotenv-expand) npm package for more information)
For example:
```
IP=127.0.0.1
PORT=1234
APP_URL=http://${IP}:${PORT}
```
Using the above example `.env` file, `process.env.APP_URL` would be `http://127.0.0.1:1234`.
#### Disabling variable expansion
If your `.env` variables include values that should not be expanded (e.g. `PASSWORD="pas$word"`), you can pass flag `--no-expand` to `dotenv-cli` to disable variable expansion.
For example:
```bash
dotenv --no-expand <command>
```
### Variable expansion in the command
If your `.env` file looks like:
```
SAY_HI=hello!
```
you might expect `dotenv echo "$SAY_HI"` to display `hello!`. In fact, this is not what happens: your shell will first interpret your command before passing it to `dotenv-cli`, so if `SAY_HI` envvar is set to `""`, the command will be expanded into `dotenv echo`: that's why `dotenv-cli` cannot make the expansion you expect.
#### Possible solutions
1. Bash and escape
One possible way to get the desired result is:
```
$ 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`
2. Subscript encapsulation
Another solution is simply to encapsulate your script in another subscript.
Example here with npm scripts in a package.json
```json
{
"scripts": {
"_print-stuff": "echo $STUFF",
"print-stuff": "dotenv -- npm run _print-stuff"
}
}
```
This example is used in a project setting (has a package.json). Should always install locally `npm install -D dotenv-cli`
### Debugging
You can add the `--debug` flag to output the `.env` files that would be processed and exit.
### Override
Override any environment variables that have already been set on your machine with values from your .env file.
```bash
dotenv -e .env.test -o -- jest
```
## License
[MIT](https://en.wikipedia.org/wiki/MIT_License)
Generated Vendored Executable
+127
View File
@@ -0,0 +1,127 @@
#!/usr/bin/env node
const spawn = require('cross-spawn')
const path = require('path')
const argv = require('minimist')(process.argv.slice(2))
const dotenv = require('dotenv')
const dotenvExpand = require('dotenv-expand').expand
function printHelp () {
console.log([
'Usage: dotenv [--help] [--debug] [--quiet=false] [-e <path>] [-v <name>=<value>] [-p <variable name>] [-c [environment]] [--no-expand] [-- command]',
' --help print help',
' --debug output the files that would be processed but don\'t actually parse them or run the `command`',
' --quiet, -q suppress debug output from dotenv (default: true)',
' -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.<environment>`, `.env.local`, `.env.<environment>.local` files',
' --no-expand skip variable expansion',
' -o, --override override system variables. Cannot be used along with cascade (-c).',
' 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'
].join('\n'))
}
if (argv.help) {
printHelp()
process.exit()
}
const override = argv.o || argv.override
// Handle quiet flag - default is true (quiet), can be disabled with --quiet=false or -q=false
const isQuiet = !(argv.quiet === false || argv.q === false || argv.quiet === 'false' || argv.q === 'false')
if (argv.c && override) {
console.error('Invalid arguments. Cascading env variables conflicts with overrides.')
process.exit(1)
}
let paths = []
if (argv.e) {
if (typeof argv.e === 'string') {
paths.push(argv.e)
} else {
paths.push(...argv.e)
}
} else {
paths.push('.env')
}
if (argv.c) {
paths = paths.reduce((accumulator, path) => accumulator.concat(
typeof argv.c === 'string'
? [`${path}.${argv.c}.local`, `${path}.local`, `${path}.${argv.c}`, path]
: [`${path}.local`, path]
), [])
}
function validateCmdVariable (param) {
const [, key, val] = param.match(/^(\w+)=([\s\S]+)$/m) || []
if (!key || !val) {
console.error(`Invalid variable name. Expected variable in format '-v variable=value', but got: \`-v ${param}\`.`)
process.exit(1)
}
return [key, val]
}
const variables = []
if (argv.v) {
if (typeof argv.v === 'string') {
variables.push(validateCmdVariable(argv.v))
} else {
variables.push(...argv.v.map(validateCmdVariable))
}
}
const parsedVariables = Object.fromEntries(variables)
if (argv.debug) {
console.log(paths)
console.log(parsedVariables)
process.exit()
}
paths.forEach(function (env) {
dotenv.config({ path: path.resolve(env), override, quiet: isQuiet })
})
// Expand when all path configs are loaded
if (argv.expand !== false) {
dotenvExpand({
parsed: process.env
})
}
Object.assign(process.env, parsedVariables)
if (argv.p) {
let value = process.env[argv.p]
if (typeof value === 'string') {
value = `${value}`
}
console.log(value != null ? value : '')
process.exit()
}
const command = argv._[0]
if (!command) {
printHelp()
process.exit(1)
}
const child = spawn(command, argv._.slice(1), { stdio: 'inherit' })
.on('exit', function (exitCode, signal) {
if (typeof exitCode === 'number') {
process.exit(exitCode)
} else {
process.kill(process.pid, signal)
}
})
for (const signal of ['SIGINT', 'SIGTERM', 'SIGPIPE', 'SIGHUP', 'SIGBREAK', 'SIGWINCH', 'SIGUSR1', 'SIGUSR2']) {
process.on(signal, function () {
child.kill(signal)
})
}
+26
View File
@@ -0,0 +1,26 @@
{
"name": "dotenv-cli",
"description": "A global executable to run applications with the ENV variables loaded by dotenv",
"version": "11.0.0",
"author": "entropitor",
"bin": {
"dotenv": "./cli.js"
},
"dependencies": {
"cross-spawn": "^7.0.6",
"dotenv": "^17.1.0",
"dotenv-expand": "^12.0.0",
"minimist": "^1.2.6"
},
"devDependencies": {
"standard": "^16.0.4"
},
"license": "MIT",
"main": "index.js",
"repository": "entropitor/dotenv-cli",
"scripts": {
"lint": "standard"
},
"resolutions": {},
"packageManager": "yarn@1.22.22+sha1.ac34549e6aa8e7ead463a7407e1c7390f61a6610"
}