Reputation:
I am spawning a child process in node where for the opts I'm using {stdio:'inherit'}
, I need to do this because the child process needs to accept some input from the user.
I need to also get this output though, because the output of the child is actually nothing because it is using the parent's. I thought about doing a quick process.stdout.on('data', ...)
attach and detach after the child process, but I prefer not to do that. I assume that is some node stream solution? Like something that would get intercept the input first, and then pass it along to my parent process?
TLDR Need output of child process while still doing stdio:'inherit'
in node. Perhaps something that pipes output of child process to a Buffer/string?
Thank you and please if answering provide working code example
Upvotes: 3
Views: 1818
Reputation:
Figured it out!
const child_process = require('child_process');
const child = child_process.spawn('python3', ['./eater.py'], {
stdio: ['inherit', 'pipe', 'pipe'],
});
const output = [];
child.stdout.on('data', d => {
console.log(d.toString());
output.push(d.toString());
});
child.stdout.on('end', () => {
console.log('Finished');
console.log({ output });
});
and
if the python was something like:
print("Some prompt")
auth_code = input('Provide some code to us\n')
Upvotes: 1