Skip to content

Commit 8a7f7ce

Browse files
committed
add exec.stdout
Change `exec.output` to `exec.stdout` and deprecate `output`.
1 parent 74f1ff8 commit 8a7f7ce

File tree

3 files changed

+31
-27
lines changed

3 files changed

+31
-27
lines changed

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -481,24 +481,24 @@ Available options (all `false` by default):
481481
Examples:
482482

483483
```javascript
484-
var version = exec('node --version', {silent:true}).output;
484+
var version = exec('node --version', {silent:true}).stdout;
485485

486486
var child = exec('some_long_running_process', {async:true});
487487
child.stdout.on('data', function(data) {
488488
/* ... do something with data ... */
489489
});
490490

491-
exec('some_long_running_process', function(code, output, stderr) {
491+
exec('some_long_running_process', function(code, stdout, stderr) {
492492
console.log('Exit code:', code);
493-
console.log('Program output:', output);
493+
console.log('Program output:', stdout);
494494
console.log('Program stderr:', stderr);
495495
});
496496
```
497497

498498
Executes the given `command` _synchronously_, unless otherwise specified. When in synchronous
499-
mode returns the object `{ code:..., output:... , stderr:... }`, containing the program's
500-
`output` (stdout), `stderr`, and its exit `code`. Otherwise returns the child process object,
501-
and the `callback` gets the arguments `(code, output, stderr)`.
499+
mode returns the object `{ code:..., stdout:... , stderr:... }`, containing the program's
500+
`stdout`, `stderr`, and its exit `code`. Otherwise returns the child process object,
501+
and the `callback` gets the arguments `(code, stdout, stderr)`.
502502

503503
**Note:** For long-lived processes, it's best to run `exec()` asynchronously as
504504
the current synchronous implementation uses a lot of CPU. This should be getting

src/exec.js

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -142,15 +142,16 @@ function execSync(cmd, opts) {
142142
// True if successful, false if not
143143
var obj = {
144144
code: code,
145-
output: stdout,
145+
output: stdout, // deprecated
146+
stdout: stdout,
146147
stderr: stderr
147148
};
148149
return obj;
149150
} // execSync()
150151

