Skip to content

Commit 76c9cb2

Browse files
authored
Merge pull request #17 from MikeGarde/env-var-for-env-file-selection
add support for DOTENV_FILE environment variable
2 parents c4fd30c + 0fae1a1 commit 76c9cb2

3 files changed

Lines changed: 24 additions & 5 deletions

File tree

README.md

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,19 @@ Delete a value from a .env file:
8080
dotenv <key> --delete
8181
```
8282

83+
### Using DOTENV_FILE Environment Variable
84+
85+
You can define the `DOTENV_FILE` environment variable in your shell or script to specify the `.env` file to use, instead
86+
of passing the `--file` option every time.
87+
88+
```shell
89+
export DOTENV_FILE=.env.example
90+
dotenv <key>
91+
```
92+
93+
This will use the `.env.example` file automatically. If the `--file` option is provided, it will override the
94+
`DOTENV_FILE` environment variable.
95+
8396
## Examples
8497

8598
### RSA Key Pair
@@ -111,13 +124,13 @@ dotenv APP_VERSION --set $NEW_VERSION
111124
Make it pretty with `jq`:
112125

113126
```shell
114-
dotenv --json | jq
127+
dotenv | jq
115128
```
116129

117130
Or filter the output:
118131

119132
```shell
120-
$ dotenv --json | jq 'to_entries | map(select(.key | startswith("DB_")))[] | "\(.key)=\(.value)"'
133+
$ dotenv | jq 'to_entries | map(select(.key | startswith("DB_")))[] | "\(.key)=\(.value)"'
121134
"DB_HOST=localhost"
122135
"DB_USER=root"
123136
"DB_PASS=password"

src/app.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ async function app() {
4444
throw new RuleViolationError(`Error reading from stdin: ${err}`);
4545
});
4646

47-
const envFilePath: string = cliOptions.file || '.env';
47+
const envFilePath: string = cliOptions.file || process.env.DOTENV_FILE || '.env';
4848
const fullEnvPath: string = path.resolve(envFilePath);
4949
const keys: string[] = program.args;
5050
const set: string = cliOptions.set;
@@ -65,7 +65,7 @@ async function app() {
6565

6666
// Must have a .env file
6767
if (!fs.existsSync(fullEnvPath)) {
68-
throw new RuleViolationError(`.env file not found: ${fullEnvPath}`);
68+
throw new RuleViolationError(`File not found: ${fullEnvPath}`);
6969
}
7070

7171
let options: Options = {

tests/app.tests.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,16 @@ describe('app.ts', () => {
1919
const errorMsg: string = buffer.toString('utf8');
2020

2121
expect(parsedError.status).toEqual(1);
22-
expect(errorMsg).toContain('.env file not found');
22+
expect(errorMsg).toContain('File not found');
2323
}
2424
});
2525

26+
test('uses DOTENV_FILE environment variable', () => {
27+
const result = execSync(`export DOTENV_FILE=${envPath} && node ${appPath} NAME`);
28+
expect(result.toString().trim()).toBe('dotenv-cli');
29+
delete process.env.DOTENV_FILE;
30+
});
31+
2632
test('read simple value', () => {
2733
const result = execSync(`node ${appPath} NAME --file ${envPath}`);
2834
expect(result.toString().trim()).toBe('dotenv-cli');

0 commit comments

Comments
 (0)