SquadJS/squad-server/utils/config-tools.js
Matz Reckeweg 50d92691c0 Merge multiple configurations
Allows to use multiple JSON config files that will be merged into a single configuration
2023-02-04 14:11:23 +01:00

28 lines
765 B
JavaScript

function isObject(item) {
return item && typeof item === 'object' && !Array.isArray(item);
}
export default class ConfigTools {
static mergeConfigs(target, ...sources) {
if (!sources.length) return target;
const source = sources.shift();
if (isObject(target) && isObject(source)) {
for (const key in source) {
if (isObject(source[key])) {
if (!target[key]) Object.assign(target, { [key]: {} });
ConfigTools.mergeConfigs(target[key], source[key]);
} else {
Object.assign(target, { [key]: source[key] });
}
}
}
if (Array.isArray(target) && Array.isArray(source)) {
target = [...target, ...source];
}
return ConfigTools.mergeConfigs(target, ...sources);
}
}