gpt4 book ai didi

node.js - Nodejs process.stdin.resume() 在 Cygwin 中不起作用

转载 作者:太空宇宙 更新时间:2023-11-03 22:02:58 24 4
gpt4 key购买 nike

尝试制作 CLI 类型的程序 - 等待用户输入。在 Cygwin 中,脚本直接退出。就是脚本 process.stdin.resume() 中的这个

似乎可以在 Linux VM 上运行。也适用于 Windows 命令行。

我假设有关 Cygwin 的“终端”内容..

最佳答案

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

我找到了解决方法

版本

> 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

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

c:\test\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$ _

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

可移植性

如果有人想编写一个可移植脚本来调用需要标准输入的 Node 应用程序:

#!/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

关于node.js - Nodejs process.stdin.resume() 在 Cygwin 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12218542/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com