Browse Source

Add node.exec()

v0.7.4-release
Ryan Dahl 15 years ago
parent
commit
083d150bc4
  1. 29
      doc/api.html
  2. 14
      doc/api.txt
  3. 21
      doc/node.1
  4. 25
      src/node.js
  5. 38
      test/mjsunit/test-exec.js

29
doc/api.html

@ -112,6 +112,33 @@ Immediately ends the process with the specified code.
</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>
@ -1880,7 +1907,7 @@ init (Handle&lt;Object&gt; target)
<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>

14
doc/api.txt

@ -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.

21
doc/node.1

@ -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\.

25
src/node.js

@ -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);

38
test/mjsunit/test-exec.js

@ -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);
});
Loading…
Cancel
Save