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?
The node:child_process module provides the ability to spawn subprocesses in a manner that is similar, but not identical, to popen(3) . This capability is primarily provided by the child_process. spawn() function: const { spawn } = require('node:child_process'); const ls = spawn('ls', ['-lh', '/usr']); ls. stdout.
The stdin property of the process object is a Readable Stream. It uses on() function to listen for the event.
stdin (0): The standard input stream, which is a source of input for the program. process. stdout (1): The standard output stream, which is a source of output from the program. process. stderr (2): The standard error stream, which is used for error messages and diagnostics issued by the program.
Like spawn()
, execFile()
also returns a ChildProcess
instance which has a stdin
writable stream.
As an alternative to using write()
and listening for the data
event, you could create a readable stream, push()
your input data, and then pipe()
it to child.stdin
:
var execFile = require('child_process').execFile;
var stream = require('stream');
var optipng = require('pandoc-bin').path;
var child = execFile(optipng, ['--from=markdown', '--to=html'], function (err, stdout, stderr) {
console.log(err);
console.log(stdout);
console.log(stderr);
});
var input = '# HELLO';
var stdinStream = new stream.Readable();
stdinStream.push(input); // Add data to the internal queue for users of the stream to consume
stdinStream.push(null); // Signals the end of the stream (EOF)
stdinStream.pipe(child.stdin);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With