Monday, 12 June 2017

How to simulate bash process substitution with node.js?

Assuming that I have 1.js:

process.stdout.write("Hello world.\n")

And 2.js:

process.stdout.write(
    require("fs")
    .readFileSync(process.argv[2])
    .toString()
    .replace(/Hello/,"Goodbye")
)

and can execute the following command within bash terminal to get 2.js output:

$ node 2.js <(node 1.js)
Goodbye world.

how can I simulate the same mechanism of process substitution without bash mediation? What should I write in 3.js?

var child_process = require("child_process")
var cp1 = child_process.spawn("node",[__dirname + "/1.js"])
var dev_fd_pipe = ??? // How do I create it?
var cp2 = child_process.spawn("node",[__dirname + "/2.js",dev_fd_pipe])
cp2.stdout.pipe(process.stdout)



via user619271

No comments:

Post a Comment