2020-06-11 01:15:49 +00:00
|
|
|
// deps
|
|
|
|
const paths = require('path');
|
|
|
|
const async = require('async');
|
2020-06-12 04:40:20 +00:00
|
|
|
const moment = require('moment');
|
2020-06-11 01:15:49 +00:00
|
|
|
|
|
|
|
const _ = require('lodash');
|
2020-06-12 03:38:33 +00:00
|
|
|
const mapValuesDeep = require('deepdash/getMapValuesDeep')(_);
|
2020-06-11 01:15:49 +00:00
|
|
|
|
|
|
|
module.exports = class ConfigLoader {
|
2020-06-12 03:16:15 +00:00
|
|
|
constructor(
|
2020-06-16 03:21:26 +00:00
|
|
|
{
|
|
|
|
hotReload = true,
|
|
|
|
defaultConfig = {},
|
|
|
|
defaultsCustomizer = null,
|
|
|
|
onReload = null,
|
2020-07-07 01:13:22 +00:00
|
|
|
keepWsc = false,
|
2022-06-05 20:04:25 +00:00
|
|
|
} = {
|
|
|
|
hotReload: true,
|
|
|
|
defaultConfig: {},
|
|
|
|
defaultsCustomizer: null,
|
|
|
|
onReload: null,
|
|
|
|
keepWsc: false,
|
2020-06-16 03:21:26 +00:00
|
|
|
}
|
2022-06-05 20:04:25 +00:00
|
|
|
) {
|
2020-06-11 01:15:49 +00:00
|
|
|
this.current = {};
|
2020-06-12 03:16:15 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
this.hotReload = hotReload;
|
|
|
|
this.defaultConfig = defaultConfig;
|
2020-06-12 03:16:15 +00:00
|
|
|
this.defaultsCustomizer = defaultsCustomizer;
|
2022-06-05 20:04:25 +00:00
|
|
|
this.onReload = onReload;
|
|
|
|
this.keepWsc = keepWsc;
|
2020-06-11 01:15:49 +00:00
|
|
|
}
|
|
|
|
|
2020-06-12 03:16:15 +00:00
|
|
|
init(baseConfigPath, cb) {
|
|
|
|
this.baseConfigPath = baseConfigPath;
|
|
|
|
return this._reload(baseConfigPath, cb);
|
2020-06-11 01:15:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
get() {
|
|
|
|
return this.current;
|
|
|
|
}
|
|
|
|
|
2020-06-12 03:16:15 +00:00
|
|
|
_reload(baseConfigPath, cb) {
|
|
|
|
let defaultConfig;
|
|
|
|
if (_.isFunction(this.defaultConfig)) {
|
|
|
|
defaultConfig = this.defaultConfig();
|
|
|
|
} else if (_.isObject(this.defaultConfig)) {
|
|
|
|
defaultConfig = this.defaultConfig;
|
|
|
|
} else {
|
|
|
|
defaultConfig = {};
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// 1 - Fetch base configuration from |baseConfigPath|
|
|
|
|
// 2 - Merge with |defaultConfig|
|
|
|
|
// 3 - Resolve any includes
|
|
|
|
// 4 - Resolve @reference and @environment
|
|
|
|
// 5 - Perform any validation
|
|
|
|
//
|
|
|
|
async.waterfall(
|
|
|
|
[
|
2022-06-05 20:04:25 +00:00
|
|
|
callback => {
|
2020-06-12 03:16:15 +00:00
|
|
|
return this._loadConfigFile(baseConfigPath, callback);
|
|
|
|
},
|
|
|
|
(config, callback) => {
|
|
|
|
if (_.isFunction(this.defaultsCustomizer)) {
|
|
|
|
const stack = [];
|
|
|
|
const mergedConfig = _.mergeWith(
|
|
|
|
defaultConfig,
|
|
|
|
config,
|
|
|
|
(defaultVal, configVal, key, target, source) => {
|
|
|
|
var path;
|
2022-06-05 20:04:25 +00:00
|
|
|
while (true) {
|
|
|
|
// eslint-disable-line no-constant-condition
|
2020-06-12 03:16:15 +00:00
|
|
|
if (!stack.length) {
|
2022-06-05 20:04:25 +00:00
|
|
|
stack.push({ source, path: [] });
|
2020-06-12 03:16:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const prev = stack[stack.length - 1];
|
|
|
|
|
|
|
|
if (source === prev.source) {
|
|
|
|
path = prev.path.concat(key);
|
2022-06-05 20:04:25 +00:00
|
|
|
stack.push({ source: configVal, path });
|
2020-06-12 03:16:15 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
stack.pop();
|
|
|
|
}
|
|
|
|
|
|
|
|
path = path.join('.');
|
2022-06-05 20:04:25 +00:00
|
|
|
return this.defaultsCustomizer(
|
|
|
|
defaultVal,
|
|
|
|
configVal,
|
|
|
|
key,
|
|
|
|
path
|
|
|
|
);
|
2020-06-12 03:16:15 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
return callback(null, mergedConfig);
|
|
|
|
}
|
|
|
|
|
|
|
|
return callback(null, _.merge(defaultConfig, config));
|
|
|
|
},
|
|
|
|
(config, callback) => {
|
|
|
|
const configRoot = paths.dirname(baseConfigPath);
|
|
|
|
return this._resolveIncludes(configRoot, config, callback);
|
|
|
|
},
|
|
|
|
(config, callback) => {
|
|
|
|
config = this._resolveAtSpecs(config);
|
|
|
|
return callback(null, config);
|
|
|
|
},
|
|
|
|
],
|
|
|
|
(err, config) => {
|
|
|
|
if (!err) {
|
|
|
|
this.current = config;
|
|
|
|
}
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-06-11 01:15:49 +00:00
|
|
|
_convertTo(value, type) {
|
|
|
|
switch (type) {
|
2022-06-05 20:04:25 +00:00
|
|
|
case 'bool':
|
|
|
|
case 'boolean':
|
|
|
|
value = '1' === value || 'true' === value.toLowerCase();
|
2020-06-11 01:15:49 +00:00
|
|
|
break;
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
case 'number':
|
2020-06-11 01:15:49 +00:00
|
|
|
{
|
|
|
|
const num = parseInt(value);
|
|
|
|
if (!isNaN(num)) {
|
|
|
|
value = num;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
case 'object':
|
2020-06-11 01:15:49 +00:00
|
|
|
try {
|
|
|
|
value = JSON.parse(value);
|
2022-06-05 20:04:25 +00:00
|
|
|
} catch (e) {
|
2020-06-12 04:40:20 +00:00
|
|
|
// ignored
|
|
|
|
}
|
2020-06-11 01:15:49 +00:00
|
|
|
break;
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
case 'timestamp':
|
2020-06-11 01:15:49 +00:00
|
|
|
{
|
|
|
|
const m = moment(value);
|
|
|
|
if (m.isValid()) {
|
|
|
|
value = m;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
_resolveEnvironmentVariable(spec) {
|
2020-06-12 04:40:20 +00:00
|
|
|
const [, varName, type, array] = spec.split(':');
|
2020-06-11 01:15:49 +00:00
|
|
|
if (!varName) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let value = process.env[varName];
|
|
|
|
if (!value) {
|
2020-07-07 03:14:31 +00:00
|
|
|
// console is about as good as we can do here
|
2022-06-05 20:04:25 +00:00
|
|
|
return console.info(
|
|
|
|
`WARNING: environment variable "${varName}" from spec "${spec}" not found!`
|
|
|
|
);
|
2020-06-11 01:15:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ('array' === array) {
|
|
|
|
value = value.split(',').map(v => this._convertTo(v, type));
|
|
|
|
} else {
|
|
|
|
value = this._convertTo(value, type);
|
|
|
|
}
|
|
|
|
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
_loadConfigFile(filePath, cb) {
|
|
|
|
const ConfigCache = require('./config_cache');
|
|
|
|
|
|
|
|
const options = {
|
|
|
|
filePath,
|
2022-06-05 20:04:25 +00:00
|
|
|
hotReload: this.hotReload,
|
|
|
|
keepWsc: this.keepWsc,
|
|
|
|
callback: this._configFileChanged.bind(this),
|
2020-06-11 01:15:49 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
ConfigCache.getConfigWithOptions(options, (err, config) => {
|
2020-06-19 03:29:24 +00:00
|
|
|
if (err) {
|
|
|
|
err.configPath = options.filePath;
|
|
|
|
}
|
2020-06-11 01:15:49 +00:00
|
|
|
return cb(err, config);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
_configFileChanged({ fileName, fileRoot }) {
|
2020-06-11 01:15:49 +00:00
|
|
|
const reCachedPath = paths.join(fileRoot, fileName);
|
2020-06-12 04:40:20 +00:00
|
|
|
if (this.configPaths.includes(reCachedPath)) {
|
|
|
|
this._reload(this.baseConfigPath, err => {
|
2020-06-16 03:21:26 +00:00
|
|
|
if (_.isFunction(this.onReload)) {
|
|
|
|
this.onReload(err, reCachedPath);
|
2020-06-12 04:40:20 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2020-06-11 01:15:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_resolveIncludes(configRoot, config, cb) {
|
|
|
|
if (!Array.isArray(config.includes)) {
|
2022-06-05 20:04:25 +00:00
|
|
|
this.configPaths = [this.baseConfigPath];
|
2020-06-11 01:15:49 +00:00
|
|
|
return cb(null, config);
|
|
|
|
}
|
|
|
|
|
|
|
|
// If a included file is changed, we need to re-cache, so this
|
|
|
|
// must be tracked...
|
|
|
|
const includePaths = config.includes.map(inc => paths.join(configRoot, inc));
|
2022-06-05 20:04:25 +00:00
|
|
|
async.eachSeries(
|
|
|
|
includePaths,
|
|
|
|
(includePath, nextIncludePath) => {
|
|
|
|
this._loadConfigFile(includePath, (err, includedConfig) => {
|
|
|
|
if (err) {
|
|
|
|
return nextIncludePath(err);
|
|
|
|
}
|
2020-06-11 01:15:49 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
_.defaultsDeep(config, includedConfig);
|
|
|
|
return nextIncludePath(null);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
err => {
|
|
|
|
this.configPaths = [this.baseConfigPath, ...includePaths];
|
|
|
|
return cb(err, config);
|
|
|
|
}
|
|
|
|
);
|
2020-06-11 01:15:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_resolveAtSpecs(config) {
|
2022-06-05 20:04:25 +00:00
|
|
|
return mapValuesDeep(config, value => {
|
|
|
|
if (_.isString(value) && '@' === value.charAt(0)) {
|
|
|
|
if (value.startsWith('@reference:')) {
|
|
|
|
const refPath = value.slice(11);
|
|
|
|
value = _.get(config, refPath, value);
|
|
|
|
} else if (value.startsWith('@environment:')) {
|
|
|
|
value = this._resolveEnvironmentVariable(value) || value;
|
2020-06-11 01:15:49 +00:00
|
|
|
}
|
|
|
|
}
|
2022-06-05 20:04:25 +00:00
|
|
|
|
|
|
|
return value;
|
|
|
|
});
|
2020-06-11 01:15:49 +00:00
|
|
|
}
|
|
|
|
};
|