在node.js中检测控制台输出结束 [英] Detect end of console output in node.js

查看:45
本文介绍了在node.js中检测控制台输出结束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

var cp = require("child_process");

var proc = cp.spawn("cmd");
proc.stdout.on("data", data => console.log("Data:", data.toString()));
proc.stdin.write("help\n");
proc.stdin.write("help\n");

在上面的代码片段中,您如何检测流何时完成对特定命令的写入(即,如果在终端中执行了这些命令,它将显示一个闪烁的光标,您可以在其中输入文本)?

In the above code snippet, how would you detect when the stream has finished writing for a specific command (i.e. when, if these commands were executed in the terminal, it would show a blinking cursor that you could enter text into)?

我尝试过使用监听事件 end 的方法,但这似乎仅在过程完成时才会触发.

I have tried using listening to the event end, but this seems only to be fired when the process finishes.

推荐答案

除非您正在执行的程序具有定义明确的输出格式(例如,以换行符分隔的JSON,XML等),否则没有确定的方法可以知道.).无论哪种方式,您都必须对程序输出执行某种形式的解析(和可能的缓冲).

There is no definitive way to know, unless the program you're executing has a well-defined output format (e.g. newline-delimited JSON, XML, etc.). Either way, you will have to perform some kind of parsing (and possible buffering) of the program output.

这篇关于在node.js中检测控制台输出结束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