如何在Eletron中打开window的powershell

js 复制代码
const { spawn } = require("child_process");
const { app, shell, dialog } = require("electron");
let server;
let powershellProcess;
let isExiting = false;
function startPowerShell() {
  try {
    const command = [
      "[Console]::OutputEncoding = [System.Text.Encoding]::UTF8",
      "[Console]::InputEncoding = [System.Text.Encoding]::UTF8",
      '[Console]::WriteLine("==================== 应用状态 ====================")',
      '[Console]::WriteLine("本地服务器已启动:http://localhost:3000")',
      '[Console]::WriteLine("提示:关闭此窗口将终止应用程序")',
      '[Console]::WriteLine("===================================================")',
      '[Console]::WriteLine("按任意键关闭应用...")',
      '$host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown") | Out-Null',
      "exit",
    ].join("; ");

    // 使用更简单直接的方式启动PowerShell
    powershellProcess = spawn(
      "powershell.exe",
      ["-NoProfile", "-Command", command],
      {
        stdio: "inherit",
        detached: false,
        windowsHide: false,
      }
    );
    // 处理PowerShell进程退出
    powershellProcess.on("exit", (code) => {
      if (!isExiting) {
        // 用户主动关闭了PowerShell窗口
        app.quit();
      }
    });

    // 处理启动错误
    powershellProcess.on("error", (err) => {
      console.error("启动PowerShell失败:", err);
      dialog.showErrorBox(
        "启动失败",
        `无法启动PowerShell:${err.message}\n请确认系统已安装PowerShell`
      );
      cleanupResources();
      app.quit();
    });
  } catch (err) {
    dialog.showErrorBox("启动异常", err.message);
    cleanupResources();
    app.quit();
  }
}

function cleanupResources() {
  if (isExiting) return;
  isExiting = true;

  if (server) {
    server.close();
  }

  if (powershellProcess && !powershellProcess.killed) {
    powershellProcess.kill();
  }
}
app.whenReady().then(() => {
  startPowerShell();
  startLocalServer();
});

为什么打开window的powershell无法显示任何文字,这是在electron的主进程启动的

相关推荐
DanCheOo几秒前
AI Streaming 架构:从浏览器到服务端的全链路流式设计
前端·agent
我是小趴菜6 分钟前
前端如何让图片、视频、pdf等文件在浏览器直接下载而非预览
前端
cg3311 分钟前
开源项目自动化:用 GitHub Actions 让每个 Issue 都被温柔以待
前端
haierccc21 分钟前
Win7、2008R2、Win10、Win11使用FLASH的方法
前端·javascript·html
We་ct24 分钟前
LeetCode 50. Pow(x, n):从暴力法到快速幂的优化之路
开发语言·前端·javascript·算法·leetcode·typescript·
柠檬味的Cat27 分钟前
使用腾讯云COS作为WordPress图床的实践
前端·github·腾讯云
Hilaku29 分钟前
卷AI、卷算法、2026 年的前端工程师到底在卷什么?
前端·javascript·面试
非凡ghost31 分钟前
AIMP(音乐播放软件)
前端·windows·音视频·firefox
xiaotao13133 分钟前
Vite 完全学习指南
前端·vite·前端打包
军军君011 小时前
Three.js基础功能学习十五:智能黑板实现实例二
开发语言·前端·javascript·vue.js·3d·threejs·三维