2018-07-30 17:40:03 -05:00
|
|
|
const { isString, isPlainObject } = require('lodash');
|
|
|
|
|
|
|
|
const regex = /\$\{[^()]*\}/g;
|
2018-09-19 18:11:22 -05:00
|
|
|
const excludeConfigPaths = ['info.scripts'];
|
2018-07-30 17:40:03 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Allow dynamic config values through the native ES6 template string function.
|
|
|
|
*/
|
2018-09-19 18:11:22 -05:00
|
|
|
const templateConfiguration = (obj, configPath = '') => {
|
2018-07-30 17:40:03 -05:00
|
|
|
// Allow values which looks like such as an ES6 literal string without parenthesis inside (aka function call).
|
2018-09-19 18:11:22 -05:00
|
|
|
// Exclude config with conflicting syntax (e.g. npm scripts).
|
2018-07-30 17:40:03 -05:00
|
|
|
return Object.keys(obj).reduce((acc, key) => {
|
|
|
|
if (isPlainObject(obj[key]) && !isString(obj[key])) {
|
2018-09-19 18:11:22 -05:00
|
|
|
acc[key] = templateConfiguration(obj[key], `${configPath}.${key}`);
|
2018-07-30 17:40:03 -05:00
|
|
|
|
2018-09-19 18:11:22 -05:00
|
|
|
} else if (isString(obj[key])
|
|
|
|
&& !excludeConfigPaths.includes(configPath.substr(1))
|
|
|
|
&& obj[key].match(regex) !== null
|
|
|
|
) {
|
2018-07-30 17:40:03 -05:00
|
|
|
// eslint-disable-next-line prefer-template
|
|
|
|
acc[key] = eval('`' + obj[key] + '`');
|
|
|
|
|
|
|
|
} else {
|
|
|
|
acc[key] = obj[key];
|
|
|
|
}
|
|
|
|
|
|
|
|
return acc;
|
|
|
|
}, {});
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = templateConfiguration;
|