Files
yt/electron/main.cjs

92 lines
2.0 KiB
JavaScript

const { app, BrowserWindow } = require("electron");
const { spawn } = require("child_process");
const path = require("path");
let mainWindow;
let astroProcess;
// Prüfe ob wir im Development-Modus sind
const isDev = process.env.NODE_ENV === "development" || !app.isPackaged;
function createWindow() {
mainWindow = new BrowserWindow({
width: 1200,
height: 800,
webPreferences: {
nodeIntegration: false,
contextIsolation: true,
},
autoHideMenuBar: true,
titleBarStyle: "default",
});
if (isDev) {
// Im Development-Modus: Verbinde zu lokalem Dev-Server
mainWindow.loadURL("http://localhost:4321");
mainWindow.webContents.openDevTools();
} else {
// Im Production-Modus: Starte lokalen Server und verbinde
startLocalServer();
}
mainWindow.on("closed", () => {
mainWindow = null;
});
}
function startLocalServer() {
const serverPath = path.join(__dirname, "../dist/server/entry.mjs");
// Starte den Astro-Server
astroProcess = spawn("node", [serverPath], {
cwd: path.join(__dirname, ".."),
env: {
...process.env,
PORT: "4321",
},
});
astroProcess.stdout.on("data", (data) => {
console.log(`Server: ${data}`);
if (data.toString().includes("Local") || data.toString().includes("4321")) {
// Warte kurz, dann lade die Seite
setTimeout(() => {
mainWindow.loadURL("http://localhost:4321");
}, 2000);
}
});
astroProcess.stderr.on("data", (data) => {
console.error(`Server Error: ${data}`);
});
astroProcess.on("close", (code) => {
console.log(`Server process exited with code ${code}`);
});
}
app.whenReady().then(() => {
createWindow();
app.on("activate", () => {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow();
}
});
});
app.on("window-all-closed", () => {
if (astroProcess) {
astroProcess.kill();
}
if (process.platform !== "darwin") {
app.quit();
}
});
app.on("before-quit", () => {
if (astroProcess) {
astroProcess.kill();
}
});