Files

110 lines
3.0 KiB
TypeScript
Raw Permalink Normal View History

import { app, BrowserWindow, Menu, dialog, ipcMain } from "electron";
2026-05-02 00:53:13 +02:00
import type { OpenDialogOptions, SaveDialogOptions } from "electron";
2026-05-01 12:54:29 +02:00
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));
2026-05-02 01:01:57 +02:00
app.setAppUserModelId("de.wilkensxl.envhelper");
2026-05-01 12:54:29 +02:00
async function createWindow() {
const win = new BrowserWindow({
width: 1220,
height: 820,
minWidth: 980,
minHeight: 680,
title: "EnvHelper",
2026-05-01 21:14:41 +02:00
frame: true,
autoHideMenuBar: true,
2026-05-01 12:54:29 +02:00
backgroundColor: "#f6f7f4",
webPreferences: {
preload: path.join(__dirname, "preload.js"),
contextIsolation: true,
2026-05-02 01:01:57 +02:00
nodeIntegration: false,
sandbox: true,
webSecurity: true,
allowRunningInsecureContent: false,
devTools: !app.isPackaged
2026-05-01 12:54:29 +02:00
}
});
Menu.setApplicationMenu(null);
2026-05-02 01:01:57 +02:00
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();
});
2026-05-01 17:45:16 +02:00
if (!app.isPackaged) {
2026-05-01 12:54:29 +02:00
await win.loadURL("http://127.0.0.1:5173");
} else {
await win.loadFile(path.join(__dirname, "../dist/index.html"));
}
}
2026-05-01 23:54:51 +02:00
ipcMain.handle("envhelper:open-file", async (event) => {
const owner = BrowserWindow.fromWebContents(event.sender);
const options = {
2026-05-01 12:54:29 +02:00
properties: ["openFile"],
2026-05-01 23:54:51 +02:00
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);
2026-05-01 12:54:29 +02:00
if (result.canceled || result.filePaths.length === 0) {
return null;
}
const filePath = result.filePaths[0];
return {
path: filePath,
content: await readFile(filePath, "utf8")
};
});
2026-05-02 00:53:13 +02:00
ipcMain.handle("envhelper:save-file", async (event, content: string) => {
2026-05-02 01:01:57 +02:00
if (typeof content !== "string") {
throw new TypeError("Save content must be a string.");
}
2026-05-02 00:53:13 +02:00
const owner = BrowserWindow.fromWebContents(event.sender);
const options = {
2026-05-01 12:54:29 +02:00
defaultPath: ".env",
2026-05-02 00:53:13 +02:00
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);
2026-05-01 12:54:29 +02:00
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();
}
});