我正在尝试将Enter
密钥发送到 Node.js 中的 child_process。\r\n
, \n
, 实际上并没有将Enter
密钥发送到 Psy Shell(PHP repl)。
const execa = require("execa");
const { Readable } = require("stream");
const subprocess = execa("php", ["artisan", "tinker"], { cwd: "C:\\laravel-app" });
const str = Readable.from("5+5;\r\n2+2;"); // <-- Here
str.pipe(subprocess.stdin);
(async () => {
const a = await readableToString(subprocess.stdout);
const b = await readableToString(subprocess.stderr);
subprocess.kill();
console.log(a);
console.log(b);
})();
// This function turns a stream to a readable string
async function readableToString(readable) {
let result = "";
for await (const chunk of readable) {
result += chunk;
}
return result;
}
输出应该是
10
4
相反,我得到
4
这与我输入5+5;2+2
PsySh(手动在终端中)时得到的结果相同。