config.js tests

This commit is contained in:
Clément GOUIN
2019-06-19 14:33:12 +02:00
parent 2dd87a3d9b
commit 611d88d088
6 changed files with 72 additions and 13 deletions
+13 -2
View File
@@ -1,16 +1,27 @@
const refConfig = require('./default_config.json');
const srcConfig = require('../config.json');
const fs = require('fs');
const merge = function (ref, src) {
if (typeof ref !== typeof src) {
console.log(ref, src, ref);
return ref;
} else if (typeof ref === 'object') {
const out = {};
Object.keys(ref).forEach(key => out[key] = merge(ref[key], src[key]));
return out;
} else {
console.log(ref, src, src);
return src;
}
};
module.exports = merge(refConfig, srcConfig);
module.exports = function () {
try {
let configData = fs.readFileSync('./config.json');
let config = JSON.parse(configData);
return merge(refConfig, config);
} catch (error) {
console.error('Failed to load config.json');
return refConfig;
}
};