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

Add all property to returned object #5

Closed
wants to merge 6 commits into from
Closed

Add all property to returned object #5

wants to merge 6 commits into from

Conversation

sotojuan
Copy link

@sotojuan sotojuan commented Jan 5, 2016

This fixes #1 by returning an additional all property that is just the stderr and stdout together, updating the readme, and adding two tests that test this feature.

@sindresorhus
Copy link
Owner

Ok, I should have been more clear in the original issue. It's not that simple. The user could already do this themselves. What I'm after is the combination of stdout and stderr as it's outputted, just like what you would see in your terminal.

process.stdout.write('1');
process.stderr.write('2');
process.stdout.write('3');

Would result in 132 your way. I want 123.

Will probably have to spawn and gather it manually.

@sotojuan
Copy link
Author

sotojuan commented Jan 5, 2016

I was wondering why you said you didn't have enough time to do this! I'll look into it. Thanks for clearing it up.

@sotojuan
Copy link
Author

sotojuan commented Jan 5, 2016

All right, so I use spawn and then listen on data events in stdout and stderr and add each to a string for stdout, stderr, and all respectively. I got it, but I am trying to think of something I can use to test it. For some reason if I do echo good; ls x; echo good for example (assuming x does not exist), the two echos get put together, with the error from ls put last.

However, if I do echo good; ls x; ls . then it works, with the error from ls x in the middle and the result from ls . put last.

I am not sure if this is an error in my implementation, but let me know!

@sindresorhus
Copy link
Owner

but I am trying to think of something I can use to test it

Just create a fixture.js file that contains my above code example.

@sotojuan
Copy link
Author

sotojuan commented Jan 6, 2016

All right, so this passes all the tests and seems to work, but (IMO) probably could be refactored/cleaned up.

spawn does not seem to have an option for encoding, so all the outputs are saved as strings and if the user set an encoding (anything not undefined) the string is converted into a buffer with that encoding. I did this in the handle function. I tried using buffers instead of strings but it got messy/wasn't working.

A lot of stuff I did was new to me (I learned stuff at least!), so let me know what you think.

@sindresorhus
Copy link
Owner

spawn does not seem to have an option for encoding

You use e.g. stdout.setEncoding() on the streams instead.

var all = '';

spawned.stdout.on('data', function (data) {
out += data.toString();
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using .toString() is a common mistake in Node.js, but this will totally break on unicode where one character might be split between different data chunks. Instead use .setEncoding() upfront.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aren't you supposed to use string-decoder for that? Or is that what setEncoding uses under the hood?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jamestalmage You could. I think it uses it under the hood, but .setEncoding() is the correct way regardless.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup. I blame the Node.js docs. They could be a lot clearer. Especially since it's easy to do it wrong and would cause subtle bugs.

@sotojuan
Copy link
Author

Hey Sindre, so my old all branch of my fork somehow disappeared/got messed up, so uh I just started with a fresh one based on latest master and forced push. Probably a bad thing to do but the reason I am commenting is because apparently for some reason the all output works. It works on the tests, tried it with external files and the fixture, and even shell commands. Not sure why since it's the same code.

@sindresorhus
Copy link
Owner

Force push is fine, and often useful, on non-master branches. Your changes does not yet address all the previous inline feedback.

@sotojuan
Copy link
Author

Sure, was just explaining the force push! I'll get to work on the rest of the feedback. Thanks!

@sotojuan
Copy link
Author

All right, I took care of some of the smaller things. The all works when running a file but not when doing a shell command (echo at least, passes with others). Still investigating that.

Also, can you clarify on what I should do about this?

@sindresorhus sindresorhus mentioned this pull request Apr 26, 2016
4 tasks
@jamestalmage
Copy link
Contributor

@sotojuan
Copy link
Author

sotojuan commented May 2, 2016

Ha, I had forgotten about this PR. Yeah there's probably a better way to do this now. Might be worth to just close this one and I (or someone else?) can work with the new proposed solutions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Return an .all property with both stdout and stderr intermixed
3 participants