151152
// Wrapper around exec() to enable echoing output to console in real time
152153
function execAsync(cmd, opts, callback) {
153-
var output = '';
154+
var stdout = '';
154155
var stderr = '';
155156

156157
var options = common.extend({
@@ -159,11 +160,11 @@ function execAsync(cmd, opts, callback) {
159160

160161
var c = child.exec(cmd, {env: process.env, maxBuffer: 20*1024*1024}, function(err) {
161162
if (callback)
162-
callback(err ? err.code : 0, output, stderr);
163+
callback(err ? err.code : 0, stdout, stderr);
163164
});
164165

165166
c.stdout.on('data', function(data) {
166-
output += data;
167+
stdout += data;
167168
if (!options.silent)
168169
process.stdout.write(data);
169170
});
@@ -187,24 +188,24 @@ function execAsync(cmd, opts, callback) {
187188
//@ Examples:
188189
//@
189190
//@ ```javascript
190-
//@ var version = exec('node --version', {silent:true}).output;
191+
//@ var version = exec('node --version', {silent:true}).stdout;
191192
//@
192193
//@ var child = exec('some_long_running_process', {async:true});
193194
//@ child.stdout.on('data', function(data) {
194195
//@ /* ... do something with data ... */
195196
//@ });
196197
//@
197-
//@ exec('some_long_running_process', function(code, output, stderr) {
198+
//@ exec('some_long_running_process', function(code, stdout, stderr) {
198199
//@ console.log('Exit code:', code);
199-
//@ console.log('Program output:', output);
200+
//@ console.log('Program output:', stdout);
200201
//@ console.log('Program stderr:', stderr);
201202
//@ });
202203
//@ ```
203204
//@
204205
//@ Executes the given `command` _synchronously_, unless otherwise specified. When in synchronous
205-
//@ mode returns the object `{ code:..., output:... , stderr:... }`, containing the program's
206-
//@ `output` (stdout), `stderr`, and its exit `code`. Otherwise returns the child process object,
207-
//@ and the `callback` gets the arguments `(code, output, stderr)`.
206+
//@ mode returns the object `{ code:..., stdout:... , stderr:... }`, containing the program's
207+
//@ `stdout`, `stderr`, and its exit `code`. Otherwise returns the child process object,
208+
//@ and the `callback` gets the arguments `(code, stdout, stderr)`.
208209
//@
209210
//@ **Note:** For long-lived processes, it's best to run `exec()` asynchronously as
210211
//@ the current synchronous implementation uses a lot of CPU. This should be getting

test/exec.js

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -44,20 +44,20 @@ process.exit = old_exit;
4444
var result = shell.exec('node -e \"console.log(1234);\"');
4545
assert.equal(shell.error(), null);
4646
assert.equal(result.code, 0);
47-
assert.ok(result.output === '1234\n' || result.output === '1234\nundefined\n'); // 'undefined' for v0.4
47+
assert.ok(result.stdout === '1234\n' || result.stdout === '1234\nundefined\n'); // 'undefined' for v0.4
4848

4949
// check if stderr goes to output
5050
var result = shell.exec('node -e \"console.error(1234);\"');
5151
assert.equal(shell.error(), null);
5252
assert.equal(result.code, 0);
53-
assert.ok(result.output === '' || result.output === 'undefined\n'); // 'undefined' for v0.4
53+
assert.ok(result.stdout === '' || result.stdout === 'undefined\n'); // 'undefined' for v0.4
5454
assert.ok(result.stderr === '1234\n' || result.stderr === '1234\nundefined\n'); // 'undefined' for v0.4
5555

5656
// check if stdout + stderr go to output
5757
var result = shell.exec('node -e \"console.error(1234); console.log(666);\"');
5858
assert.equal(shell.error(), null);
5959
assert.equal(result.code, 0);
60-
assert.ok(result.output === '666\n' || result.output === '666\nundefined\n'); // 'undefined' for v0.4
60+
assert.ok(result.stdout === '666\n' || result.stdout === '666\nundefined\n'); // 'undefined' for v0.4
6161
assert.ok(result.stderr === '1234\n' || result.stderr === '1234\nundefined\n'); // 'undefined' for v0.4
6262

6363
// check exit code
@@ -70,14 +70,14 @@ shell.cd('resources/external');
7070
var result = shell.exec('node node_script.js');
7171
assert.equal(shell.error(), null);
7272
assert.equal(result.code, 0);
73-
assert.equal(result.output, 'node_script_1234\n');
73+
assert.equal(result.stdout, 'node_script_1234\n');
7474
shell.cd('../..');
7575

7676
// check quotes escaping
7777
var result = shell.exec( util.format('node -e "console.log(%s);"', "\\\"\\'+\\'_\\'+\\'\\\"") );
7878
assert.equal(shell.error(), null);
7979
assert.equal(result.code, 0);
80-
assert.equal(result.output, "'+'_'+'\n");
80+
assert.equal(result.stdout, "'+'_'+'\n");
8181

8282
//
8383
// async
@@ -91,23 +91,26 @@ assert.ok('stdout' in c, 'async exec returns child process object');
9191
//
9292
// callback as 2nd argument
9393
//
94-
shell.exec('node -e \"console.log(5678);\"', function(code, output) {
94+
shell.exec('node -e \"console.log(5678);\"', function(code, stdout, stderr) {
9595
assert.equal(code, 0);
96-
assert.ok(output === '5678\n' || output === '5678\nundefined\n'); // 'undefined' for v0.4
96+
assert.ok(stdout === '5678\n' || stdout === '5678\nundefined\n'); // 'undefined' for v0.4
97+
assert.ok(stderr === '' || stderr === 'undefined\n'); // 'undefined' for v0.4
9798

9899
//
99100
// callback as 3rd argument
100101
//
101-
shell.exec('node -e \"console.log(5566);\"', {async:true}, function(code, output) {
102+
shell.exec('node -e \"console.log(5566);\"', {async:true}, function(code, stdout, stderr) {
102103
assert.equal(code, 0);
103-
assert.ok(output === '5566\n' || output === '5566\nundefined\n'); // 'undefined' for v0.4
104+
assert.ok(stdout === '5566\n' || stdout === '5566\nundefined\n'); // 'undefined' for v0.4
105+
assert.ok(stderr === '' || stderr === 'undefined\n'); // 'undefined' for v0.4
104106

105107
//
106108
// callback as 3rd argument (slient:true)
107109
//
108-
shell.exec('node -e \"console.log(5678);\"', {silent:true}, function(code, output) {
110+
shell.exec('node -e \"console.log(5678);\"', {silent:true}, function(code, stdout, stderr) {
109111
assert.equal(code, 0);
110-
assert.ok(output === '5678\n' || output === '5678\nundefined\n'); // 'undefined' for v0.4
112+
assert.ok(stdout === '5678\n' || stdout === '5678\nundefined\n'); // 'undefined' for v0.4
113+
assert.ok(stderr === '' || stderr === 'undefined\n'); // 'undefined' for v0.4
111114

112115
shell.exit(123);
113116

0 commit comments

Comments
 (0)