Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Execute child.stdout.on before child.on("exit") #224

Closed
orbatschow opened this issue Aug 22, 2015 · 6 comments
Closed

Execute child.stdout.on before child.on("exit") #224

orbatschow opened this issue Aug 22, 2015 · 6 comments
Labels
exec Issues specific to the shell.exec() API fix Bug/defect, or a fix for such a problem

Comments

@orbatschow
Copy link

var shell = require('shelljs');

var child = shell.exec('node --version', {silent:true, async:true});

child.stdout.on('data', function(data) {
    console.log(data);
});

child.on("exit", function(code) {
    console.log(code);
});

child.on("error", function (error) {
console.log(error);
});

The output of this command is :

0
v1.0.4

What do i have to do, to make shelljs return all data immediately and before on exit is executed ?

@summer4096
Copy link
Contributor

child.stdout.on('data', stuff);
child.stdout.on('end', otherStuff);

@orbatschow
Copy link
Author

Everything is now printed in the correct order, but end is always undefined:

child.stdout.on('end', function(end) {
    console.log(end);
});

@summer4096
Copy link
Contributor

var code;
child.on('exit', function(c) {
    code = c;
});
child.stdout.on('data', function(data) {
    console.log(data);
});
child.stdout.on('end', function() {
    console.log(code);
});

@kevin-smets
Copy link

@devtristan this now results in undefined. end is triggered before exit.

This gives me the correct output with exit code:

var child = shell.exec(cmd, {async: true, silent: true});
var output = "";

child.on('exit', function(c) {
    console.log(output);
    console.log('Exit code is ' + c);
});

child.stdout.on('data', function (data) {
    if (!!data)
        output += data;
});

@ariporad ariporad added the fix Bug/defect, or a fix for such a problem label Jan 9, 2016
@nfischer
Copy link
Member

nfischer commented Feb 9, 2016

@Synturas @kevin-smets is this resolved now?

@nfischer nfischer added the exec Issues specific to the shell.exec() API label Feb 9, 2016
@nfischer
Copy link
Member

nfischer commented Feb 7, 2018

Assuming this is fixed (we did a heavy refactor of exec())

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
exec Issues specific to the shell.exec() API fix Bug/defect, or a fix for such a problem
Projects
None yet
Development

No branches or pull requests

5 participants