From 9d6758b3158cd1b0851eb9b2057c49c527fcee0c Mon Sep 17 00:00:00 2001 From: Elian Doran Date: Wed, 21 May 2025 16:32:51 +0300 Subject: [PATCH] refactor(server); electron imports causing issues in bundle --- apps/server/src/app.ts | 2 +- apps/server/src/routes/electron.ts | 4 ++-- apps/server/src/services/tray.ts | 16 ++++++++-------- apps/server/src/services/window.ts | 24 ++++++++++++------------ 4 files changed, 23 insertions(+), 23 deletions(-) diff --git a/apps/server/src/app.ts b/apps/server/src/app.ts index b48c7ced8..3ac3ee5c4 100644 --- a/apps/server/src/app.ts +++ b/apps/server/src/app.ts @@ -137,7 +137,7 @@ export default async function buildApp() { startScheduledCleanup(); if (utils.isElectron) { - (await import("@electron/remote/main")).initialize(); + (await import("@electron/remote/main/index.js")).initialize(); } return app; diff --git a/apps/server/src/routes/electron.ts b/apps/server/src/routes/electron.ts index 6990bab79..feb9dceae 100644 --- a/apps/server/src/routes/electron.ts +++ b/apps/server/src/routes/electron.ts @@ -1,4 +1,4 @@ -import { ipcMain } from "electron"; +import electron from "electron"; interface Response { statusCode: number; @@ -10,7 +10,7 @@ interface Response { } function init(app: Express.Application) { - ipcMain.on("server-request", (event, arg) => { + electron.ipcMain.on("server-request", (event, arg) => { const req = { url: arg.url, method: arg.method, diff --git a/apps/server/src/services/tray.ts b/apps/server/src/services/tray.ts index 0d054d9ec..0866439e3 100644 --- a/apps/server/src/services/tray.ts +++ b/apps/server/src/services/tray.ts @@ -1,7 +1,7 @@ -import { BrowserWindow, Menu, Tray, ipcMain, nativeTheme } from "electron"; +import electron from "electron"; +import type { BrowserWindow, Tray } from "electron"; import { default as i18next, t } from "i18next"; import path from "path"; -import { fileURLToPath } from "url"; import becca from "../becca/becca.js"; import becca_service from "../becca/becca_service.js"; @@ -33,7 +33,7 @@ function getTrayIconPath() { } function getIconPath(name: string) { - const suffix = !isMac && nativeTheme.shouldUseDarkColors ? "-inverted" : ""; + const suffix = !isMac && electron.nativeTheme.shouldUseDarkColors ? "-inverted" : ""; return path.resolve(path.join(getResourceDir(), "assets", "images", "tray", `${name}Template${suffix}.png`)); } @@ -216,7 +216,7 @@ function updateTrayMenu() { } - const contextMenu = Menu.buildFromTemplate([ + const contextMenu = electron.Menu.buildFromTemplate([ ...windowVisibilityMenuItems, { type: "separator" }, { @@ -255,7 +255,7 @@ function updateTrayMenu() { type: "normal", icon: getIconPath("close"), click: () => { - const windows = BrowserWindow.getAllWindows(); + const windows = electron.BrowserWindow.getAllWindows(); windows.forEach(window => { window.close(); }); @@ -287,7 +287,7 @@ function createTray() { return; } - tray = new Tray(getTrayIconPath()); + tray = new electron.Tray(getTrayIconPath()); tray.setToolTip(t("tray.tooltip")); // Restore focus tray.on("click", changeVisibility); @@ -295,9 +295,9 @@ function createTray() { if (!isMac) { // macOS uses template icons which work great on dark & light themes. - nativeTheme.on("updated", updateTrayMenu); + electron.nativeTheme.on("updated", updateTrayMenu); } - ipcMain.on("reload-tray", updateTrayMenu); + electron.ipcMain.on("reload-tray", updateTrayMenu); i18next.on("languageChanged", updateTrayMenu); } diff --git a/apps/server/src/services/window.ts b/apps/server/src/services/window.ts index 1c041be59..522d33ffc 100644 --- a/apps/server/src/services/window.ts +++ b/apps/server/src/services/window.ts @@ -7,9 +7,9 @@ import log from "./log.js"; import sqlInit from "./sql_init.js"; import cls from "./cls.js"; import keyboardActionsService from "./keyboard_actions.js"; -import * as remoteMain from "@electron/remote/main"; -import { BrowserWindow, shell, type App, type BrowserWindowConstructorOptions, type WebContents } from "electron"; -import { dialog, ipcMain } from "electron"; +import * as remoteMain from "@electron/remote/main/index.js"; +import electron from "electron"; +import type { App, BrowserWindowConstructorOptions, BrowserWindow, WebContents } from "electron"; import { formatDownloadTitle, isDev, isMac, isWindows } from "./utils.js"; import { fileURLToPath } from "url"; @@ -28,14 +28,14 @@ function trackWindowFocus(win: BrowserWindow) { allWindows = allWindows.filter(w => !w.isDestroyed() && w !== win); allWindows.push(win); if (!optionService.getOptionBool("disableTray")) { - ipcMain.emit("reload-tray"); + electron.ipcMain.emit("reload-tray"); } }); win.on("closed", () => { allWindows = allWindows.filter(w => !w.isDestroyed()); if (!optionService.getOptionBool("disableTray")) { - ipcMain.emit("reload-tray"); + electron.ipcMain.emit("reload-tray"); } }); } @@ -66,7 +66,7 @@ async function createExtraWindow(extraWindowHash: string) { trackWindowFocus(win); } -ipcMain.on("create-extra-window", (event, arg) => { +electron.ipcMain.on("create-extra-window", (event, arg) => { createExtraWindow(arg.extraWindowHash); }); @@ -76,13 +76,13 @@ interface ExportAsPdfOpts { pageSize: "A0" | "A1" | "A2" | "A3" | "A4" | "A5" | "A6" | "Legal" | "Letter" | "Tabloid" | "Ledger"; } -ipcMain.on("export-as-pdf", async (e, opts: ExportAsPdfOpts) => { - const browserWindow = BrowserWindow.fromWebContents(e.sender); +electron.ipcMain.on("export-as-pdf", async (e, opts: ExportAsPdfOpts) => { + const browserWindow = electron.BrowserWindow.fromWebContents(e.sender); if (!browserWindow) { return; } - const filePath = dialog.showSaveDialogSync(browserWindow, { + const filePath = electron.dialog.showSaveDialogSync(browserWindow, { defaultPath: formatDownloadTitle(opts.title, "file", "application/pdf"), filters: [ { @@ -111,18 +111,18 @@ ipcMain.on("export-as-pdf", async (e, opts: ExportAsPdfOpts) => { ` }); } catch (e) { - dialog.showErrorBox(t("pdf.unable-to-export-title"), t("pdf.unable-to-export-message")); + electron.dialog.showErrorBox(t("pdf.unable-to-export-title"), t("pdf.unable-to-export-message")); return; } try { await fs.writeFile(filePath, buffer); } catch (e) { - dialog.showErrorBox(t("pdf.unable-to-export-title"), t("pdf.unable-to-save-message")); + electron.dialog.showErrorBox(t("pdf.unable-to-export-title"), t("pdf.unable-to-save-message")); return; } - shell.openPath(filePath); + electron.shell.openPath(filePath); }); async function createMainWindow(app: App) {