import { app, BrowserWindow, Menu, dialog, ipcMain } from "electron"; import type { OpenDialogOptions, SaveDialogOptions } from "electron"; import { readFile, writeFile } from "node:fs/promises"; import path from "node:path"; import { fileURLToPath } from "node:url"; const __dirname = path.dirname(fileURLToPath(import.meta.url)); app.setAppUserModelId("de.wilkensxl.envhelper"); async function createWindow() { const win = new BrowserWindow({ width: 1220, height: 820, minWidth: 980, minHeight: 680, title: "EnvHelper", frame: true, autoHideMenuBar: true, backgroundColor: "#f6f7f4", webPreferences: { preload: path.join(__dirname, "preload.js"), contextIsolation: true, nodeIntegration: false, sandbox: true, webSecurity: true, allowRunningInsecureContent: false, devTools: !app.isPackaged } }); Menu.setApplicationMenu(null); win.webContents.setWindowOpenHandler(() => ({ action: "deny" })); win.webContents.on("will-navigate", (event, url) => { const currentUrl = win.webContents.getURL(); if (!currentUrl || url === currentUrl || (!app.isPackaged && url.startsWith("http://127.0.0.1:5173"))) { return; } event.preventDefault(); }); if (!app.isPackaged) { await win.loadURL("http://127.0.0.1:5173"); } else { await win.loadFile(path.join(__dirname, "../dist/index.html")); } } ipcMain.handle("envhelper:open-file", async (event) => { const owner = BrowserWindow.fromWebContents(event.sender); const options = { properties: ["openFile"], filters: [ { name: "Environment files", extensions: ["env", "txt"] }, { name: "All files", extensions: ["*"] } ] } satisfies OpenDialogOptions; const result = owner ? await dialog.showOpenDialog(owner, options) : await dialog.showOpenDialog(options); if (result.canceled || result.filePaths.length === 0) { return null; } const filePath = result.filePaths[0]; return { path: filePath, content: await readFile(filePath, "utf8") }; }); ipcMain.handle("envhelper:save-file", async (event, content: string) => { if (typeof content !== "string") { throw new TypeError("Save content must be a string."); } const owner = BrowserWindow.fromWebContents(event.sender); const options = { defaultPath: ".env", filters: [ { name: "Environment files", extensions: ["env", "txt"] }, { name: "All files", extensions: ["*"] } ] } satisfies SaveDialogOptions; const result = owner ? await dialog.showSaveDialog(owner, options) : await dialog.showSaveDialog(options); if (result.canceled || !result.filePath) { return null; } await writeFile(result.filePath, content, "utf8"); return result.filePath; }); app.whenReady().then(createWindow); app.on("window-all-closed", () => { if (process.platform !== "darwin") { app.quit(); } }); app.on("activate", () => { if (BrowserWindow.getAllWindows().length === 0) { void createWindow(); } });