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 pipeStdout()-related methods #531

Merged
merged 9 commits into from Mar 6, 2023
Merged

Add pipeStdout()-related methods #531

merged 9 commits into from Mar 6, 2023

Conversation

ehmicky
Copy link
Collaborator

@ehmicky ehmicky commented Mar 5, 2023

Fixes #525.

This adds the following shortcut methods: pipeStdout(), pipeStderr() and pipeAll().

For Node.js shell-like scripts, this is the equivalent to the |, |&, >, &>, or 2>&1 shell characters.

Pipe to stream, before:

const subprocess = $`echo foo`;
subprocess.stdout.pipe(process.stdout);
const {stdout} = await subprocess;

After:

const {stdout} = await $`echo foo`.pipeStdout(process.stdout);

Pipe to file, before:

const subprocess = $`echo foo`;
subprocess.stdout.pipe(fs.createWriteStream('stdout.txt'))
const {stdout} = await subprocess;

After:

const {stdout} = await $`echo foo`.pipeStdout('stdout.txt');

Pipe to another process, before:

const ffmpegProcess = $`ffmpeg -ss 5 -i input.mp4 -t 1 -vf cropdetect -f null -`
const awkProcess = $`awk ${'/crop/ {print $NF}'}`
const tailProcess = $`tail -1`

ffmpegProcess.stderr.pipe(awkProcess.stdin)
awkProcess.stdout.pipe(tailProcess.stdin)

const { stdout } = await tailProcess

After:

const {stdout} = await $`ffmpeg -ss 5 -i input.mp4 -t 1 -vf cropdetect -f null -`
  .pipeStderr($`awk ${'/crop/ {print $NF}'}`)
  .pipeStdout($`tail -1`)

The PR's code and tests are done, but I haven't added the types, type tests nor readme documentation. I'd like to first confirm the overall idea and design make sense to you @sindresorhus. :)

index.js Show resolved Hide resolved
lib/pipe.js Outdated Show resolved Hide resolved
lib/pipe.js Outdated Show resolved Hide resolved
lib/pipe.js Outdated Show resolved Hide resolved
@sindresorhus
Copy link
Owner

This is very nice API sugar 👍

@ehmicky ehmicky force-pushed the pipe branch 2 times, most recently from ec6de2d to 90e9a33 Compare March 6, 2023 16:27
The `stdout` option] must be kept as `pipe`, its default value.
*/
pipeStdout?<Target extends ExecaChildPromise<StdoutStderrAll>>(target: Target): Target;
pipeStdout?(target: WritableStream | string): ExecaChildProcess<StdoutStderrType>;
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Those methods must be marked as optional since they depend on the value of the stdout, stderr, stdio, all and encoding options.

In the future, we might want to improve the types to infer the value of those options so they can be used to determine those methods' optionality. However, this would complicate the types quite a bit, so I'm leaving it off for this PR.

@ehmicky
Copy link
Collaborator Author

ehmicky commented Mar 6, 2023

I have added types and documentation. The PR is ready to review 👍

@ehmicky ehmicky mentioned this pull request Mar 6, 2023
@ehmicky ehmicky merged commit 06287c8 into main Mar 6, 2023
20 checks passed
@ehmicky ehmicky deleted the pipe branch March 6, 2023 19:58
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.

Piping shortcut notation
2 participants