From 2943875a10eb5baf61495d601e5f9236df49128f Mon Sep 17 00:00:00 2001 From: Ondrej Sevcik Date: Fri, 1 Dec 2017 15:35:39 +0100 Subject: [PATCH 1/3] #44 Add check for env variables presence --- README.md | 5 ++++- index.js | 30 ++++++++++++++++++++++-------- 2 files changed, 26 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index c9782a9..d792a22 100644 --- a/README.md +++ b/README.md @@ -35,7 +35,10 @@ Next, configure `config/dotenv.js`. // config/dotenv.js module.exports = function(env) { return { - clientAllowedKeys: ['DROPBOX_KEY'] + clientAllowedKeys: ['DROPBOX_KEY'], + // Fail build when there is missing any of clientAllowedKeys environment variables. + // By default false. + failOnMissingKey: false, }; }; ``` diff --git a/index.js b/index.js index 202c0cf..cc254e4 100644 --- a/index.js +++ b/index.js @@ -23,23 +23,37 @@ module.exports = { let configFactory = path.join(root, 'config', 'dotenv.js'); let options = { path: path.join(root, '.env'), - clientAllowedKeys: [] + clientAllowedKeys: [], + failOnMissingKey: false, }; if (fs.existsSync(configFactory)) { Object.assign(options, require(configFactory)(this._resolveEnvironment())); } - if (fs.existsSync(options.path) && dotenv.config({ path: options.path })) { - let loadedConfig = dotenv.parse(fs.readFileSync(options.path)); - let allowedKeys = options.clientAllowedKeys || []; + let loadedConfig = dotenv.config({ path: options.path }); + if (loadedConfig.error && options.failOnMissingKey) { + throw new Error('[ember-cli-dotenv]: Error while loading ' + options.path + '.'); + } else if (loadedConfig.error) { + // Nothing to load + return; + } - this._config = allowedKeys.reduce((accumulator, key) => { - accumulator[key] = loadedConfig[key]; + let allowedKeys = options.clientAllowedKeys || []; - return accumulator; - }, {}); + if (options.failOnMissingKey) { + allowedKeys.map(key => { + if (!loadedConfig.parsed[key]) { + throw new Error('[ember-cli-dotenv]: Required environment variable \'' + key + '\' is missing.'); + } + }); } + + this._config = allowedKeys.reduce((accumulator, key) => { + accumulator[key] = loadedConfig.parsed[key]; + + return accumulator; + }, {}); }, _resolveEnvironment() { From c4f821ad7ce486fa8feb7ca74af26f9088f1b300 Mon Sep 17 00:00:00 2001 From: Ondrej Sevcik Date: Mon, 4 Dec 2017 08:15:56 +0100 Subject: [PATCH 2/3] #44 Display warnings when failOnMissingKey disabled better than nothing at all --- index.js | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/index.js b/index.js index cc254e4..96aecb5 100644 --- a/index.js +++ b/index.js @@ -32,22 +32,30 @@ module.exports = { } let loadedConfig = dotenv.config({ path: options.path }); - if (loadedConfig.error && options.failOnMissingKey) { - throw new Error('[ember-cli-dotenv]: Error while loading ' + options.path + '.'); - } else if (loadedConfig.error) { - // Nothing to load - return; + + // It might happen that environment config is missing or corrupted + if (loadedConfig.error) { + let loadingErrMsg = `[ember-cli-dotenv]: ${loadedConfig.error.message}`; + if (options.failOnMissingKey) { + throw new Error(loadingErrMsg); + } else { + console.warn(loadingErrMsg); + return; + } } let allowedKeys = options.clientAllowedKeys || []; - if (options.failOnMissingKey) { - allowedKeys.map(key => { - if (!loadedConfig.parsed[key]) { - throw new Error('[ember-cli-dotenv]: Required environment variable \'' + key + '\' is missing.'); + allowedKeys.map(key => { + if (loadedConfig.parsed[key] === undefined) { + let errMsg = '[ember-cli-dotenv]: Required environment variable \'' + key + '\' is missing.'; + if (options.failOnMissingKey) { + throw new Error(errMsg); + } else { + console.warn(errMsg); } - }); - } + } + }); this._config = allowedKeys.reduce((accumulator, key) => { accumulator[key] = loadedConfig.parsed[key]; From d0d6b791b1fe44aab55c858c6c354e287d41aa2c Mon Sep 17 00:00:00 2001 From: Ondrej Sevcik Date: Mon, 4 Dec 2017 14:25:12 +0100 Subject: [PATCH 3/3] #44 Use forEach instead of map --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index 96aecb5..f7bd937 100644 --- a/index.js +++ b/index.js @@ -46,7 +46,7 @@ module.exports = { let allowedKeys = options.clientAllowedKeys || []; - allowedKeys.map(key => { + allowedKeys.forEach(key => { if (loadedConfig.parsed[key] === undefined) { let errMsg = '[ember-cli-dotenv]: Required environment variable \'' + key + '\' is missing.'; if (options.failOnMissingKey) {