read-conf
Version:
reads a config file
128 lines (124 loc) • 4.47 kB
JavaScript
var dirname, resolveFrom;
resolveFrom = require("resolve-from");
({dirname} = require("path"));
module.exports = ({run, position}) => {
return run.hookIn(position.before, async(base, o) => {
var arr, conf, def, disablePlugins, disableProp, find, found, getPlugins, i, j, len, len1, plgs, pluginPaths, pluginProp, plugins, prep, prop, reduce, ref, ref1, ref2, ref3, schema, schemaObj, str;
if ((plgs = o.plugins) != null) {
conf = base[o.prop || "config"];
pluginProp = plgs.prop || "plugins";
disableProp = plgs.disableProp || "disablePlugins";
if ((schemaObj = (ref = o._schema) != null ? ref[base._readConfigIndex] : void 0) != null) {
schema = schemaObj.schema;
ref1 = [pluginProp, disableProp];
for (i = 0, len = ref1.length; i < len; i++) {
prop = ref1[i];
if ((def = (ref2 = schema[prop]) != null ? ref2.default : void 0) != null) {
arr = conf[prop] != null ? conf[prop] : conf[prop] = [];
for (j = 0, len1 = def.length; j < len1; j++) {
str = def[j];
if (!~arr.indexOf(str)) {
arr.push(str);
}
}
}
}
// processed plugins are saved over config plugins
// so they shouldn't get validated
if ((((ref3 = schema[pluginProp]) != null ? ref3.default : void 0) != null) || (conf[pluginProp] != null)) {
schemaObj.ignore.push(pluginProp);
}
}
if ((prep = plgs.prepare) != null) {
prep(conf, base);
}
pluginPaths = plgs.paths || [process.cwd()];
plugins = plgs.plugins || conf[pluginProp];
disablePlugins = plgs.disablePlugins || conf[disableProp];
found = [];
find = (name, paths) => {
if ((disablePlugins != null) && ~disablePlugins.indexOf(name)) {
return Promise.resolve();
}
if (~found.indexOf(name)) {
return Promise.resolve();
}
return new Promise((resolve, reject) => {
var filename, k, len2, path;
for (k = 0, len2 = paths.length; k < len2; k++) {
path = paths[k];
filename = resolveFrom.silent(path, name);
if (filename) {
found.push(name);
return resolve({
pluginPath: filename,
plugin: require(filename)
});
}
}
return reject(new Error(`Plugin ${name} not found in ${paths.join(', ')}`));
});
};
getPlugins = (plugins, plg) => {
var k, len2, name, paths, processed;
if (plg != null) {
paths = [dirname(plg.pluginPath)];
processed = [plg];
} else {
paths = pluginPaths;
processed = [];
}
if (plugins != null) {
for (k = 0, len2 = plugins.length; k < len2; k++) {
name = plugins[k];
processed.push(find(name, paths).then((val) => {
var configSchema, plugin, tmpPlgs;
if (val == null) {
return;
}
({plugin} = val);
if ((schemaObj != null) && ((configSchema = plugin.configSchema) != null)) {
if (typeof configSchema === "function") {
configSchema(schemaObj.add, schema);
} else {
schemaObj.add(configSchema);
}
delete plugin.configSchema;
}
if ((tmpPlgs = plugin.plugins) != null) {
delete plugin.plugins;
return getPlugins(tmpPlgs, val);
} else {
return val;
}
}));
}
}
return Promise.all(processed);
};
reduce = (arr) => {
var item, k, len2, tmpArr;
tmpArr = [];
if (arr != null) {
for (k = 0, len2 = arr.length; k < len2; k++) {
item = arr[k];
if (Array.isArray(item)) {
Array.prototype.push.apply(tmpArr, reduce(item));
} else {
if (item != null) {
tmpArr.push(item);
}
}
}
}
return tmpArr;
};
plugins = (await getPlugins(plugins).then(reduce));
if (plgs.plugins) {
return base[pluginProp] = plugins;
} else {
return conf[pluginProp] = plugins;
}
}
});
};