const {app, BrowserWindow, dialog, shell} = require("electron"); const path = require("node:path"); const {pathToFileURL} = require("node:url"); const PORT = 4321; let mainWindow; const waitForServer = async () => { for (let attempt = 0; attempt < 80; attempt++) { try { const response = await fetch(`http://127.0.0.1:${PORT}/api/health`); if (response.ok) return; } catch {} await new Promise((resolve) => setTimeout(resolve, 150)); } throw new Error("Der lokale PDEV-Server konnte nicht gestartet werden."); }; const startServer = async () => { const projectRoot = path.join(__dirname, ".."); const resourceRoot = app.isPackaged ? process.resourcesPath : projectRoot; process.env.NODE_ENV = "production"; process.env.PDEV_DESKTOP = "1"; process.env.PORT = String(PORT); process.env.PDEV_APP_ROOT = projectRoot; process.env.PDEV_OUTPUT_DIR = path.join(app.getPath("videos"), "PDEV AI Video Studio"); process.env.PDEV_REMOTION_ENTRY = app.isPackaged ? path.join(resourceRoot, "remotion-src", "index.ts") : path.join(projectRoot, "src", "remotion", "index.ts"); await import(pathToFileURL(path.join(projectRoot, "server.mjs")).href); await waitForServer(); }; const createWindow = () => { mainWindow = new BrowserWindow({ width: 1540, height: 980, minWidth: 1080, minHeight: 720, backgroundColor: "#080a0f", title: "PDEV AI Video Studio", show: false, autoHideMenuBar: true, webPreferences: { contextIsolation: true, nodeIntegration: false, sandbox: true, }, }); mainWindow.loadURL(`http://127.0.0.1:${PORT}`); mainWindow.once("ready-to-show", () => mainWindow.show()); mainWindow.webContents.setWindowOpenHandler(({url}) => { if (url.startsWith(`http://127.0.0.1:${PORT}/renders/`)) return {action: "allow"}; shell.openExternal(url); return {action: "deny"}; }); mainWindow.webContents.on("will-navigate", (event, url) => { if (!url.startsWith(`http://127.0.0.1:${PORT}`)) { event.preventDefault(); shell.openExternal(url); } }); }; if (!app.requestSingleInstanceLock()) { app.quit(); } else { app.on("second-instance", () => { if (mainWindow) { if (mainWindow.isMinimized()) mainWindow.restore(); mainWindow.focus(); } }); app.whenReady().then(async () => { try { await startServer(); createWindow(); } catch (error) { dialog.showErrorBox("PDEV AI Video Studio", error instanceof Error ? error.message : String(error)); app.quit(); } }); } app.on("window-all-closed", () => app.quit());