[英]How do I correctly launch a shell environment with node-pty in Electron?
I have an Electron App that's using node-pty to create a pseudo-terminal instance.我有一个 Electron 应用程序,它使用 node-pty 创建一个伪终端实例。 This instance is created as follows:该实例创建如下:
const pty = require('node-pty');
const os = require('os');
const shell = process.env[os.platform() === 'win32' ? 'COMSPEC' : 'SHELL'];
const ptyProcess = pty.spawn(shell, [], {
name: 'xterm-color',
cols: 100,
rows: 40,
cwd: process.env.HOME,
env: process.env
});
When I launch the app from the vscode terminal using ( electron.
) everything works as expected.当我使用( electron.
)从 vscode 终端启动应用程序时,一切都按预期工作。 Ie the shell can find all programs just like it would in cmd.exe or Terminal.app.即 shell 可以像在 cmd.exe 或 Terminal.app 中一样找到所有程序。
However, if I build the app, and open it via double clicking on it, I notice that my path does not contain certain directories such as /usr/local/bin/
, which can cause certain programs (like brew
) to fail with "Not found" errors.但是,如果我构建应用程序并通过双击打开它,我注意到我的路径不包含某些目录,例如/usr/local/bin/
,这可能导致某些程序(例如brew
)失败并显示“未找到”错误。
Notably, if I launch the electron app from a terminal on mac using open -a MyElectronApp
everything works as expected.值得注意的是,如果我使用open -a MyElectronApp
MyElectronApp 从 Mac 上的终端启动 electron 应用程序,一切都会按预期工作。
I suspect my app needs to initialize the path differently somehow.我怀疑我的应用程序需要以不同的方式初始化路径。 However, since this is a cross-platform app, I'd like to avoid doing anything like "if os.platform == 'darwin' load /etc/paths".但是,由于这是一个跨平台的应用程序,我想避免做任何类似“if os.platform == 'darwin' load /etc/paths”的事情。
Any help would be greatly appreciated.任何帮助将不胜感激。 Please let me know if you need more information.如果您需要更多信息,请告诉我。 I assume my issue is probably due to a misunderstanding/ignorance of shell environments!我认为我的问题可能是由于对 shell 环境的误解/无知!
Faced same issue.面临同样的问题。 Used https://github.com/sindresorhus/fix-path二手https://github.com/sindresorhus/fix-path
process.env.PATH was not available for the electron process in production mode. process.env.PATH 不适用于生产模式下的 electron 进程。 The above fix-path
fixes the issue上面fix-path
解决了这个问题
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.