在节点子进程中获取标准输入

问题描述

是否可以在使用永久监视器生成的子进程中获取输入? 我想实现类似 Inquirer.js用户能够使用简单的命令(例如 --stop--change-message "new message" ecc.

)执行某些操作

这是运行cli应用程序的索引文件代码

index.js

commander.version('1.3.0')
.option('-m,--message <text>','set custom bot message')
.action( (options) => {
    const child = new forever.Monitor(script,{
        max: 2,silent: false,args: [options.message]
    });

    child.start();

    child.on('start',(process) => {
        console.log(chalk.magenta.bold(header));
    });

    child.on('restart',() => {
        console.log(`Forever restarting script for ${child.times} time`);
    });

    child.on('exit:code',(code) => {
        console.log(`Forever detected script exited with code ${code}`);
    });

}).parse();

我想要实现的是将第二个脚本作为子进程运行,并使用用户传递的选项,当它运行时,如果用户在终端窗口中输入某些内容,我想获取用户输入到子进程中的内容执行相关动作的脚本

我也在考虑用 require('child').spawn 替换永久监控库并使用它。我不确定,但如果我理解 child.spawn 函数,它将运行脚本,直到主终端窗口关闭,并且永远监视器的行为相同。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)