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"; 5import { MoonlightBranch } from "@moonlight-mod/types"; 6 7const natives = getNatives(); 8 9const 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 19async 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 41async 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 54async function changeBranch(branch: MoonlightBranch) { 55 if (moonlightHost.branch === branch) return; 56 if (!(await confirm("switch branches"))) return; 57 try { 58 await natives.updateMoonlight(branch); 59 await electron.dialog.showMessageBox({ message: "Branch switch successful, restarting Discord." }); 60 electron.app.relaunch(); 61 electron.app.exit(0); 62 } catch (e) { 63 await electron.dialog.showMessageBox({ message: "Failed to switch branches:\n" + e, type: "error" }); 64 } 65} 66 67function showAbout() { 68 electron.dialog.showMessageBox({ 69 title: "About moonlight", 70 message: `moonlight ${moonlightHost.branch} ${moonlightHost.version}` 71 }); 72} 73 74electron.app.whenReady().then(() => { 75 const original = electron.Menu.buildFromTemplate; 76 electron.Menu.buildFromTemplate = function (entries) { 77 const i = entries.findIndex((e) => e.label === "Check for Updates..."); 78 if (i === -1) return original.call(this, entries); 79 80 if (!entries.find((e) => e.label === "moonlight")) { 81 const options: Electron.MenuItemConstructorOptions[] = [ 82 { label: "Update and restart", click: updateAndRestart }, 83 { label: "Reset config", click: resetConfig } 84 ]; 85 86 if (moonlightHost.branch !== MoonlightBranch.DEV) { 87 options.push({ 88 label: "Switch branch", 89 submenu: [MoonlightBranch.STABLE, MoonlightBranch.NIGHTLY].map((branch) => ({ 90 label: branch, 91 type: "radio", 92 checked: moonlightHost.branch === branch, 93 click: () => changeBranch(branch) 94 })) 95 }); 96 } 97 98 options.push({ label: "About", click: showAbout }); 99 100 entries.splice(i + 1, 0, { 101 label: "moonlight", 102 submenu: options 103 }); 104 } 105 106 return original.call(this, entries); 107 }; 108});