-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.cjs
81 lines (65 loc) · 1.89 KB
/
index.cjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
const { app, BrowserWindow } = require("electron");
const http = require("http");
const log = require("electron-log");
//Set custom environment variables:
process.env["PORT"] = "3000";
process.env["HOST"] = "localhost";
process.env["ORIGIN"] = `http://${process.env.HOST}:${process.env.PORT}`;
const isDev = !app.isPackaged;
const isMac = process.platform === "darwin" ? true : false;
if (!isDev) {
const server = import(`./build/index.js`)
.then((server) => server)
.catch((err) => log.error(`Server Load Error: ${err}`));
}
let mainWindow;
async function createMainWindow() {
mainWindow = new BrowserWindow({
title: `Svelte Electron | Home`,
width: 800,
height: 600,
resizable: !isDev ? true : false,
show: true,
webPreferences: {
contextIsolation: true,
devTools: isDev,
nodeIntegration: true,
},
});
if (isDev) {
mainWindow.webContents.openDevTools();
}
await serveHome(0);
}
app.on("ready", async () => {
createMainWindow();
mainWindow.on("close", () => {
app.quit();
});
});
app.on("window-all-closed", () => {
if (!isMac) app.quit();
});
app.on("activate", () => {
if (BrowserWindow.getAllWindows().length === 0) createMainWindow();
});
async function serveHome(attempts) {
if (attempts >= 10) app.quit();
http
.get(`${process.env.ORIGIN}`, (res) => {
const { statusCode, statusMessage } = res;
log.info(`HTTP Response: ${statusCode}, ${statusMessage}`);
if (statusMessage !== "OK") {
log.error(`Request Failed with status code: ${statusCode}`);
setTimeout(() => serveHome(attempts + 1), 2000);
return;
} else {
log.info(`Request end, loadURL: /`);
mainWindow.webContents.loadURL(`${process.env.ORIGIN}/`);
}
})
.on("error", (err) => {
log.error(`Error received: ${err}`);
setTimeout(() => serveHome(attempts + 1), 2000);
});
}