This repository has been archived on 2026-05-02. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
GitBlog.md/src/config.js
T
2021-03-30 16:29:20 +02:00

36 lines
1.0 KiB
JavaScript

const refConfig = require('./config.default.json');
const fs = require('fs');
/**
* Merge resources by reading object keys and keeping reference value only if it's type is different from the source
* @param ref - reference object/value
* @param src - source object/value
* @returns {*}
*/
const merge = (ref, src) => {
if (typeof ref !== typeof src) {
return ref;
} else if (ref.length && !src.length) {
return ref;
} else if (ref.length && src.length) {
return src;
} else if (typeof ref === 'object') {
const out = {};
Object.keys(ref).forEach((key) => out[key] = merge(ref[key], src[key]));
return out;
} else {
return src;
}
};
module.exports = () => {
try {
let configData = fs.readFileSync('config.json', { encoding: 'UTF-8' });
let config = JSON.parse(configData);
return merge(refConfig, config);
} catch (error) {
console.log('\x1b[33m⚠\x1b[0m %s', 'Failed to load config.json : ' + error);
return refConfig;
}
};