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

相关推荐
打小就很皮...5 分钟前
React 19 + Vite 6 + SWC 构建优化实践
前端·react.js·vite·swc
Highcharts.js8 分钟前
使用Highcharts与React集成 官网文档使用说明
前端·react.js·前端框架·react·highcharts·官方文档
这是个栗子8 分钟前
AI辅助编程(二) - 通译千问
前端·ai·通译千问
VT.馒头19 分钟前
【力扣】2625. 扁平化嵌套数组
前端·javascript·算法·leetcode·职场和发展·typescript
数研小生1 小时前
Full Analysis of Taobao Item Detail API taobao.item.get
java·服务器·前端
Shirley~~1 小时前
Vue-skills的中文文档
前端·人工智能
毎天要喝八杯水1 小时前
搭建vue前端后端环境
前端·javascript·vue.js
计算机程序设计小李同学2 小时前
幼儿园信息管理系统的设计与实现
前端·bootstrap·html·毕业设计
雨季6662 小时前
Flutter 三端应用实战:OpenHarmony “专注时光盒”——在碎片洪流中守护心流的数字容器
开发语言·前端·安全·flutter·交互
tao3556672 小时前
【用AI学前端】HTML-02-HTML 常用标签(基础)
前端·html