this repo has no description
1import * as electron from "electron";
2import * as fs from "node:fs/promises";
3import * as path from "node:path";
4import getNatives from "./native";
5
6const natives = getNatives();
7
8const 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
18async 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
40async 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
53function showAbout() {
54 electron.dialog.showMessageBox({
55 title: "About moonlight",
56 message: `moonlight ${moonlightHost.branch} ${moonlightHost.version}`
57 });
58}
59
60electron.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 entries.splice(i + 1, 0, {
67 label: "moonlight",
68 submenu: [
69 { label: "Update and restart", click: updateAndRestart },
70 { label: "Reset config", click: resetConfig },
71 { label: "About", click: showAbout }
72 ]
73 });
74
75 return original.call(this, entries);
76 };
77});