NodeJS子进程:写入stdin任意时间

时间:2017-06-03 00:27:55

标签: node.js read-eval-print-loop child-process

我正在尝试制作和c REPL,所以我需要与stdin交互多少次c代码需要。目前有一个这个代码(只有一次)

const spawn = require('child_process').spawn;
const gcc = spawn('gcc', ['-Wall', 'main.c', '-o', 'main', '-lm']);

gcc.stdout.on('data', (data) => {
  console.log(`stdout: ${data}`);
});

gcc.stderr.on('data', (data) => {
  console.log(`stderr: ${data}`);
});

gcc.on('close', (code) => {
  console.log(`child process exited with code ${code}`);
  const child = spawn('./main')
  child.stdin.setEncoding('utf-8');
  child.stdout.pipe(process.stdout)
  child.stdin.write("5\n");
  child.stdin.end();
});

任何线索?

0 个答案:

没有答案