I'm using a library that wraps pandoc
for node. But I can't figure out how to pass STDIN to the child process `execFile...
var execFile = require('child_process').execFile;
var optipng = require('pandoc-bin').path;
// STDIN SHOULD GO HERE!
execFile(optipng, ['--from=markdown', '--to=html'], function (err, stdout, stderr) {
console.log(err);
console.log(stdout);
console.log(stderr);
});
On the CLI it would look like this:
echo "# Hello World" | pandoc -f markdown -t html
UPDATE 1
Trying to get it working with spawn
:
var cp = require('child_process');
var optipng = require('pandoc-bin').path;
var child = cp.spawn(optipng, ['--from=markdown', '--to=html'], { stdio: [ 0, 'pipe', 'pipe' ] });
child.stdin.write('# HELLO');
// then what?
I'm not sure its possible to use
STDIN
withchild_process.execFile()
based on these docs and the below excerpt, looks like its available only tochild_process.spawn()
String as STDIN
If you are using synchronous methods (
execFileSync
,execSync
, orspawnSync
) you can pass a string as stdin using theinput
key in options. Like this:Like
spawn()
,execFile()
also returns aChildProcess
instance which has astdin
writable stream.As an alternative to using
write()
and listening for thedata
event, you could create a readable stream,push()
your input data, and thenpipe()
it tochild.stdin
:Here's how I got it to work: