106 lines
3.5 kB
1
import * as electron from "electron";
2
import * as fs from "node:fs/promises";
3
import * as path from "node:path";
4
import getNatives from "./native";
5
import { MoonlightBranch } from "@moonlight-mod/types";
6
7
const natives = getNatives();
8
9
const confirm = (action: string) =>
10
electron.dialog
11
.showMessageBox({
12
title: "Are you sure?",
13
message: `Are you sure? This will ${action} and restart Discord.`,
14
type: "warning",
15
buttons: ["OK", "Cancel"]
16
})
17
.then((r) => r.response === 0);
18
19
async function updateAndRestart() {
20
if (!(await confirm("update moonlight"))) return;
21
const newVersion = await natives.checkForMoonlightUpdate();
22
23
if (newVersion === null) {
24
electron.dialog.showMessageBox({ message: "You are already on the latest version of moonlight." });
25
return;
26
}
27
28
try {
29
await natives.updateMoonlight();
30
await electron.dialog.showMessageBox({ message: "Update successful, restarting Discord." });
31
electron.app.relaunch();
32
electron.app.exit(0);
33
} catch {
34
await electron.dialog.showMessageBox({
35
message: "Failed to update moonlight. Please use the installer instead.",
36
type: "error"
37
});
38
}
39
}
40
41
async function resetConfig() {
42
if (!(await confirm("reset your configuration"))) return;
43
44
const config = await moonlightHost.getConfigPath();
45
const dir = path.dirname(config);
46
const branch = path.basename(config, ".json");
47
await fs.rename(config, path.join(dir, `${branch}-backup-${Math.floor(Date.now() / 1000)}.json`));
48
49
await electron.dialog.showMessageBox({ message: "Configuration reset, restarting Discord." });
50
electron.app.relaunch();
51
electron.app.exit(0);
52
}
53
54
function showAbout() {
55
electron.dialog.showMessageBox({
56
title: "About moonlight",
57
message: `moonlight ${moonlightHost.branch} ${moonlightHost.version}`
58
});
59
}
60
61
electron.app.whenReady().then(() => {
62
const original = electron.Menu.buildFromTemplate;
63
electron.Menu.buildFromTemplate = function (entries) {
64
const i = entries.findIndex((e) => e.label === "Check for Updates...");
65
if (i === -1) return original.call(this, entries);
66
67
if (!entries.find((e) => e.label === "moonlight")) {
68
const options: Electron.MenuItemConstructorOptions[] = [
69
{ label: "Update and restart", click: updateAndRestart },
70
{ label: "Reset config", click: resetConfig }
71
];
72
73
if (moonlightHost.branch !== MoonlightBranch.DEV) {
74
options.push({
75
label: "Switch branch",
76
submenu: [MoonlightBranch.STABLE, MoonlightBranch.NIGHTLY].map((branch) => ({
77
label: branch,
78
type: "radio",
79
checked: moonlightHost.branch === branch,
80
click: async () => {
81
if (moonlightHost.branch === branch) return;
82
if (!(await confirm("switch branches"))) return;
83
try {
84
await natives.updateMoonlight(branch);
85
await electron.dialog.showMessageBox({ message: "Branch switch successful, restarting Discord." });
86
electron.app.relaunch();
87
electron.app.exit(0);
88
} catch (e) {
89
await electron.dialog.showMessageBox({ message: "Failed to switch branches:\n" + e, type: "error" });
90
}
91
}
92
}))
93
});
94
}
95
96
options.push({ label: "About", click: showAbout });
97
98
entries.splice(i + 1, 0, {
99
label: "moonlight",
100
submenu: options
101
});
102
}
103
104
return original.call(this, entries);
105
};
106
});
107