UNPKG

read-conf

Version:
96 lines (86 loc) 1.84 kB
var close, hookUp, plugins, toClose; hookUp = require("hook-up"); plugins = ["Util", "Read", "Iterate", "Plugins", "Watch", "Schema"]; toClose = []; close = async() => { await Promise.all(toClose.map((o) => { return o.close(); })); return process.exit(1); }; process.on("SIGINT", close); process.on("SIGTERM", close); process.on("SIGHUP", close); module.exports = (o = {}) => { var isString, j, len, plugin; ({isString} = require("./pluginUtil")); if (isString(o)) { o = { name: o }; } o.fs = require("fs-extra"); o.path = require("path"); hookUp(o, { actions: ["read", "run", "cleanUp"], catch: { read: o.catch }, state: { read: "running", cleanUp: "cleaning" } }); for (j = 0, len = plugins.length; j < len; j++) { plugin = plugins[j]; require("./plugin" + plugin)(o); } o.read.hookIn(o.position.init, () => { var i; if (!~(i = toClose.indexOf(o))) { return toClose.push(o); } }); o.cleanUp.hookIn(o.position.end, () => { var i; if (~(i = toClose.indexOf(o))) { return toClose.splice(i, 1); } }); o._getBase = () => { if (o.iterate) { return o.bases || []; } else { return (o.bases || [])[0]; } }; o.close = async() => { var e; await o.cleanUp(); if (o.onClose) { try { await o.onClose(o._getBase()); } catch (error) { e = error; console.log(e); } } return o._getBase(); }; return o.read().catch(async(e) => { if (o.watch) { return console.log(e); } else { await o.close(); throw e; } }).then(async() => { if (o.watch) { return o; } else { await o.close(); return o._getBase(); } }); }; module.exports.validate = require("./validate");