I am building an application with Ink react on a terminal. I'd like to have a dedicated and seperate terminal to output any logs I want from the main application. In order to do that, I went to try the "ipc"
stdio option from the spawn
function:
我正在用终端上的Ink Reaction构建一个应用程序。我想有一个专用的和独立的终端输出任何我想要的主应用程序的日志。为此,我尝试了从spawn函数中选择“ipc”stdio选项:
import { spawn } from "child_process";
import type { ChildProcess } from "child_process";
import { fileURLToPath } from "url";
import path from "path";
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
export function useExternalDebugTerminal(): ChildProcess {
const terminal = spawn("x-terminal-emulator", ["-e", `yarn node ${path.resolve(__dirname, "childTerm.js")}`], {
stdio: [0, 1, 2, "ipc"],
detached: true
});
return terminal;
}
const term = useExternalDebugTerminal();
term.send("Hello, child process!\n");
And here is the childTerm.js code:
以下是Child Term.js代码:
import process from "process";
process.on("message", (msg) => {
console.log("Received message in child:", msg);
});
The result is a terminal that briefly spawns without outputting anything.
结果是一个短暂地产生而不输出任何内容的终端。
When I added these to the parent:
当我将这些添加到父项中时:
term.on("error", (err) => {
console.error("Error in parent process:", err);
});
term.on("exit", (code, signal) => {
console.log(`Child process exited with code ${code} and signal ${signal}`);
});
This was the output:
以下是输出:
Child process exited with code 134 and signal null
I am losing my mind on this problem. I thought about using sockets, which will be easier and surely will work, but I am still haunted by this IPC thing and I want to know the solution to this insane problem.
在这个问题上我快失去理智了。我想过使用套接字,这会更容易,而且肯定会起作用,但我仍然被IPC的事情困扰着,我想知道这个疯狂问题的解决方案。
Thank you
谢谢
更多回答
我是一名优秀的程序员,十分优秀!