[英]How to get input in child process from custom node REPL
I am trying to create a node repl, and want to spawn a child process, which could take user input, then return control back to repl. 我正在尝试创建一个节点repl,并希望产生一个子进程,该进程可以接受用户输入,然后将控制权返回给repl。
... contents of main repl file ( a.js
) ...主repl文件(
a.js
)的内容
!function(){
var repl = require("repl"),
spawn = require('child_process').spawn;
function eval(cmd, context, filename, callback) {
var child = spawn('node', ['./b.js']);
child.stdout.pipe(process.stdout);
child.stdin.pipe(process.stdin);
child.on('close', function (code) {
console.log('closing')
callback(code, 'closing callback');
});
}
repl.start({
prompt: 'repl> ',
input: process.stdin,
output: process.stdout,
eval: eval
}).on('exit', function () {
process.exit();
});
}();
... contents of script called in child process ( b.js
) ...在子进程(
b.js
)中调用的脚本的内容
!function(){
promptly = require('promptly');
promptly.prompt('some question: ', function (err, answer) {
console.log(answer);
process.exit();
});
}();
When I run node b
all is as expected... 当我运行
node b
一切都如预期...
$ node b
some question: something
something
$
But called from repl, it gets into loop, where it keeps asking question, never returns to repl... 但是从repl调用后,它进入循环,不断询问问题,再也没有返回repl ...
$ node a
repl> anything
some question: my answer
some question: another answer
some question: etc...
It seems that the stdin is not being captured in the child process, and it is still being captured in repl. 似乎stdin并未在子进程中被捕获,并且仍在repl中被捕获。
How do I pass control to child process, until it is complete, then pass back to parent? 如何将控制权传递给子进程,直到完成,然后再传递回父进程?
nb I am open to using any other means of creating child process. nb我愿意使用任何其他方式来创建子进程。
A few changes to a.js
should make this work. 对
a.js
进行一些更改即可a.js
这项工作。 You need to pipe stdin
input from the main repl process to the stdin
of the child process using process.stdin.pipe(child.stdin);
您需要使用
process.stdin.pipe(child.stdin);
repl进程的stdin
输入通过管道传递到子进程的stdin
process.stdin.pipe(child.stdin);
To get the data back from the child process you need to pipe child.stdout
to process.stdin
using child.stdout.pipe(process.stdout);
为了获取数据需要从你到管子进程回到
child.stdout
到process.stdin
使用child.stdout.pipe(process.stdout);
Perhaps there is a better way of tracking child processes. 也许有更好的跟踪子进程的方法。 But I added a flag to mark if a child is spawned or not, and reset it when the child process closes.
但是我添加了一个标记来标记是否生成了一个子代,并在子代进程关闭时将其重置。
Lastly, when the child process ends, resume the main processes stdin
with process.stdin.resume();
最后,当子进程结束时,使用
process.stdin.resume();
恢复主进程stdin
process.stdin.resume();
, otherwise the repl will stop on the next input. ,否则repl将在下一个输入时停止。
!function(){
var repl = require("repl"),
spawn = require('child_process').spawn;
var child_spawned = false;
function eval(cmd, context, filename, callback) {
// don't start a new child process if one is already running
if(child_spawned) return;
var child = spawn('node', ['./b.js']);
child_spawned = true;
// pipe child output back up to parent process
child.stdout.pipe(process.stdout);
// pipe the main process input to the child process
process.stdin.pipe(child.stdin);
child.on('close', function (code) {
console.log('closing')
callback(code, 'closing callback');
// mark child process as closed
child_spawned = false;
// resume the main process stdin after child ends so the repl continues to run
process.stdin.resume();
});
}
repl.start({
prompt: 'repl> ',
input: process.stdin,
output: process.stdout,
eval: eval
}).on('exit', function () {
process.exit();
});
}();
I solved this by using a combination of spawnSync
(to simplify flow) and { stdio: 'inherit' }
as passed option... 我通过结合使用
spawnSync
(以简化流程)和{ stdio: 'inherit' }
作为传递选项来解决此问题...
!function(){
var repl = require("repl"),
spawnSync = require('child_process').spawnSync;
function eval(cmd, context, filename, callback) {
spawnSync('node', ['./b.js'], { stdio: 'inherit' });
callback();
}
repl.start({
prompt: 'repl> ',
input: process.stdin,
output: process.stdout,
eval: eval
}).on('exit', function () {
process.exit();
});
}();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.