-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
55 lines (50 loc) · 1.67 KB
/
index.js
File metadata and controls
55 lines (50 loc) · 1.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
'use strict';
/**
* This module helps exporting the right configuration to your APP.
* It will try to see if there is any environment variable added
* to the process (process.env.NODE_ENV) which will tell this module
* which environment it is in. If none is found it will assume we are
* in the 'development' environment.
*
* @module ExportConfig
*/
/**
* This module will pick one configObj's environment attributes accordingly to your environment
* guided by process.env.NODE_ENV variable and will merge it with the configObj's default attribute
* with Object.assign. You can define required variables which is ESSENTIAL to your app, if this variable
* is missing, this module will throw an error.
*
* @param {Object} configObj Object which contains your environment configuration.
*
* @example
* const configObj = {
* default: {
* TIMEOUT: 720000,
* VIEW_ENGINE: 'handlebars'
* }
* development: {
* PORT: 9000
* },
* production: {
* PORT: 1337,
* TIMEOUT: 1000
* },
* required: ['VIEW_ENGINE']
* }
*/
module.exports = function ExportConfig(configObj) {
const env = process.env.NODE_ENV || 'development';
const defaultObj = configObj.default || {};
const envObj = configObj[env] || {};
const newConfig = Object.assign({}, defaultObj, envObj);
if (configObj.required && configObj.required.length > 0) {
configObj.required.forEach(function (requiredConfig) {
if (newConfig[requiredConfig] === undefined ||
newConfig[requiredConfig] === null ||
newConfig[requiredConfig] === '') {
throw new Error('There is a configuration missing! Shutting down.');
}
});
}
return newConfig;
};