Tag: stdin

使用按键在node.js中启动操作

我正在使用node.js v4.5 我写了下面的函数来延迟发送重复的消息。 function send_messages() { Promise.resolve() .then(() => send_msg() ) .then(() => Delay(1000) ) .then(() => send_msg() ) .then(() => Delay(1000)) .then(() => send_msg() ) ; } function Delay(duration) { return new Promise((resolve) => { setTimeout(() => resolve(), duration); }); } 而不是延迟,我想激活使用按键发送消息。 类似下面的function。 function send_messages_keystroke() { Promise.resolve() .then(() => send_msg() ) .then(() => keyPress('ctrl-b') […]

写入产生的进程stdin nodejs?

我有一个脚本,我想从另一个运行。 问题在于子脚本(进程)在继续之前需要用户input。 var child = spawn('script'); child.stdin.setEncoding('utf8'); child.stdout.on('data', function (data) { console.log(data.toString().trim()); // tells me to input my data child.stdin.write('my data\n'); }); input我的数据后,子脚本应该继续,而是挂在那里。 解 其实上面的代码适合我。 我在子脚本中使用commander.js来提示用户采取行动。 以下是我如何回应小孩的脚本提示: child.stdout.on('data', function (data) { switch (data.toString().trim()) { case 'Username:': child.stdin.write('user'); break; case 'Password:': child.stdin.write('pass'); break; } }); 同样的事情与假设 : var suppose = require('suppose'); suppose('script') .on('Username: ').respond('user') .on('Password: ').respond('pass') […]

如何读取node.js中的整个文本stream?

在RingoJS中有一个叫做read的函数 ,它允许你读完整个stream,直到到达结尾。 在制作命令行应用程序时,这非常有用。 例如,你可以写一个tac 程序如下: #!/usr/bin/env ringo var string = system.stdin.read(); // read the entire input stream var lines = string.split("\n"); // split the lines lines.reverse(); // reverse the lines var reversed = lines.join("\n"); // join the reversed lines system.stdout.write(reversed); // write the reversed lines 这可以让你启动一个shell并运行tac命令。 然后input你想要的行数,完成后你可以按下Ctrl + D (或Windows上的Ctrl + Z )来表示传输结束 。 我想在node.js做同样的事情,但我找不到任何function。 […]

node.js:如何检测一个空的标准inputstream?

我有一个node.js脚本/服务器,读取stdin启动时的一些input。 但是,有时候没有数据要传入。这很麻烦,因为在这种情况下, data和事件都不会被调用。 如何检测node.js代码中的情况? 我想避免在input结尾附加特殊的“结束”字符,以免给客户带来不便。 相关的代码如下: var newHTML = ''; var gfm = spawn(__dirname + '/node_modules/docter/bin/github-flavored-markdown.rb'); process.stdin.on('data', function(chunk){ gfm.stdin.write(chunk); }); process.stdin.on('end', function(){ gfm.stdin.end(); }); gfm.stdout.on('data', function(data) { newHTML += data; }); gfm.on('exit',function(ecode){ socket.emit('newContent', newHTML); }); process.stdin.resume();

Nodejssubprocess:从已经初始化的进程写入stdin

我正尝试使用节点的child_process产生一个外部进程phantomjs ,然后在初始化之后将信息发送到该进程,这可能吗? 我有以下代码: var spawn = require('child_process').spawn, child = spawn('phantomjs'); child.stdin.setEncoding = 'utf-8'; child.stdout.pipe(process.stdout); child.stdin.write("console.log('Hello from PhantomJS')"); 但是我在stdout上唯一得到的是phantomjs控制台的初始提示。 phantomjs> 所以似乎child.stdin.write没有任何效果。 我不知道我可以发送额外的信息phantomjs最初的产卵。 提前致谢。