-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(dotenv-flow/config): add ability to configure `dotenv-flow/confi…
…g` via environment variables, relates #11
- Loading branch information
1 parent
d94d21c
commit 0118d27
Showing
6 changed files
with
166 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
(function() { | ||
require('./lib/dotenv-flow').config() | ||
})() | ||
'use strict'; | ||
|
||
const env_options = require('./lib/env-options'); | ||
|
||
require('./lib/dotenv-flow').config(env_options()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
'use strict'; | ||
|
||
const ENV_OPTIONS_MAP = { | ||
NODE_ENV: 'node_env', | ||
DEFAULT_NODE_ENV: 'default_node_env', | ||
DOTENV_FLOW_PATH: 'path', | ||
DOTENV_FLOW_ENCODING: 'encoding', | ||
DOTENV_FLOW_PURGE_DOTENV: 'purge_dotenv' | ||
}; | ||
|
||
/** | ||
* Get environment variable defined options for `dotenv-flow#config()`. | ||
* | ||
* @param {object} [env=process.env] | ||
* @return {{node_env?: string, default_node_env?: string, path?: string, encoding?: string, purge_dotenv?: string}} | ||
*/ | ||
module.exports = function env_options(env = process.env) { | ||
return Object.keys(ENV_OPTIONS_MAP) | ||
.reduce((options, key) => { | ||
if (key in env) { | ||
options[ ENV_OPTIONS_MAP[key] ] = env[key]; | ||
} | ||
return options; | ||
}, {}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
'use strict'; | ||
|
||
const path = require('path'); | ||
const util = require('util'); | ||
const execFile = util.promisify(require('child_process').execFile); | ||
const {expect} = require('chai'); | ||
|
||
/** | ||
* Get the path to a given fixture project. | ||
* | ||
* @param {string} name – fixture project name | ||
* @return {string} – path to a fixture project | ||
*/ | ||
function getFixtureProjectPath(name) { | ||
return path.join(__dirname, 'fixtures', name); | ||
} | ||
|
||
/** | ||
* Executes the preload helper script using the given fixture project as a working directory. | ||
* | ||
* @param {string} cwd – path to a fixture project | ||
* @param {object} [env] – predefined environment variables | ||
* @param {string[]} [args] – command line arguments | ||
* @return {Promise<object>} – stdout parsed as a json | ||
*/ | ||
async function execWithPreload(cwd, {env = {}, args = []} = {}) { | ||
const {stdout} = await execFile( | ||
process.argv[0], // ~= /usr/bin/node | ||
[ | ||
'-r', '../config', | ||
'-e', 'console.log(JSON.stringify(process.env));', | ||
'--', ...args | ||
], | ||
{ cwd, env } | ||
); | ||
|
||
try { | ||
return JSON.parse(stdout); | ||
} | ||
catch (e) { | ||
console.error(e); | ||
throw new Error(`Unable to parse the following as a JSON:\n${stdout}`); | ||
} | ||
} | ||
|
||
describe('dotenv-flow/config', () => { | ||
it('preloads `.env*` files defined environment variables', async () => { | ||
const variables = await execWithPreload(getFixtureProjectPath('env')); | ||
|
||
expect(variables) | ||
.to.have.property('DEFAULT_ENV_VAR') | ||
.that.is.equal('ok'); | ||
}); | ||
|
||
it('supports configuration via environment variables', async () => { | ||
let variables = await execWithPreload(getFixtureProjectPath('env'), { | ||
env: { | ||
DEFAULT_NODE_ENV: 'development', | ||
DOTENV_FLOW_PATH: getFixtureProjectPath('node-env') | ||
} | ||
}); | ||
|
||
expect(variables).to.include({ | ||
DEFAULT_NODE_ENV: 'development', | ||
DEFAULT_ENV_VAR: 'ok', | ||
DEVELOPMENT_ENV_VAR: 'ok', | ||
DEVELOPMENT_ONLY_VAR: 'ok' | ||
}); | ||
|
||
// -- | ||
|
||
variables = await execWithPreload(getFixtureProjectPath('env'), { | ||
env: { | ||
NODE_ENV: 'production', | ||
DEFAULT_NODE_ENV: 'development', | ||
DOTENV_FLOW_PATH: getFixtureProjectPath('node-env') | ||
} | ||
}); | ||
|
||
expect(variables).to.include({ | ||
NODE_ENV: 'production', | ||
DEFAULT_NODE_ENV: 'development', | ||
DEFAULT_ENV_VAR: 'ok', | ||
PRODUCTION_ENV_VAR: 'ok', | ||
PRODUCTION_ONLY_VAR: 'ok' | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
'use strict'; | ||
|
||
const {expect} = require('chai'); | ||
const env_options = require('../lib/env-options'); | ||
|
||
describe('env_options', () => { | ||
it('maps related environment variables to options', () => { | ||
expect(env_options({ | ||
NODE_ENV: 'production', | ||
DEFAULT_NODE_ENV: 'development', | ||
DOTENV_FLOW_PATH: '/path/to/project', | ||
DOTENV_FLOW_ENCODING: 'latin1', | ||
DOTENV_FLOW_PURGE_DOTENV: 'yes' | ||
})) | ||
.to.deep.equal({ | ||
node_env: 'production', | ||
default_node_env: 'development', | ||
path: '/path/to/project', | ||
encoding: 'latin1', | ||
purge_dotenv: 'yes' | ||
}); | ||
}); | ||
|
||
it("doesn't include undefined environment variables", () => { | ||
expect(env_options({ | ||
DEFAULT_NODE_ENV: 'development', | ||
DOTENV_FLOW_ENCODING: 'latin1' | ||
})) | ||
.to.have.keys([ | ||
'default_node_env', | ||
'encoding' | ||
]); | ||
}); | ||
|
||
it('ignores unrelated environment variables', () => { | ||
expect(env_options({ PATH: '/usr/local/bin' })).to.be.empty; | ||
}); | ||
}); |