108 lines
2.3 KiB
TypeScript
108 lines
2.3 KiB
TypeScript
import { app, BrowserWindow, Menu, dialog, ipcMain } 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));
|
|
|
|
let mainWindow: BrowserWindow | null = null;
|
|
|
|
async function createWindow() {
|
|
const win = new BrowserWindow({
|
|
width: 1220,
|
|
height: 820,
|
|
minWidth: 980,
|
|
minHeight: 680,
|
|
title: "EnvHelper",
|
|
frame: false,
|
|
autoHideMenuBar: true,
|
|
backgroundColor: "#f6f7f4",
|
|
webPreferences: {
|
|
preload: path.join(__dirname, "preload.js"),
|
|
contextIsolation: true,
|
|
nodeIntegration: false
|
|
}
|
|
});
|
|
|
|
mainWindow = win;
|
|
win.on("closed", () => {
|
|
if (mainWindow === win) {
|
|
mainWindow = null;
|
|
}
|
|
});
|
|
|
|
Menu.setApplicationMenu(null);
|
|
|
|
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 () => {
|
|
const result = await dialog.showOpenDialog({
|
|
properties: ["openFile"],
|
|
filters: [{ name: "Environment files", extensions: ["env", "txt", "*"] }]
|
|
});
|
|
|
|
if (result.canceled || result.filePaths.length === 0) {
|
|
return null;
|
|
}
|
|
|
|
const filePath = result.filePaths[0];
|
|
return {
|
|
path: filePath,
|
|
content: await readFile(filePath, "utf8")
|
|
};
|
|
});
|
|
|
|
ipcMain.on("envhelper:window-minimize", () => {
|
|
mainWindow?.minimize();
|
|
});
|
|
|
|
ipcMain.on("envhelper:window-toggle-maximize", () => {
|
|
if (!mainWindow) {
|
|
return;
|
|
}
|
|
|
|
if (mainWindow.isMaximized()) {
|
|
mainWindow.unmaximize();
|
|
return;
|
|
}
|
|
|
|
mainWindow.maximize();
|
|
});
|
|
|
|
ipcMain.on("envhelper:window-close", () => {
|
|
mainWindow?.close();
|
|
});
|
|
|
|
ipcMain.handle("envhelper:save-file", async (_event, content: string) => {
|
|
const result = await dialog.showSaveDialog({
|
|
defaultPath: ".env",
|
|
filters: [{ name: "Environment file", extensions: ["env"] }]
|
|
});
|
|
|
|
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();
|
|
}
|
|
});
|