我正在使用包装pandoc节点的库。但是我不知道如何将STDIN传递给子进程execFile …
pandoc
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); });
在CLI上,它看起来像这样:
echo "# Hello World" | pandoc -f markdown -t html
更新1
尝试使其与spawn:
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?
这是我如何使其工作的方法:
var cp = require('child_process'); var optipng = require('pandoc-bin').path; //This is a path to a command var child = cp.spawn(optipng, ['--from=markdown', '--to=html']); //the array is the arguments child.stdin.write('# HELLO'); //my command takes a markdown string... child.stdout.on('data', function (data) { console.log('stdout: ' + data); }); child.stdin.end();