Skip to content

Instantly share code, notes, and snippets.

@gigafied
Created February 14, 2012 23:05
Show Gist options
  • Save gigafied/1831430 to your computer and use it in GitHub Desktop.
Save gigafied/1831430 to your computer and use it in GitHub Desktop.
Node.js ExecQueue
/*jslint node: true, onevar: false */
/*global jake, desc, task */
var ExecQueue = function (silent) {
this.commands = [];
this.silent = silent;
};
ExecQueue.prototype.add = function () {
for (var i = 0; i < arguments.length; i ++) {
var arg = arguments[i];
var regexp = /["|']+(.*)["|']+/;
var match = arg.match(regexp);
if (match && match[1]) {
arg = arg.replace(match[1], match[1].replace(/\s/, "%20"));
}
var cmd = arg.split(" "),
j, k;
for (j = 0, k = cmd.length; j < k; j++) {
cmd[j] = cmd[j].replace(/\%20/g, " ");
}
this.commands.push([cmd.shift(), cmd]);
}
return this;
};
ExecQueue.prototype.run = function (cwd, cb, success) {
if (typeof cwd === "function") {
cb = cwd;
cwd = null;
}
if (this.commands.length === 0) {
return cb ? cb(success || false) : "";
}
var cmd = this.commands.shift();
var cp = require("child_process");
var child = cp.spawn(cmd[0], cmd[1], {cwd: cwd, env: null, setsid: true});
if (!this.silent) {
child.stdout.pipe(process.stdout, {end: false});
child.stdin.pipe(process.stdin, {end: false});
child.stderr.pipe(process.stderr, {end: false});
child.stdout.setEncoding('utf8');
child.stderr.setEncoding('utf8');
}
child.on("exit", function (code) {
this.run(cwd, cb, !code);
}.bind(this));
};
module.exports = ExecQueue;
var ExecQueue = require("exec-queue");
var q = new ExecQueue();
q.add("echo 1");
q.add("echo 2");
q.add("echo 3");
q.add("echo 4");
q.add("echo 5");
q.add("echo 6");
q.add("echo 7");
q.add("echo 8");
q.add("echo 9");
q.add("echo 10", "echo 11", "echo 12").run(function(success){
console.log("woot!");
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment