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 pipeStdin() method #534

Closed
ehmicky opened this issue Mar 6, 2023 · 8 comments · Fixed by #536
Closed

Add pipeStdin() method #534

ehmicky opened this issue Mar 6, 2023 · 8 comments · Fixed by #536

Comments

@ehmicky
Copy link
Collaborator

ehmicky commented Mar 6, 2023

#531 added pipeStdout(), pipeStderr() and pipeAll() to redirect a child process's stdout/stderr to another process, a stream or a file.

For completeness and convenience, we should add a similar pipeStdin() which would redirect another process stdout, stream or file to a child process stdin.

This pattern is currently already documented in our Tips section.

const subprocess = execa('cat')
fs.createReadStream('stdin.txt').pipe(subprocess.stdin)
await subprocess

This would simplify it to:

await execa('cat').pipeStdin('stdin.txt')
@sindresorhus
Copy link
Owner

await execa('cat').pipeStdin('stdin.txt') reads to me as we are piping cat into stdin.txt, but we are doing the reverse.

Maybe:

await execa('cat').pipeToStdinFrom('stdin.txt')

@ehmicky
Copy link
Collaborator Author

ehmicky commented Mar 6, 2023

Yes, I was also thinking users might not realize things are done from a reverse direction than pipeStdout() and the likes. pipeToStdinFrom() sounds good 👍

I'm going to take up this PR right now. 🧑‍🏭

@ehmicky
Copy link
Collaborator Author

ehmicky commented Mar 6, 2023

I implemented it in #535. Doing so gave me some additional insights in terms of overall experience as I wrote tests and documentation for it. And now, it feels to me like this method is not as useful as pipeStdout()/pipeStderr()/pipeAll():

  • Users might be confused about the direction being reversed from pipeStdout()
  • Unlike pipeStdout(), the flow of data is different from the flow of the code. It makes more sense for processes that are later in the pipe order to come last in the code itself. The current pattern $({ input: process.stdin })`cat` follows that order, but not $`cat`.pipeToStdinFrom(process.stdin)
  • The input option already fills this feature for streams. It also supports raw data. When it comes to processes, pipeStdout() can be used, after flipping the order of the processes in the code.

So pipeToStdinFrom() would only facilitate reading from a file, by specifying 'input.txt' instead of fs.createReadStream('input.txt').

Based on this, I do not think it is worth the additional complexity introduced to the API.

On the other hand, we could improve the documentation to highlight the above patterns, so I opened #536 for this.

@sindresorhus
Copy link
Owner

I agree with your assessment.

@sindresorhus
Copy link
Owner

Do you think a `$({inputFromFile: 'input.txt'})`cat` option would be useful as a shortcut? Piping from a file is quite a common use-case.

@ehmicky
Copy link
Collaborator Author

ehmicky commented Mar 7, 2023

Yes, I agree it would.

Naming-wise, would inputFile be good too? I am wondering whether it might shorter yet clear enough. 🤔

@sindresorhus
Copy link
Owner

inputFile is good 👍

@ehmicky
Copy link
Collaborator Author

ehmicky commented Mar 7, 2023

Done in #542.

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