NodeJs process.stdin.resume()在Cygwin中不起作用

试图制作一个CLItypes的程序 – 等待用户input。 在Cygwin中,脚本刚刚退出。 只要在脚本process.stdin.resume()

似乎在Linux VM上工作。 在Windows命令行上也可以工作。

我假设'terminal'有关Cygwin的东西..

症状是Cygwin process.stdin.on('data', cb);process.stdin.on('end', cb); 不会被调用。

我find了一个解决方法

版本

 > ver Microsoft Windows [Version 6.1.7601] $ uname -a CYGWIN_NT-6.1 localhost 1.7.28(0.271/5/3) 2014-02-04 16:01 x86_64 Cygwin $ node --version v0.8.14 

也许它适用于较新的节点,我不知道,我需要使用这个版本。

C:\testing\ myecho.js

 console.error("Starting up"); process.stdin.resume(); process.stdin.setEncoding('utf8'); console.error("Waiting for data"); var inputContents = ""; process.stdin.on('data', function (chunk) { process.stderr.write("."); inputContents += chunk.toString(); }); process.stdin.on('end', function () { console.error("\nGot the full thing :)"); console.log(inputContents); }); 

让我们在Windows上尝试

 c:\test>echo hello | node myecho Starting up Waiting for data . Got hello c:\test>_ 

我们来试试Cygwin

 /cygdrive/c/test$ echo hello | node myecho Starting up Waiting for data /cygdrive/c/test$ _ 

让我们再试一次“在Cygwin”

 /cygdrive/c/test$ cmd /c 'echo hello | node myecho' Starting up Waiting for data . Got the full thing :) hello /cygdrive/c/test$ _ 

好吧,现在看起来很花哨!

 /cygdrive/c/test$ cmd /c 'echo hello | node myecho 2>NUL' hello /cygdrive/c/test$ cmd /c 'echo hello | node myecho' 2>/dev/null hello /cygdrive/c/test$ _ 

太糟糕了,input不能来自Cygwin文件描述符,但作为一种解决方法,可以将其写入文件,然后从那里用type来读取它。

可移植性

如果有人想写一个可移植的脚本调用期待标准input的节点应用程序:

 #!/bin/bash SCRIPT_DIR='../path/to/scripts' # absolute paths would need a little more playing if [[ `uname -s` == CYGWIN* ]]; then cmd /c "type file1 | node ${SCRIPT_DIR//\//\\}\program.js ${BASHVAR}" | grep stuff >file2 else cat file1 | node ${SCRIPT_DIR}/program.js ${BASHVAR} | grep stuff >file2 fi