Permalink
Browse files

first pass at a promised subprocess module, node-only for now

  • Loading branch information...
1 parent 0769e59 commit a96132db81eb695efcb2d402f9608f51df618628 @deanlandolt committed Sep 16, 2011
Showing with 54 additions and 0 deletions.
  1. +45 −0 subprocess.js
  2. +9 −0 tests/subprocess.js
View
@@ -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;
+};
View
@@ -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.