I'm not quite sure why this is happening, however I have the following which I wrote:
const runScript = (cmd, args) => {
return new Promise((res, rej) => {
// spawn a new process
const ls = spawn(cmd, args);
ls.stdout.on("data", (data) => {process.stdout.write(data)});
ls.stderr.on("data", (data) => {process.stderr.write(data)});
ls.on("close", code => {
console.log(`Command ${cmd} ${args} exited with code ${code}`);
code === 0 ? res(code) : rej(code);
});
ls.on("error", code => {
rej(code);
});
});
};
This works fine. But I figured I could change the .on("data")
event handlers to just pass in the write function directly. So I switched from
ls.stdout.on("data", (data) => {process.stdout.write(data)});
To
ls.stdout.on("data", process.stdout.write);
Depending on the command, I either get no output with the refactored version (passing .write
directly) or an EPIPE. I thought they were the exact same. What's happening exactly here? I'm suspecting it has something to do with either the buffering or what process
is referring to.