Initial commit

parents
node_modules
const { readFileSync } = require('fs');
const { merge } = require('lodash');
module.exports = function (filenames, logger) {
const configs = filenames
.map(filename =>loadAndParseFile(filename, logger || console))
.filter(string => string !== null);
if (!configs.length) {
throw new Error(`Failed to read each of these config files: ${filenames.join(', ')}`);
}
return merge(configs[0], ...configs.slice(1));
};
function loadAndParseFile (filename, logger) {
let string;
try {
string = readFileSync(filename, 'utf8');
} catch (error) {
logger.warn(`Warning: can't read config file ${filename}. ${error.message}`);
return null;
}
try {
return JSON.parse(string);
} catch (error) {
throw new Error(`Failed to parse config file ${filename}. ${error.message}`);
}
}
{
"name": "read-config",
"version": "1.0.0",
"description": "Simple config-reader function",
"main": "index.js",
"private": true,
"dependencies": {
"lodash": "4.17.4"
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment