This repository has been archived by the owner on Jul 18, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
152 lines (126 loc) · 4.86 KB
/
index.js
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
'use strict';
var fs = require('fs');
var path = require('path');
var CONFIG_DIR = process.env.NODE_CONFIG_DIR;
// if the CONFIG_DIR env variable is undefined, use the app root dir + 'config/'
if (CONFIG_DIR === undefined || CONFIG_DIR === null || typeof CONFIG_DIR !== 'string')
CONFIG_DIR = path.resolve(process.cwd(), 'config/') + '/';
else
CONFIG_DIR = path.resolve(CONFIG_DIR) + '/';
var ENV = process.env.NODE_ENV;
// if the CONFIG_DIR env variable is undefined, use default configuration scripts without environment (e.g. directly in config/ directory)
if (ENV === undefined || ENV === null || typeof ENV !== 'string')
ENV = '';
// halt the app in case of failure during configs' load
var HALT = process.env.NODE_CONFIG_NO_HALT === undefined;
// print out debug messages if enabled
var LOG = process.env.NODE_CONFIG_LOG !== undefined;
/**
* SYNCHRONOUSLY fetches all environments available. Generally, returns the list of sub-directories under the CONFIG_DIR.
* @returns {Array} List of environments available or an empty array in case there is no environment sub-directory
*/
function getEnvs() {
if (LOG)
console.info('Reading environments available in', path.resolve(CONFIG_DIR));
var stat;
var envs = fs.readdirSync(CONFIG_DIR);
// strip away anything but directories - environments must have their own sub-directory
envs.forEach(function(val, i, arr) {
stat = fs.statSync(CONFIG_DIR + val);
if (!stat.isDirectory())
envs.splice(i, 1);
});
return envs;
}
/**
* SYNCHRONOUSLY fetches all config files under the environment specified in parameter. Currently, only .js files are supported.
* @param {String} env The environment to search in for config files
* @returns {Array} List of configuration files as an object with name and path. I.e. for 'app.js' file this array of objects will get returned: [{name: 'app', path: '/app/root/config/app.js'}]
*/
function getConfigs(env) {
var dir = CONFIG_DIR + env;
if (LOG)
console.info('Reading configuration files under', dir);
var configs = [];
var dirContent = fs.readdirSync(dir);
var stat, file, ext;
// strips away file extensions
dirContent.forEach(function(val, i, arr) {
file = dir + '/' + val;
stat = fs.statSync(file);
// skip all items but files
if (!stat.isFile())
return;
// adds JS file types only
ext = path.extname(val);
if (ext === '.js')
configs.push({
name: path.basename(val, ext),
path: file
});
});
return configs;
}
// ---
// code for loading the configs based on NODE_ENV
// ---
// check for non-existence of configuration directory
try {
fs.accessSync(CONFIG_DIR, fs.R_OK);
}
catch (e) {
if (LOG || HALT) { // always print the error if halting the app
console.error('Configuration directory for the app is either missing or non-readable. Expected location is', CONFIG_DIR);
console.error(e);
}
if (HALT) {
console.error('Halting the app.');
process.exit(-1);
} else {
module.exports = null;
return;
}
}
var configs = [];
// if no environment was specified, load the configuration directly under config/ directory
// this is required for production and test releases as the config/ directory does not contain environment directories but rather the appropriate config files themselves
if (ENV === '') {
if (LOG)
console.info('Loading app configuration');
configs = getConfigs('');
}
else {
if (LOG)
console.info('Loading app configuration for the \'%s\' environment', ENV);
var envs = getEnvs();
var e = envs.indexOf(ENV);
if (e < 0) {
if (LOG || HALT) // always print the error if halting the app
console.error('Configuration for \'%s\' environment is not available. Expected location is %s/*.js', ENV, CONFIG_DIR + ENV);
if (HALT) {
console.error('Halting the app.');
process.exit(-1);
} else {
module.exports = null;
return;
}
}
else {
configs = getConfigs(ENV);
}
}
// all configs will be added as a property into this object
var cfg = {};
// add all configurations available into the object being returned by require('app-config') call
configs.forEach(function(val, index, arr) {
// delete the module from cache if in there
// this will ensure the files get reloaded every time this code is being run
// useful for unit tests which may delete the `app-config` module from cache to get the configs reloaded
var requireKey = require.resolve(val.path);
if (require.cache[requireKey])
delete require.cache[requireKey];
cfg[val.name] = require(val.path);
});
if (LOG && configs.length <= 0)
console.warn('No configuration file found');
module.exports = cfg;