Skip to content

Commit 99b9b75

Browse files
committed
closes shelljs#31
1 parent 37ae50c commit 99b9b75

4 files changed

Lines changed: 32 additions & 63 deletions

File tree

README.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ Returns the current directory.
150150
Available options:
151151

152152
+ `-R`: recursive
153-
+ `-A`: all files (include files beginning with `.`)
153+
+ `-A`: all files (include files beginning with `.`, except for `.` and `..`)
154154

155155
Examples:
156156

@@ -352,7 +352,7 @@ Object containing environment variables (both getter and setter). Shortcut to pr
352352
### exec(command [, options] [, callback])
353353
Available options (all `false` by default):
354354

355-
+ `async`: Asynchronous execution. Needs callback.
355+
+ `async`: Asynchronous execution. Defaults to true if a callback is provided.
356356
+ `silent`: Do not echo program output to console.
357357

358358
Examples:
@@ -364,6 +364,11 @@ var child = exec('some_long_running_process', {async:true});
364364
child.stdout.on('data', function(data) {
365365
/* ... do something with data ... */
366366
});
367+
368+
exec('some_long_running_process', function(code, output) {
369+
console.log('Exit code:', code);
370+
console.log('Program output:', output);
371+
});
367372
```
368373

369374
Executes the given `command` _synchronously_, unless otherwise specified.

scripts/run-tests.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@ var failed = false;
66
cd(__dirname + '/../test');
77
ls('*.js').forEach(function(file) {
88
echo('Running test:', file);
9-
if (exec('node '+file).code !== 123) // 123 avoids false positives (e.g. premature exit)
9+
if (exec('node '+file).code !== 123) { // 123 avoids false positives (e.g. premature exit)
1010
failed = true;
11+
echo('*** FAILED! (missing return code)');
12+
}
1113
});
1214

1315
if (failed) {

shell.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -866,7 +866,7 @@ exports.env = process.env;
866866
//@ ### exec(command [, options] [, callback])
867867
//@ Available options (all `false` by default):
868868
//@
869-
//@ + `async`: Asynchronous execution. Needs callback.
869+
//@ + `async`: Asynchronous execution. Defaults to true if a callback is provided.
870870
//@ + `silent`: Do not echo program output to console.
871871
//@
872872
//@ Examples:
@@ -878,6 +878,11 @@ exports.env = process.env;
878878
//@ child.stdout.on('data', function(data) {
879879
//@ /* ... do something with data ... */
880880
//@ });
881+
//@
882+
//@ exec('some_long_running_process', function(code, output) {
883+
//@ console.log('Exit code:', code);
884+
//@ console.log('Program output:', output);
885+
//@ });
881886
//@ ```
882887
//@
883888
//@ Executes the given `command` _synchronously_, unless otherwise specified.
@@ -894,7 +899,7 @@ function _exec(command, options, callback) {
894899

895900
if (typeof options === 'function') {
896901
callback = options;
897-
options = {};
902+
options = { async: true };
898903
}
899904

900905
options = extend({

test/exec.js

Lines changed: 15 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -65,72 +65,29 @@ shell.cd('../..');
6565
// async
6666
//
6767

68-
// no callback (no need for asyncFlags)
68+
// no callback
6969
var c = shell.exec('node -e \"console.log(1234)\"', {async:true});
7070
assert.equal(shell.error(), null);
7171
assert.ok('stdout' in c, 'async exec returns child process object');
7272

73-
var asyncFlags = [];
74-
7573
//
7674
// callback as 2nd argument
7775
//
78-
asyncFlags[0] = false;
79-
shell.exec('node -e \"console.log(5678);\"', {async:true}, function(code, output) {
76+
shell.exec('node -e \"console.log(5678);\"', function(code, output) {
8077
assert.equal(code, 0);
8178
assert.ok(output === '5678\n' || output === '5678\nundefined\n'); // 'undefined' for v0.4
82-
asyncFlags[0] = true;
83-
84-
85-
// Most of the following code doesn't really belong here since it tests the sync version (stdout).
86-
// However there seems to be a race condition with the stdout returned by child.exec()
87-
// that makes the tests fail intermittently. So we're keeping them here in a chain
88-
// to avoid this race issue
89-
90-
// STILL SUFFERING INTERMITTENT FAILURES - COMMENTING OUT UNTIL THIS GETS SORTED OUT
91-
92-
shell.exit(123);
93-
94-
95-
// //
96-
// // check if stdout is proxied with default silent options (i.e. silent = false)
97-
// //
98-
// asyncFlags[1] = false;
99-
// shell.mkdir('-p', 'tmp');
100-
// var file = 'tmp/tempscript'+Math.random()+'.js',
101-
// script = 'require(\'../../global.js\'); exec(\'node -e \"console.log(555);\"\')';
102-
// script.to(file);
103-
// child.exec('node '+file, function(err, stdout, stderr) {
104-
// assert.ok(stdout === '555\n' || stdout === '555\nundefined\n'); // 'undefined' for v0.4
105-
// asyncFlags[1] = true;
106-
107-
// //
108-
// // check if stdout is proxied when: silent(true), {silent:false}
109-
// //
110-
// asyncFlags[2] = false;
111-
// shell.mkdir('-p', 'tmp');
112-
// var file = 'tmp/tempscript'+Math.random()+'.js',
113-
// script = 'require(\'../../global.js\'); silent(true); exec(\'node -e \"console.log(333);\"\', {silent:false})';
114-
// script.to(file);
115-
// child.exec('node '+file, function(err, stdout, stderr) {
116-
// assert.ok(stdout === '333\n' || stdout === '333\nundefined\n'); // 'undefined' for v0.4
117-
// asyncFlags[2] = true;
118-
119-
// //
120-
// // check if stdout is proxied when: silent(true), {silent:false} - async
121-
// //
122-
// asyncFlags[3] = false;
123-
// shell.mkdir('-p', 'tmp');
124-
// var file = 'tmp/tempscript'+Math.random()+'.js',
125-
// script = 'require(\'../../global.js\'); silent(true); exec(\'node -e \"console.log(222);\"\', {silent:false, async:true})';
126-
// script.to(file);
127-
// child.exec('node '+file, function(err, stdout, stderr) {
128-
// assert.ok(stdout === '222\n' || stdout === '222\nundefined\n'); // 'undefined' for v0.4
129-
// asyncFlags[3] = true;
130-
131-
// shell.exit(123);
132-
// });
133-
// });
134-
// });
79+
80+
//
81+
// callback as 3rd argument
82+
//
83+
shell.exec('node -e \"console.log(5566);\"', {async:true}, function(code, output) {
84+
assert.equal(code, 0);
85+
assert.ok(output === '5566\n' || output === '5566\nundefined\n'); // 'undefined' for v0.4
86+
87+
shell.exit(123);
88+
89+
});
90+
13591
});
92+
13693
assert.equal(shell.error(), null);

0 commit comments

Comments
 (0)