-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfigComponent.js
64 lines (48 loc) · 1.87 KB
/
configComponent.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
'use strict';
/**
* @ref http://docs.ansible.com/ansible/YAMLSyntax.html
*/
const _ = require('lodash');
const
loadSync = require('./loadSync'),
utils = require('./utils'),
checkSecretParameters = _.memoize(require('./checkSecretParameters')),
view = require('./view'),
path = require('path')
;
const configComponent = module.exports;
// get Config dynamically depending on NODE_ENV
configComponent.get = get;
// Same as get but return config metadata too
configComponent._getFullConfig = _getFullConfig;
// View prettyfied config depending on NODE_ENV
configComponent.view = view;
// Sync
configComponent.loadSync = _.memoize(loadSync);
function get (module) {
return _.omit(configComponent._getFullConfig(module), ['parameters', 'env', 'fileEnv', 'filePath']);
}
function _getFullConfig (module) {
let config, filePath, fileEnv;
const secretParamsPath = path.join(utils.resolve(module.filename), 'secret_parameters.yml');
if (!checkSecretParameters.cache.has(secretParamsPath)) {
checkSecretParameters(secretParamsPath);
}
filePath = utils.getEnvPathFrom(module.filename);
fileEnv = utils.getEnv(module.filename);
try {
config = configComponent.loadSync(filePath);
} catch (err) {
if (err.code !== 'ENOENT' || fileEnv === 'production') {
// Unexpected Error or production config not found.
throw err;
}
console.warn('ConfigComponent: No config found for NODE_ENV=' + process.env.NODE_ENV + ' and File: ' + module.filename + ' fallback on config_production.yml');
filePath = utils.getProductionPathFrom(module.filename);
fileEnv = 'production';
config = configComponent.loadSync(filePath);
}
config.filePath = filePath;
config.fileEnv = fileEnv;
return config;
}