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

相关推荐
RoyLin5 小时前
C++ 基础与核心概念
前端·后端·node.js
记得坚持5 小时前
vue2插槽
前端·vue.js
臣臣臣臣臣什么臣5 小时前
uni-app 多文件上传:直接循环调用 uni.uploadFile 实现(并行 / 串行双模式)
android·前端
带只拖鞋去流浪5 小时前
Vue.js响应式API
前端·javascript·vue.js
Coder_R5 小时前
如何 把 Mac 上的 APK(app) 安装到安卓手机上?
前端·面试
前端小灰狼5 小时前
Ant Design Vue Vue3 table 表头筛选重置不清空Bug
前端·javascript·vue.js·bug
前端付豪5 小时前
11、JavaScript 语法:到底要不要写分号?一文吃透 ASI 与坑点清单
前端·javascript
Copper peas5 小时前
Vue 中的 v-model 指令详解
前端·javascript·vue.js
前端小书生5 小时前
NestJs
前端·nestjs
万少5 小时前
十行代码 带你极速接入鸿蒙6新特性 - 应用内打分评价
前端·harmonyos