the-shepherd
Version:
Control a herd of wild processes.
113 lines (98 loc) • 2.66 kB
JavaScript
// Generated by CoffeeScript 2.5.1
(function() {
var $, ChildProcess, Fs, basePath, carefulUnlink, cmd, createBasePath, echo, exists, expandPath, makePath, readPid, seekForBasePath, verbose, warn;
({$, cmd, echo, warn, verbose} = require('./common'));
Fs = require('fs');
ChildProcess = require('child_process');
expandPath = (s) => {
return s != null ? s.replace(/^~/, process.env.HOME).replace(/^%/, basePath) : void 0;
};
exists = (p) => {
try {
p = expandPath(p);
if (p == null) {
return false;
}
Fs.accessSync(p, Fs.constants.R_OK);
return true;
} catch (error) {}
return false;
};
seekForBasePath = () => {
var path, paths;
paths = $(process.cwd().split('/'));
while (paths.length > 0) {
path = paths.join('/') + '/.shep';
if (exists(path)) {
return path;
}
paths.pop();
}
return null;
};
if ("SHEPHERD_HOME" in process.env) {
basePath = process.env.SHEPHERD_HOME;
} else {
if (cmd.base != null) {
basePath = cmd.base + "/.shep";
} else {
basePath = seekForBasePath();
}
}
exists(basePath) && verbose("Using", basePath);
createBasePath = (prefix, cb) => {
return ChildProcess.spawn(`mkdir -p \"${prefix}/.shep\"`, {
shell: true
}).on('exit', (code, signal) => {
return cb(code === 0 ? null : new Error(`mkdir failed: code ${code}`));
});
};
makePath = (...parts) => {
var ret;
if (basePath == null) {
return void 0;
} else {
ret = [basePath].concat(parts).join("/").replace(/\/\//g, '/');
if (ret.indexOf(process.env.HOME) === 0) {
ret = ret.replace(process.env.HOME, '~');
} else if (ret.indexOf(basePath) === 0) {
ret = ret.replace(basePath, '%');
}
return ret;
}
};
readPid = function() {
try {
return parseInt(Fs.readFileSync(expandPath(module.exports.pidFile)).toString(), 10);
} catch (error) {
return void 0;
}
};
carefulUnlink = function(path, cb) {
var err;
path = expandPath(path);
echo("Unlinking file:", path);
try {
return Fs.unlink(path, cb);
} catch (error) {
err = error;
return cb(err);
}
};
Object.assign(module.exports, {
createBasePath,
exists,
makePath,
basePath,
expandPath,
readPid,
carefulUnlink,
exists: exists,
pidFile: makePath("pid"),
socketFile: makePath("socket"),
configFile: makePath("config"),
nginxFile: makePath("nginx"),
nginxTemplate: makePath("nginx.template"),
outputFile: makePath("log")
});
}).call(this);