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

Ability to prefix child process stdio lines with a custom string #121

Closed
callumlocke opened this issue Jan 15, 2018 · 6 comments · Fixed by #693 or #699
Closed

Ability to prefix child process stdio lines with a custom string #121

callumlocke opened this issue Jan 15, 2018 · 6 comments · Fixed by #693 or #699

Comments

@callumlocke
Copy link

callumlocke commented Jan 15, 2018

Proposal: add a new option prefix (optional, string). If given, the child's stdout and stderr streams would be transformed so each line is prefixed with the given string.

Use case: when running several child processes in parallel and using inherit to pipe each child's stdio to the parent, prefixing would make it easier to see what's going on:

Promise.all(jobs.map((job, i) => execa(job.cmd, job.args, {
  stdio: 'inherit',
  prefix: `[job ${i + 1} of jobs.length] `,
}))).then(() => {
  // ...
});
[job 1 of 4] blah blah
[job 3 of 4] blah blah
[job 3 of 4] blah blah blah
[job 2 of 4] blah
...

I think the best API would be to have a prefix option which would apply to both stdout and stderr streams, and also prefixStdout and prefixStderr in case you want to override them individually.

Happy to implement this if interested.

@sindresorhus
Copy link
Owner

sindresorhus commented Jan 15, 2018

It's not possible to intercept the stdout/stderr when you're using inherit (Really wish it was though). We could support it on pipe, but not sure it belongs here.

@callumlocke
Copy link
Author

callumlocke commented Jan 15, 2018

Can't we just reimplement inherit so it doesn't actually use the underlying cp's inherit? AFAICT inherit is just shorthand for [process.stdin, process.stdout, process.stderr].

@sindresorhus
Copy link
Owner

Relevant Node.js issue: nodejs/node#8033

@ehmicky
Copy link
Collaborator

ehmicky commented Jan 17, 2024

Re-opening because this feature still depends on #695.

@ehmicky
Copy link
Collaborator

ehmicky commented Jan 17, 2024

PR at #699 implementing this.

The initial message's example would then written as:

const prefixLine = function * (line) {
  yield `[job ${i + 1} of jobs.length] ${line}`
}

await Promise.all(jobs.map((job, i) => execa(
  job.cmd, 
  job.args, 
  { stdout: ['inherit', prefixLine] },
)))

@ehmicky
Copy link
Collaborator

ehmicky commented May 8, 2024

This feature has been just released in Execa 9.0.0. Please see (and share!) the release post and the changelog.

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