79 lines
2.4 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
6
const natives = getNatives();
7
8
const confirm = (action: string) =>
9
electron.dialog
10
.showMessageBox({
11
title: "Are you sure?",
12
message: `Are you sure? This will ${action} and restart Discord.`,
13
type: "warning",
14
buttons: ["OK", "Cancel"]
15
})
16
.then((r) => r.response === 0);
17
18
async function updateAndRestart() {
19
if (!(await confirm("update moonlight"))) return;
20
const newVersion = await natives.checkForMoonlightUpdate();
21
22
if (newVersion === null) {
23
electron.dialog.showMessageBox({ message: "You are already on the latest version of moonlight." });
24
return;
25
}
26
27
try {
28
await natives.updateMoonlight();
29
await electron.dialog.showMessageBox({ message: "Update successful, restarting Discord." });
30
electron.app.relaunch();
31
electron.app.exit(0);
32
} catch {
33
await electron.dialog.showMessageBox({
34
message: "Failed to update moonlight. Please use the installer instead.",
35
type: "error"
36
});
37
}
38
}
39
40
async function resetConfig() {
41
if (!(await confirm("reset your configuration"))) return;
42
43
const config = await moonlightHost.getConfigPath();
44
const dir = path.dirname(config);
45
const branch = path.basename(config, ".json");
46
await fs.rename(config, path.join(dir, `${branch}-backup-${Math.floor(Date.now() / 1000)}.json`));
47
48
await electron.dialog.showMessageBox({ message: "Configuration reset, restarting Discord." });
49
electron.app.relaunch();
50
electron.app.exit(0);
51
}
52
53
function showAbout() {
54
electron.dialog.showMessageBox({
55
title: "About moonlight",
56
message: `moonlight ${moonlightHost.branch} ${moonlightHost.version}`
57
});
58
}
59
60
electron.app.whenReady().then(() => {
61
const original = electron.Menu.buildFromTemplate;
62
electron.Menu.buildFromTemplate = function (entries) {
63
const i = entries.findIndex((e) => e.label === "Check for Updates...");
64
if (i === -1) return original.call(this, entries);
65
66
if (!entries.find((e) => e.label === "moonlight")) {
67
entries.splice(i + 1, 0, {
68
label: "moonlight",
69
submenu: [
70
{ label: "Update and restart", click: updateAndRestart },
71
{ label: "Reset config", click: resetConfig },
72
{ label: "About", click: showAbout }
73
]
74
});
75
}
76
77
return original.call(this, entries);
78
};
79
});
80