UNPKG

the-shepherd

Version:
125 lines (109 loc) 3.51 kB
// Generated by CoffeeScript 2.5.1 (function() { var $, Actions, Fs, Groups, Health, Nginx, Output, _reading, basePath, configFile, echo, expandPath, parseArguments, readConfig, saveConfig, verbose, warn; ({$, echo, warn, verbose} = require('../common')); Fs = require('fs'); Nginx = require('../daemon/nginx'); Output = require('../daemon/output'); Health = require('../daemon/health'); ({Groups} = require('../daemon/groups')); ({parseArguments} = require('./parse-args')); ({configFile, basePath, expandPath} = require('../files')); Actions = null; _reading = false; saveConfig = function(cb) { var buf, clean; if (_reading) { verbose("Cannot call saveConfig() while reading configuration file."); return typeof cb === "function" ? cb(null, false) : void 0; } verbose("Saving config..."); clean = function(o) { var ref; return ((ref = o != null ? o.replace(basePath, '%') : void 0) != null ? ref : '') + ((o != null ? o.length : void 0) && "\n" || ""); }; buf = clean(Output.toConfig()); Groups.forEach(function(group) { var all_disabled, i, j, len, len1, proc, results; buf += clean(group.toConfig()); all_disabled = true; for (i = 0, len = group.length; i < len; i++) { proc = group[i]; if (!proc.enabled) { continue; } all_disabled = false; break; } if (all_disabled) { return buf += `disable --group ${group.name}\n`; } else { results = []; for (j = 0, len1 = group.length; j < len1; j++) { proc = group[j]; if (!proc.enabled) { results.push(buf += `disable --instance ${proc.id}\n`); } } return results; } }); buf += clean(Health.toConfig()); buf += clean(Nginx.toConfig()); buf += "start\n"; Fs.writeFile(expandPath(configFile), buf, function(err) { return typeof cb === "function" ? cb(err, false) : void 0; }); return true; }; readConfig = function(cb) { var _err_text, config_lines, done, err, next; if (_reading) { return typeof cb === "function" ? cb(null, false) : void 0; } _reading = true; _err_text = ""; config_lines = null; done = () => { if (_reading) { verbose("Finished reading config...", configFile, _err_text); _reading = false; return typeof cb === "function" ? cb(null, true) : void 0; } }; try { config_lines = String(Fs.readFileSync(expandPath(configFile))).split("\n"); } catch (error) { err = error; _err_text = "(empty config file)"; return done(); } (next = () => { var _cmd, cmd, line; if (!((config_lines != null ? config_lines.length : void 0) > 0)) { return done(); } try { line = config_lines.shift(); if (line.length === 0) { return next(); } echo("config:", line); cmd = parseArguments(line); if ((_cmd = cmd._[0]) in Actions) { return Actions[_cmd].onMessage(Actions[_cmd].toMessage(cmd), null, next); } else { return next(); } } catch (error) { err = error; console.error(err.stack); _err_text = String(err); return done(); } })(); return true; }; Object.assign(module.exports, {saveConfig, readConfig}); ({Actions} = require('../actions')); }).call(this);