Skip to content

Commit

Permalink
feat(dotenv-flow): return error if no .env* files were found
Browse files Browse the repository at this point in the history
With this change dotenv-flow's `.config()` method will start returning an error if there is no appropriate ".env*" files were found.

Closes #41.
  • Loading branch information
kerimdzhanov committed Aug 27, 2023
1 parent 49ad914 commit 3bcfe0a
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 2 deletions.
10 changes: 9 additions & 1 deletion lib/dotenv-flow.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

const fs = require('fs');
const {resolve} = require('path');
const {resolve, sep} = require('path');
const dotenv = require('dotenv');

/**
Expand Down Expand Up @@ -162,6 +162,14 @@ function config(options = {}) {
.filter(filename => fs.existsSync(filename))
);

if (existingFiles.length === 0) {
const message = node_env
? `no ".env*" files matching pattern "${path + sep}.env[.${node_env}][.local]"`
: `no ".env*" files matching pattern "${path + sep}.env[.local]" (no \`process.env.NODE_ENV\` or \`options.default_node_env\` is defined)`;

return { error: new Error(message) };
}

return load(existingFiles, { encoding, silent });
}
catch (error) {
Expand Down
81 changes: 80 additions & 1 deletion test/unit/dotenv-flow-api.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -770,7 +770,7 @@ describe('dotenv-flow (API)', () => {
});
});

describe('the return object', () => {
describe('the returning object', () => {
it('includes the parsed contents of the files in the `parsed` property', () => {
$dotenvFiles['/path/to/project/.env'] = 'DEFAULT_ENV_VAR=ok';
$dotenvFiles['/path/to/project/.env.local'] = 'LOCAL_ENV_VAR=ok';
Expand Down Expand Up @@ -818,5 +818,84 @@ describe('dotenv-flow (API)', () => {
.with.property('message', 'file reading error stub');
});
});

describe('when none of the appropriate ".env*" files is present', () => {
it('returns "no `.env*` files" error', () => {
const result = dotenvFlow.config();

expect(result)
.to.be.an('object')
.with.property('error')
.that.is.an('error')
.with.property('message')
.that.matches(/no "\.env\*" files/);
});

describe('and no `node_env`-specific environment is set', () => {
it('returns an error with a message indicating the working directory', () => {
const noopResult = dotenvFlow.config();

expect(noopResult.error)
.to.be.an('error')
.with.property('message')
.that.includes('/path/to/project');

const pathResult = dotenvFlow.config({
path: '/path/to/another/project'
});

expect(pathResult.error)
.to.be.an('error')
.with.property('message')
.that.includes('/path/to/another/project');
});

it('returns an error with a message indicating the default ".env[.local]" setup', () => {
const result = dotenvFlow.config();

const expectedPattern = '/path/to/project/.env[.local]';

expect(result.error)
.to.be.an('error')
.with.property('message')
.that.includes(`"${expectedPattern}"`);
});
});

describe('and `process.env.NODE_ENV` is specified', () => {
beforeEach('setup `process.env.NODE_ENV`', () => {
process.env.NODE_ENV = 'development';
});

it('returns an error with a message indicating the working directory', () => {
const noopResult = dotenvFlow.config();

expect(noopResult.error)
.to.be.an('error')
.with.property('message')
.that.includes('/path/to/project');

const pathResult = dotenvFlow.config({
path: '/path/to/another/project'
});

expect(pathResult.error)
.to.be.an('error')
.with.property('message')
.that.includes('/path/to/another/project');
});

it('returns an error with a message indicating the node_env-specific setup', () => {
const result = dotenvFlow.config();

const expectedPattern = '/path/to/project/.env[.development][.local]';

expect(result.error)
.to.be.an('error')
.with.property('message')
.that.includes(`"${expectedPattern}"`);
});
});
});
});
});

0 comments on commit 3bcfe0a

Please sign in to comment.