2022-09-30 18:42:50 -04:00
|
|
|
import IpcEvents from "./IpcEvents";
|
|
|
|
import Logger from "./logger";
|
|
|
|
import { IpcRes } from './types';
|
2022-10-05 10:47:28 -04:00
|
|
|
import gitHash from 'git-hash';
|
2022-09-30 18:42:50 -04:00
|
|
|
|
|
|
|
export const UpdateLogger = new Logger("Updater", "white");
|
|
|
|
export let isOutdated = 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() {
|
|
|
|
changes = await Unwrap(VencordNative.ipc.invoke<IpcRes<typeof changes>>(IpcEvents.GET_UPDATES));
|
2022-10-05 10:47:28 -04:00
|
|
|
if (changes.some(c => c.hash === gitHash)) {
|
|
|
|
// git log NEWER...OLDER works as well. This means it will return the more recent local commits if the
|
|
|
|
changes = [];
|
|
|
|
}
|
2022-09-30 18:42:50 -04:00
|
|
|
return (isOutdated = changes.length > 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function update() {
|
|
|
|
if (!isOutdated) return true;
|
|
|
|
|
|
|
|
const res = await Unwrap(VencordNative.ipc.invoke<IpcRes<boolean>>(IpcEvents.UPDATE));
|
|
|
|
|
|
|
|
if (res)
|
|
|
|
isOutdated = false;
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getRepo() {
|
|
|
|
return Unwrap(VencordNative.ipc.invoke<IpcRes<string>>(IpcEvents.GET_REPO));
|
|
|
|
}
|
|
|
|
|
|
|
|
type Hashes = Record<"patcher.js" | "preload.js" | "renderer.js", string>;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @returns true if hard restart is required
|
|
|
|
*/
|
|
|
|
export async function rebuild() {
|
|
|
|
const oldHashes = await Unwrap(VencordNative.ipc.invoke<IpcRes<Hashes>>(IpcEvents.GET_HASHES));
|
|
|
|
|
|
|
|
if (!await Unwrap(VencordNative.ipc.invoke<IpcRes<boolean>>(IpcEvents.BUILD)))
|
|
|
|
throw new Error("The Build failed. Please try manually building the new update");
|
|
|
|
|
|
|
|
const newHashes = await Unwrap(VencordNative.ipc.invoke<IpcRes<Hashes>>(IpcEvents.GET_HASHES));
|
|
|
|
|
|
|
|
return oldHashes["patcher.js"] !== newHashes["patcher.js"] ||
|
|
|
|
oldHashes["preload.js"] !== newHashes["preload.js"];
|
|
|
|
}
|