Skip to content
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

Added options.node_env that refers process.env.NODE_ENV by default #6

Merged
merged 18 commits into from
Dec 15, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 11 additions & 10 deletions lib/dotenv-flow.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,25 +35,23 @@ function cleanupDotenvDefinedVars(cwd, encoding) {
}

/**
* Get existing `.env*` filenames depending on the `NODE_ENV` in a prioritized order.
* Get existing `.env*` filenames depending on the `node_env` in a prioritized order.
*
* We don't include `.env.local` for "test" environment, since
* normally you expect tests to produce the same results for everyone.
*
* @private
* @param {string} cwd – path to `.env*` files directory
* @param {string} node_env – the current environment target
* @return {string[]}
*/
function getDotenvFilenames(cwd) {
const {NODE_ENV = undefined} = process.env;

function getDotenvFilenames(cwd, node_env) {
return [
NODE_ENV && `${cwd}/.env.${NODE_ENV}.local`,
NODE_ENV && `${cwd}/.env.${NODE_ENV}`,
NODE_ENV !== 'test' && `${cwd}/.env.local`,
node_env && `${cwd}/.env.${node_env}.local`,
node_env && `${cwd}/.env.${node_env}`,
node_env !== 'test' && `${cwd}/.env.local`,
`${cwd}/.env`
]
.filter(filename => filename && fs.existsSync(filename));
].filter(filename => filename && fs.existsSync(filename));
}

/**
Expand All @@ -64,6 +62,7 @@ function getDotenvFilenames(cwd) {
* @param {string} [options.cwd=process.cwd()] – path to `.env*` files directory
* @param {string} [options.encoding=utf8] – encoding of `.env*` files
* @param {boolean} [options.purge_dotenv] – perform the {@link cleanupDotenvDefinedVars}
* @param {string} [options.node_env=process.env.NODE_ENV] – custom environment target
* @return {object} with a `parsed` key containing the loaded content or an `error` key if it failed
*/
function config(options = {}) {
Expand All @@ -74,12 +73,14 @@ function config(options = {}) {
process.env.NODE_ENV = options.default_node_env;
MasonRhodesDev marked this conversation as resolved.
Show resolved Hide resolved
}

const node_env = options.node_env || process.env.NODE_ENV || options.default_node_env || undefined;

try {
if (options.purge_dotenv) {
cleanupDotenvDefinedVars(cwd, encoding);
}

const parsed = getDotenvFilenames(cwd)
const parsed = getDotenvFilenames(cwd, node_env)
.reduce((parsed, path) => {
const result = dotenv.config({ path, encoding });

Expand Down
29 changes: 29 additions & 0 deletions test/dotenv-flow.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,35 @@ describe('dotenv-flow', () => {
});
});

describe('when the `node_env` option is provided', () => {
MasonRhodesDev marked this conversation as resolved.
Show resolved Hide resolved
const directory = getFixtureProjectPath('node-env-local');

it('uses that value to load specific `.env.*` files with a different `NODE_ENV` value set', async () => {
let environment, variables;

variables = await execHelper('print-env-with-node_env.js', directory);

expect(variables).to.include({
DEFAULT_ENV_VAR: "ok",
DEFAULT_ENV_VAR: 'ok'
});

environment = {
NODE_ENV: 'production',
CUSTOM_ENV: 'development'
};

variables = await execHelper('print-env-with-node_env.js', directory, environment);

expect(variables).to.include({
NODE_ENV: 'production',
DEFAULT_ENV_VAR: 'ok',
DEVELOPMENT_ENV_VAR: 'ok',
DEVELOPMENT_LOCAL_VAR: 'ok'
});
});
});

describe('when the `purge_dotenv` option is set to `true`', () => {
const directory = getFixtureProjectPath('node-env-local');

Expand Down
7 changes: 7 additions & 0 deletions test/helpers/print-env-with-node_env.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
'use strict';

require('../../lib/dotenv-flow').config({
node_env: process.env.CUSTOM_ENV
});

console.log(JSON.stringify(process.env));