In trying to use child_process.spawn
to launch a series of script files, I was running into an issue getting the child processes to render their progress.
Normally, and as per the documentation for spawn, you can attach to the child process by adding event handlers to the stdout
and stderr
properties as such.
let spawn = require('child_process').spawn;
let child = spawn('babel-node', [ 'myscript.js' ]);
child.stdout.on('data', (d) => process.stdout.write(d));
child.stderr.on('data', (d) => process.stderr.write(d));
Unfortumately, that technique doesn't retain the isTTY
status in the child scripts that get executed. This can cause problems for other modules you may be using, such as progress.
Fortunately, the documentation for stdio
gives a hint that you can pass the current processes stdio options to child processes. In order to do this, you passin in the stdio
option in the spawn call and specified the values [0, 1, 2]
corresponding to stdio
, stdout
, and stderr
.
You end up with a call that looks like this:
let spawn = require('child_process').spawn;
let child = spawn(
'babel-node',
[ 'myscript.js' ],
{ stdio: [0, 1, 2] }
);