mirror of
https://github.com/strapi/strapi.git
synced 2025-07-26 02:20:32 +00:00

Move existing code to a shared library and invoke it on the build process to parse environment variables on configuration files. Looking for a consistent behavior. close #1590
27 lines
772 B
JavaScript
27 lines
772 B
JavaScript
const { isString, isPlainObject } = require('lodash');
|
|
|
|
const regex = /\$\{[^()]*\}/g;
|
|
|
|
/**
|
|
* Allow dynamic config values through the native ES6 template string function.
|
|
*/
|
|
const templateConfiguration = (obj) => {
|
|
// Allow values which looks like such as an ES6 literal string without parenthesis inside (aka function call).
|
|
return Object.keys(obj).reduce((acc, key) => {
|
|
if (isPlainObject(obj[key]) && !isString(obj[key])) {
|
|
acc[key] = templateConfiguration(obj[key]);
|
|
|
|
} else if (isString(obj[key]) && obj[key].match(regex) !== null) {
|
|
// eslint-disable-next-line prefer-template
|
|
acc[key] = eval('`' + obj[key] + '`');
|
|
|
|
} else {
|
|
acc[key] = obj[key];
|
|
}
|
|
|
|
return acc;
|
|
}, {});
|
|
};
|
|
|
|
module.exports = templateConfiguration;
|