Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
exports.runCmds = function(cmds, dir, cb) {
// it's valid to send no commands, you just get empty results.
if (is.nullOrUndef(cmds))
return cb(null, [], []);
if (!is.array(cmds))
return cb(new Error('Bad cmds parameter: '+inspect(cmds)));
if (!is.func(cb))
return cb(new Error('Bad cb parameter: '+inspect(cb)));
// one for each entry in the array
var outputs = [];
var codes = [];
// iterator function for async.eachSeries
var iterator = function(cmd, cb) {
// default directorry is the cwd
if (!is.nonEmptyStr(cmd.dir)) cmd.dir = dir;
var output = ({stdout:'', stderr:''});
if (!is.nonEmptyStr(cmd.cmd) || !is.array(cmd.args))
return debug('Bad command '+inspect(cmd));
exports.runCmd(cmd.cmd, cmd.args, cmd.dir, output,
RestApi.prototype.addRoute = function(verb, path, func) {
if (!is.nonEmptyStr(verb)) {
debug('RestApi.prototype.addRoute bad verb:', verb);
return false;
}
if (!is.nonEmptyStr(path)) {
debug('RestApi.prototype.addRoute bad path:', path);
return false;
}
if (!is.func(func)) {
debug('RestApi.prototype.addRoute bad func:', func);
return false;
}
var httpVerb = verb.toUpperCase();
debug('Adding: '+httpVerb+' '+path);
if (!this.routes[httpVerb]) this.routes[httpVerb] = {};
// Create a handling function for the route that gathers the
// the HTTP request body and passes that on.
this.routes[httpVerb][path] = function(req, res) {
var body = ''; // buffer for body.
// collect the body in buf;
req.on('data', function (data) { body += data.toString(); });
Master.prototype.taskWorker = function(worker, task, args, cb) {
var self = this;
if (!is.func(task) && !is.obj(task))
return asyncerr(new Error('Bad task object: '+inspect(task)),cb);
if (!is.nonEmptyObj(worker))
return asyncerr(new Error('Bad parameter for worker: '+
inspect(worker)), cb);
if (!is.func(cb))
debug('Master.taskAll received no callback function.');
// args is an optional argument and may not be present
if (is.func(args)) {
cb = args;
} else if (is.array(args)) {
if (is.obj(task) && !task.args) {
task.args = args;
args = undefined;
exports.gitClone = function(task, targetDir, cb) {
if (!is.obj(task) || !is.nonEmptyStr(task.user) || !is.nonEmptyStr(task.repo))
return cb(new Error('Error bad task object: '+inspect(task)));
if (!is.func(cb))
return cb(new Error('Bad cb parameter: '+inspect(cb)));
// assume, if the target directory is present, it was installed.
// FIXME: This is kinda dumb.
if (fs.existsSync(targetDir))
return cb();
debug('gitClone task: '+inspect(task));
var url = 'https://github.com/' + task.user + '/' + task.repo;
debug('gitClone url: '+url);
exec(['git', 'clone', url], function(err, out, code) {
if (err) {
debug('runGithub git clone error: '+inspect(err));
return cb(err);
}
if (code !== 0) {
Master.prototype.taskWorker = function(worker, task, args, cb) {
var self = this;
if (!is.func(task) && !is.obj(task))
return asyncerr(new Error('Bad task object: '+inspect(task)),cb);
if (!is.nonEmptyObj(worker))
return asyncerr(new Error('Bad parameter for worker: '+
inspect(worker)), cb);
if (!is.func(cb))
debug('Master.taskAll received no callback function.');
// args is an optional argument and may not be present
if (is.func(args)) {
cb = args;
} else if (is.array(args)) {
if (is.obj(task) && !task.args) {
task.args = args;
args = undefined;
}
}
// depending on the type of task, handle it
process.nextTick(function() {
if (is.func(task)) {
self.sendFuncToWorker(worker, task, args, cb);
} else if (is.obj(task)) {
switch(task.type) {
case 'github':
self.sendGithubToWorker(worker, task, cb);
Master.prototype.taskWorker = function(worker, task, args, cb) {
var self = this;
if (!is.func(task) && !is.obj(task))
return asyncerr(new Error('Bad task object: '+inspect(task)),cb);
if (!is.nonEmptyObj(worker))
return asyncerr(new Error('Bad parameter for worker: '+
inspect(worker)), cb);
if (!is.func(cb))
debug('Master.taskAll received no callback function.');
// args is an optional argument and may not be present
if (is.func(args)) {
cb = args;
} else if (is.array(args)) {
if (is.obj(task) && !task.args) {
task.args = args;
args = undefined;
}
}
// depending on the type of task, handle it
process.nextTick(function() {
if (is.func(task)) {
self.sendFuncToWorker(worker, task, args, cb);
process.nextTick(function() {
if (is.func(task)) {
self.sendFuncToWorker(worker, task, args, cb);
} else if (is.obj(task)) {
switch(task.type) {
case 'github':
self.sendGithubToWorker(worker, task, cb);
break;
case 'file':
self.sendFileToWorker(worker, task, cb);
break;
default:
debug('Unknown task.type in task: '+inspect(task));
cb(new Error('Unknown task.type in task: '+inspect(task)));
break;
}
}
});
child.on('close', function (code) {
debug('child process exited with code ' + code);
if (is.func(cb)) cb(null, output, code);
});
};
fs.unlink(file, function (err) {
if (err) {
debug('unlink: failed to delete file '+file);
return cb(err);
}
if (cb && is.func(cb)) return cb();
});
};
fs.writeFile(fileName, fileContents, function (err) {
if (err) {
debug('writeFile, could not write file: '+err.message);
return cb('writeFile, could not write file: '+err.message);
}
if (is.func(cb)) cb(null);
});
};