-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.ts
More file actions
124 lines (104 loc) · 3.4 KB
/
Copy pathmain.ts
File metadata and controls
124 lines (104 loc) · 3.4 KB
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// Must be first: side-effect import runs dotenv.config() before any other import body.
import "dotenv/config";
import { app, BrowserWindow, shell, utilityProcess } from "electron";
import type { UtilityProcess } from "electron";
import path from "node:path";
import { registerIPCHandlers } from "./ipc-handlers";
// CJS bundle: __dirname is a native global. No fileURLToPath / import.meta dance needed.
process.env.APP_ROOT = path.join(__dirname, "..");
export const VITE_DEV_SERVER_URL = process.env["VITE_DEV_SERVER_URL"];
export const MAIN_DIST = path.join(process.env.APP_ROOT, "dist-electron");
export const RENDERER_DIST = path.join(process.env.APP_ROOT, "dist");
process.env.VITE_PUBLIC = VITE_DEV_SERVER_URL
? path.join(process.env.APP_ROOT, "public")
: RENDERER_DIST;
const smokeDebugPort = process.env.OVERCODE_REMOTE_DEBUGGING_PORT?.trim();
if (smokeDebugPort) {
app.commandLine.appendSwitch("remote-debugging-port", smokeDebugPort);
}
// Opt-in sandbox bypass for Linux kernels that reject the bundled chrome-sandbox
// helper (some Arch/Manjaro and SELinux-enforcing systems). Default off.
const useLinuxPackagedNoSandbox =
process.platform === "linux" && app.isPackaged && process.env.OVERCODE_NO_SANDBOX === "1";
if (useLinuxPackagedNoSandbox) {
app.commandLine.appendSwitch("no-sandbox");
}
const smokeUserDataDir = process.env.OVERCODE_SMOKE_USER_DATA_DIR?.trim();
if (smokeUserDataDir) {
app.setPath("userData", smokeUserDataDir);
}
let win: BrowserWindow | null = null;
let gitWorker: UtilityProcess | null = null;
const EXTERNAL_LINK_HOSTS = new Set([
"github.com",
"www.github.com",
"gitlab.com",
"www.gitlab.com",
]);
function isAllowedExternalLink(url: string): boolean {
try {
const parsed = new URL(url);
return parsed.protocol === "https:" && EXTERNAL_LINK_HOSTS.has(parsed.hostname);
} catch {
return false;
}
}
function openAllowedExternalLink(url: string): void {
if (isAllowedExternalLink(url)) {
void shell.openExternal(url);
}
}
function createWindow() {
win = new BrowserWindow({
width: 1400,
height: 900,
icon: path.join(process.env.VITE_PUBLIC, "overcode-icon-512.png"),
backgroundColor: "#0e0e18",
title: "Overcode",
webPreferences: {
preload: path.join(__dirname, "preload.js"),
contextIsolation: true,
nodeIntegration: false,
sandbox: !useLinuxPackagedNoSandbox,
},
});
win.webContents.on("did-finish-load", () => {
win?.webContents.send("main-process-message", new Date().toLocaleString());
});
win.webContents.setWindowOpenHandler(({ url }) => {
openAllowedExternalLink(url);
return { action: "deny" };
});
if (VITE_DEV_SERVER_URL) {
win.loadURL(VITE_DEV_SERVER_URL);
} else {
win.loadFile(path.join(RENDERER_DIST, "index.html"));
}
}
app.on("window-all-closed", () => {
if (process.platform !== "darwin") {
app.quit();
win = null;
}
});
app.on("activate", () => {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow();
}
});
app.on("before-quit", () => {
gitWorker?.kill();
gitWorker = null;
});
function createGitWorker(): UtilityProcess {
const worker = utilityProcess.fork(path.join(__dirname, "workers", "git-worker.js"));
worker.once("exit", () => {
if (gitWorker === worker) gitWorker = null;
});
return worker;
}
app.whenReady().then(() => {
gitWorker = createGitWorker();
registerIPCHandlers(gitWorker);
createWindow();
});