Skip to content

Commit

Permalink
first pass at a promised subprocess module, node-only for now
Browse files Browse the repository at this point in the history
  • Loading branch information
deanlandolt committed Sep 16, 2011
1 parent 0769e59 commit a96132d
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
45 changes: 45 additions & 0 deletions subprocess.js
@@ -0,0 +1,45 @@
var defer = require("./promise").defer;

// TODO pull bits from narwhal and get running on raw rhino
try {
var childProcess = require("child_process");
}
catch (e) {
// TODO needs console module
//require("./console").warn("The child-process module not supported on your platform");
}


exports.spawn = function() {
var _child = childProcess.spawn.apply(this, arguments);

// TODO use es5-shim?
var child = Object.create(_child);

// node's child.kill should really be raise -- let's correcting this oversight
child.raise = function() {
_child.kill.apply(_child, arguments);
};

// TODO promisify streams
[ "stdin", "stdout", "stderr" ].forEach(function(key, i) {
child[key] = _child[key];
child[key].fd = _child.fds[i];
});

return child;
};


exports.execute = function(command, options) {
// TODO isA check for path-like object in command to do childProcess.execFile?
var deferred = defer();
var callback = function(e, stdout, stderr) {
options = options || {};
if (e != null) deferred.reject(e);
else if (options.verbose) deferred.resolve({stdout: stdout, stderr: stderr});
else deferred.resolve(stdout);
};
childProcess.exec(command, options, callback);
return deferred.promise;
};
9 changes: 9 additions & 0 deletions tests/subprocess.js
@@ -0,0 +1,9 @@

var promise = require("../promise");
var subprocess = require("../subprocess");
var assert = require("assert"); // TODO why not roll patr into promised-io

// FIXME just testing a simple execute for now
promise.when(subprocess.execute("echo hello"), function(response) {
assert.equal(response, "hello\n");
});

0 comments on commit a96132d

Please sign in to comment.