如何在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的主进程启动的

相关推荐
我是小胡胡3 小时前
彦火APP-Flutter包体分析
前端
木斯佳4 小时前
前端八股文面经大全:腾讯音乐-前端一面(2026-05-27)·面经深度解析
前端
糖果店的幽灵4 小时前
Claude Code 完全实战指南 - 第四章:Skill 怎么写
java·服务器·前端
light blue bird4 小时前
MES/ERP 工序 BOM 协同场景调度维护组件
前端·信息可视化·桌面端winform·多节点端·gdi图表绘制开发
鱼人4 小时前
Vue 3 组合式 API 最佳实践:如何写出可维护的代码
前端
wuhen_n4 小时前
LangChain 自定义 Tool 封装:打造专属 AI 能力工具集
前端·langchain·ai编程
长大19884 小时前
彻底搞懂 JavaScript 事件循环
前端
橘猫走江湖4 小时前
Web 前端本地存储:localStorage 与 IndexedDB
前端·javascript·indexeddb
小强19884 小时前
CSS 布局进化史:从 Float 到 Flexbox 再到 Grid
前端
AKA__老方丈4 小时前
删除确认 Hook - 统一管理单删/批量删除的确认弹窗与执行
前端·javascript·vue.js