示例工程 08-ipc-invoke-handle

08-ipc-invoke-handle/demo-01-invoke-error/.tsc/main/index.js

javascript · 2.9 KB

import { app, BrowserWindow, ipcMain } from "electron";
import fsSync from "node:fs";
import path from "node:path";
const isSmoke = process.argv.includes("--smoke") || process.env.DEMO_SMOKE === "1";
function resolvePreload() {
    const candidates = [
        path.join(__dirname, "preload.cjs"),
        path.join(__dirname, "../preload/index.cjs"),
        path.join(__dirname, "preload.js"),
    ];
    return (candidates.find((candidate) => fsSync.existsSync(candidate)) ??
        candidates[0] ??
        "");
}
ipcMain.handle("demo:boom", () => {
    throw new Error("demo: 主进程抛出异常");
});
function createWindow(show) {
    const win = new BrowserWindow({
        width: 720,
        height: 480,
        show,
        webPreferences: {
            preload: resolvePreload(),
            contextIsolation: true,
            sandbox: true,
            nodeIntegration: false,
            webSecurity: true,
            allowRunningInsecureContent: false,
            experimentalFeatures: false,
            webviewTag: false,
            spellcheck: false,
            backgroundThrottling: true,
        },
    });
    if (MAIN_WINDOW_VITE_DEV_SERVER_URL) {
        void win.loadURL(MAIN_WINDOW_VITE_DEV_SERVER_URL);
    }
    else {
        void win.loadFile(path.join(__dirname, `../renderer/${MAIN_WINDOW_VITE_NAME}/index.html`));
    }
    return win;
}
async function runSmoke() {
    const win = createWindow(false);
    await win.webContents.executeJavaScript(`new Promise((resolve) => {
      const started = Date.now();
      const tick = () => {
        if (window.demo) resolve(true);
        else if (Date.now() - started > 3000) resolve(false);
        else setTimeout(tick, 20);
      };
      tick();
    })`);
    const caught = (await win.webContents.executeJavaScript(`window.demo.boom().then(() => null).catch((error) => ({
      name: error.name,
      message: error.message,
      stack: String(error.stack || "").split("\\n").filter(Boolean),
    }))`));
    process.stdout.write(`${JSON.stringify({
        evt: "smoke",
        ok: caught?.name === "Error" &&
            caught.message.includes("demo: 主进程抛出异常") &&
            caught.stack.length === 1,
        name: caught?.name ?? null,
        message: caught?.message ?? null,
        stackLines: caught?.stack.length ?? 0,
        stackFirst: caught?.stack[0] ?? null,
    })}\n`);
    app.quit();
}
app.whenReady().then(() => {
    if (isSmoke) {
        void runSmoke().catch((error) => {
            process.stdout.write(`${JSON.stringify({ evt: "smoke", ok: false, message: String(error) })}\n`);
            app.exit(1);
        });
        return;
    }
    createWindow(true);
    app.on("activate", () => {
        if (BrowserWindow.getAllWindows().length === 0)
            createWindow(true);
    });
});
app.on("window-all-closed", () => {
    if (process.platform !== "darwin")
        app.quit();
});
//# sourceMappingURL=index.js.map