2022-10-21 19:17:06 -04:00
|
|
|
/*
|
|
|
|
* Vencord, a modification for Discord's desktop app
|
|
|
|
* Copyright (c) 2022 Vendicated and contributors
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2022-10-23 17:23:52 -04:00
|
|
|
import gitHash from "~git-hash";
|
2022-10-22 12:18:41 -04:00
|
|
|
|
2023-05-05 19:36:00 -04:00
|
|
|
import { Logger } from "./Logger";
|
2023-04-03 19:16:29 -04:00
|
|
|
import { relaunch } from "./native";
|
2022-10-05 18:42:58 -04:00
|
|
|
import { IpcRes } from "./types";
|
2022-09-30 18:42:50 -04:00
|
|
|
|
2023-01-23 19:42:57 -05:00
|
|
|
export const UpdateLogger = /* #__PURE__*/ new Logger("Updater", "white");
|
2022-09-30 18:42:50 -04:00
|
|
|
export let isOutdated = false;
|
2022-10-05 11:09:37 -04:00
|
|
|
export let isNewer = false;
|
2022-10-01 20:46:41 -04:00
|
|
|
export let updateError: any;
|
2022-09-30 18:42:50 -04:00
|
|
|
export let changes: Record<"hash" | "author" | "message", string>[];
|
|
|
|
|
|
|
|
async function Unwrap<T>(p: Promise<IpcRes<T>>) {
|
|
|
|
const res = await p;
|
|
|
|
|
|
|
|
if (res.ok) return res.value;
|
2022-10-01 20:46:41 -04:00
|
|
|
|
|
|
|
updateError = res.error;
|
2022-09-30 18:42:50 -04:00
|
|
|
throw res.error;
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function checkForUpdates() {
|
2023-05-01 20:50:51 -04:00
|
|
|
changes = await Unwrap(VencordNative.updater.getUpdates());
|
2022-10-05 10:47:28 -04:00
|
|
|
if (changes.some(c => c.hash === gitHash)) {
|
2022-10-05 11:09:37 -04:00
|
|
|
isNewer = true;
|
|
|
|
return (isOutdated = false);
|
2022-10-05 10:47:28 -04:00
|
|
|
}
|
2022-09-30 18:42:50 -04:00
|
|
|
return (isOutdated = changes.length > 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function update() {
|
|
|
|
if (!isOutdated) return true;
|
|
|
|
|
2023-05-01 20:50:51 -04:00
|
|
|
const res = await Unwrap(VencordNative.updater.update());
|
2022-09-30 18:42:50 -04:00
|
|
|
|
2023-05-01 20:50:51 -04:00
|
|
|
if (res) {
|
2022-09-30 18:42:50 -04:00
|
|
|
isOutdated = false;
|
2023-05-01 20:50:51 -04:00
|
|
|
if (!await Unwrap(VencordNative.updater.rebuild()))
|
|
|
|
throw new Error("The Build failed. Please try manually building the new update");
|
|
|
|
}
|
2022-09-30 18:42:50 -04:00
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2023-05-01 20:50:51 -04:00
|
|
|
export const getRepo = () => Unwrap(VencordNative.updater.getRepo());
|
2023-02-28 23:26:13 -05:00
|
|
|
|
|
|
|
export async function maybePromptToUpdate(confirmMessage: string, checkForDev = false) {
|
|
|
|
if (IS_WEB) return;
|
|
|
|
if (checkForDev && IS_DEV) return;
|
|
|
|
|
|
|
|
try {
|
|
|
|
const isOutdated = await checkForUpdates();
|
|
|
|
if (isOutdated) {
|
|
|
|
const wantsUpdate = confirm(confirmMessage);
|
|
|
|
if (wantsUpdate && isNewer) return alert("Your local copy has more recent commits. Please stash or reset them.");
|
|
|
|
if (wantsUpdate) {
|
|
|
|
await update();
|
2023-04-08 22:04:02 -04:00
|
|
|
relaunch();
|
2023-02-28 23:26:13 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
UpdateLogger.error(err);
|
|
|
|
alert("That also failed :( Try updating or re-installing with the installer!");
|
|
|
|
}
|
|
|
|
}
|