Skip to content

Commit

Permalink
Add node.exec()
Browse files Browse the repository at this point in the history
  • Loading branch information
ry committed Sep 15, 2009
1 parent fdc136d commit 083d150
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 1 deletion.
29 changes: 28 additions & 1 deletion doc/api.html
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,33 @@ <h3 id="_helpers">Helpers</h3><div style="clear:left"></div>
</p>
</dd>
<dt class="hdlist1">
<tt>node.exec(command)</tt>
</dt>
<dd>
<p>
Executes the command as a child process, buffers the output and returns it
in a promise callback.
</p>
<div class="listingblock">
<div class="content">
<pre><tt>node.exec("ls /").addCallback(function (stdout, stderr) {
puts(stdout);
});</tt></pre>
</div></div>
<div class="ulist"><ul>
<li>
<p>
on success: stdout buffer, stderr buffer
</p>
</li>
<li>
<p>
on error: exit code, stdout buffer, stderr buffer
</p>
</li>
</ul></div>
</dd>
<dt class="hdlist1">
<tt>node.cwd()</tt>
</dt>
<dd>
Expand Down Expand Up @@ -1880,7 +1907,7 @@ <h2 id="_extension_api">Extension API</h2>
<div id="footer">
<div id="footer-text">
Version 0.1.10<br />
Last updated 2009-09-15 15:23:59 CEST
Last updated 2009-09-15 15:41:15 CEST
</div>
</div>
</body>
Expand Down
14 changes: 14 additions & 0 deletions doc/api.txt
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,20 @@ Like +puts()+ but without the trailing new-line.
+node.exit(code)+::
Immediately ends the process with the specified code.

+node.exec(command)+::
Executes the command as a child process, buffers the output and returns it
in a promise callback.
+
----------------------------------------
node.exec("ls /").addCallback(function (stdout, stderr) {
puts(stdout);
});
----------------------------------------
+
- on success: stdout buffer, stderr buffer
- on error: exit code, stdout buffer, stderr buffer


+node.cwd()+::
Returns the current working directory of the process.

Expand Down
21 changes: 21 additions & 0 deletions doc/node.1
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,27 @@ node\.exit(code)
Immediately ends the process with the specified code\.
.RE
.PP
node\.exec(command)
.RS 4
Executes the command as a child process, buffers the output and returns it in a promise callback\.
.sp
.RS 4
.nf
node\.exec("ls /")\.addCallback(function (stdout, stderr) {
puts(stdout);
});
.fi
.RE
.sp
.RS 4
\h'-04'\(bu\h'+03'on success: stdout buffer, stderr buffer
.RE
.sp
.RS 4
\h'-04'\(bu\h'+03'on error: exit code, stdout buffer, stderr buffer
.RE
.RE
.PP
node\.cwd()
.RS 4
Returns the current working directory of the process\.
Expand Down
25 changes: 25 additions & 0 deletions src/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,31 @@ node.createChildProcess = function (command) {
return child;
};

node.exec = function (command) {
var child = node.createChildProcess(command);
var stdout = "";
var stderr = "";
var promise = new node.Promise();

child.addListener("output", function (chunk) {
if (chunk) stdout += chunk;
});

child.addListener("error", function (chunk) {
if (chunk) stderr += chunk;
});

child.addListener("exit", function (code) {
if (code == 0) {
promise.emitSuccess(stdout, stderr);
} else {
promise.emitError(code, stdout, stderr);
}
});

return promise;
};

node.tcp.createConnection = function (port, host) {
var connection = new node.tcp.Connection();
connection.connect(port, host);
Expand Down
38 changes: 38 additions & 0 deletions test/mjsunit/test-exec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
include("mjsunit.js");

success_count = 0;
error_count = 0;

node.exec("ls /").addCallback(function (out) {
success_count++;
p(out);
}).addErrback(function (code, out, err) {
error_count++;
puts("error!: " + code);
puts("stdout: " + JSON.stringify(out));
puts("stderr: " + JSON.stringify(err));
});



node.exec("ls /DOES_NOT_EXIST").addCallback(function (out) {
success_count++;
p(out);
assertTrue(out != "");

}).addErrback(function (code, out, err) {
error_count++;

assertEquals("", out);
assertTrue(code != 0);

puts("error!: " + code);
puts("stdout: " + JSON.stringify(out));
puts("stderr: " + JSON.stringify(err));
});


process.addListener("exit", function () {
assertEquals(1, success_count);
assertEquals(1, error_count);
});

0 comments on commit 083d150

Please sign in to comment.