Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
const configDir = process.env.ROB_CONFIG_DIR || 'config';
try {
const statDir = fs.statSync(configDir);
if (!statDir.isDirectory()) {
throw new Error('Not a directory');
}
} catch (e) {
throw new Error(`[rob-config] Config directory "${e.path}" does not exists.`);
}
// check and load convict formats if availables
const formatsFile = path.resolve(configDir, 'formats.js');
try {
const statFile = fs.statSync(formatsFile);
if (statFile.isFile()) {
convict.addFormats(require(formatsFile));
}
} catch (e) {}
// check and load convict schema
const schemaFile = path.resolve(configDir, 'schema.js');
try {
const statFile = fs.statSync(schemaFile);
if (!statFile.isFile()) {
throw new Error('Not a file');
}
} catch (e) {
throw new Error(`[rob-config] Schema file "${e.path}" does not exists.`);
}
const conf = convict(require(schemaFile));
// check and load related environment config
module.exports = (config) => {
const {create, formats} = createFormatCollection();
visitConfig({
default: (v, node) => {
const result = isObjectHash(v) && v.default;
node.isRequired = (result === null);
node.isOptional = (result !== null);
return result;
},
format: v => (typeof v === 'string') ? v : create([].concat(v))
})(config);
convict.addFormats(formats);
const instance = convict(config);
try {
instance.validate({allowed: 'strict'});
} catch ({message}) {
instance.set(
'errors',
message
.split('\n')
.map(v => v.split(':').map(vv => vv.trim()))
.reduce((r, [k, v]) => Object.assign(r, {[k]: v}), {})
);
}
return {