Skip to content
This repository

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

Open
wants to merge 1 commit into from

1 participant

Dean Landolt
Dean Landolt

i've got a bunch of promised-io stuff i wrote a while ago but never bothered getting it all together...here's a start

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 1 unique commit by 1 author.

Sep 16, 2011
Dean Landolt first pass at a promised subprocess module, node-only for now a96132d
This page is out of date. Refresh to see the latest.
45  subprocess.js
... ...
@@ -0,0 +1,45 @@
  1
+var defer = require("./promise").defer;
  2
+
  3
+// TODO pull bits from narwhal and get running on raw rhino
  4
+try {
  5
+    var childProcess = require("child_process");
  6
+}
  7
+catch (e) {
  8
+    // TODO needs console module 
  9
+    //require("./console").warn("The child-process module not supported on your platform");
  10
+}
  11
+
  12
+
  13
+exports.spawn = function() {
  14
+    var _child = childProcess.spawn.apply(this, arguments);
  15
+    
  16
+    // TODO use es5-shim?
  17
+    var child = Object.create(_child);
  18
+    
  19
+    // node's child.kill should really be raise -- let's correcting this oversight
  20
+    child.raise = function() {
  21
+        _child.kill.apply(_child, arguments);
  22
+    };
  23
+    
  24
+    // TODO promisify streams
  25
+    [ "stdin", "stdout", "stderr" ].forEach(function(key, i) {
  26
+        child[key] = _child[key];
  27
+        child[key].fd = _child.fds[i];
  28
+    });
  29
+    
  30
+    return child;
  31
+};
  32
+
  33
+
  34
+exports.execute = function(command, options) {
  35
+    // TODO isA check for path-like object in command to do childProcess.execFile?
  36
+    var deferred = defer();
  37
+    var callback = function(e, stdout, stderr) {
  38
+        options = options || {};
  39
+        if (e != null) deferred.reject(e);
  40
+        else if (options.verbose) deferred.resolve({stdout: stdout, stderr: stderr});
  41
+        else deferred.resolve(stdout);
  42
+    };
  43
+    childProcess.exec(command, options, callback);
  44
+    return deferred.promise;
  45
+};
9  tests/subprocess.js
... ...
@@ -0,0 +1,9 @@
  1
+
  2
+var promise = require("../promise");
  3
+var subprocess = require("../subprocess");
  4
+var assert = require("assert"); // TODO why not roll patr into promised-io
  5
+
  6
+// FIXME just testing a simple execute for now
  7
+promise.when(subprocess.execute("echo hello"), function(response) {
  8
+	assert.equal(response, "hello\n");
  9
+});
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.