forked from kriszyp/promised-io
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
first pass at a promised subprocess module, node-only for now
- Loading branch information
1 parent
0769e59
commit a96132d
Showing
2 changed files
with
54 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"); | ||
}); |