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

caching output? #483

Closed
loynoir opened this issue Dec 8, 2021 · 4 comments
Closed

caching output? #483

loynoir opened this issue Dec 8, 2021 · 4 comments
Labels

Comments

@loynoir
Copy link

loynoir commented Dec 8, 2021

If there's a time consuming command,

  • want real time output to console
  • want capture final output

Something similar to shell

ret=$(bash -c 'echo 1; sleep 2; echo 2'|tee /dev/stderr)

Currently I use

    p.stdout?.pipe(process.stdout)
    p.stderr?.pipe(process.stderr)
    await p.then(p => ret = p)
@ehmicky
Copy link
Collaborator

ehmicky commented Dec 8, 2021

Hi @loynoir,

Yes, this is the recommended way. The following documentation section describes it.
Please note the last line of your example can be reduced to const ret = await p.

@loynoir
Copy link
Author

loynoir commented Dec 8, 2021

Oh, I know why, I tried it first in REPL, and use a small gap.

Seems execa is not caching output.

> p=execa('bash',['-c','echo a; sleep 2; echo b']);void p.stdout.pipe(process.stdout)
... 3 seconds later ...
> ret=await p
> ret.stdout
''

@loynoir loynoir changed the title What is the recommendable way to tee real time output? caching output? Dec 8, 2021
@ehmicky
Copy link
Collaborator

ehmicky commented Dec 8, 2021

Execa is caching/buffering the output, but only after the promise returned by execa() has been followed (which you do with await in your code above).

The example in the documentation is not meant for the REPL. In the REPL, to avoid the small delay you're mentioning, you need to enter all statements at once:

> const childProcess = execa('bash', ['-c', 'echo a; sleep 2; echo b']); childProcess.stdout.pipe(process.stdout); const { stdout } = await childProcess; 
> stdout;
'a\nb'

@loynoir
Copy link
Author

loynoir commented Dec 8, 2021

Thanks for your answer 😃

@loynoir loynoir closed this as completed Dec 8, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants