2023-04-10 22:53:44 +02:00
|
|
|
/*
|
|
|
|
* SPDX-License-Identifier: GPL-3.0
|
2023-07-13 19:03:13 +02:00
|
|
|
* Vesktop, a desktop app aiming to give you a snappier Discord Experience
|
2023-04-10 22:53:44 +02:00
|
|
|
* Copyright (c) 2023 Vendicated and Vencord contributors
|
|
|
|
*/
|
|
|
|
|
2023-08-25 15:32:06 +02:00
|
|
|
import { app, BrowserWindow, shell } from "electron";
|
2024-01-07 02:26:18 +01:00
|
|
|
import { Settings, State } from "main/settings";
|
2023-08-25 15:32:06 +02:00
|
|
|
import { handle } from "main/utils/ipcWrappers";
|
2023-06-26 01:42:51 +02:00
|
|
|
import { makeLinksOpenExternally } from "main/utils/makeLinksOpenExternally";
|
2023-04-10 22:53:44 +02:00
|
|
|
import { githubGet, ReleaseData } from "main/utils/vencordLoader";
|
|
|
|
import { join } from "path";
|
|
|
|
import { IpcEvents } from "shared/IpcEvents";
|
2023-09-25 06:49:54 +05:30
|
|
|
import { ICON_PATH, VIEW_DIR } from "shared/paths";
|
2023-04-10 22:53:44 +02:00
|
|
|
|
|
|
|
export interface UpdateData {
|
|
|
|
currentVersion: string;
|
|
|
|
latestVersion: string;
|
|
|
|
release: ReleaseData;
|
|
|
|
}
|
|
|
|
|
|
|
|
let updateData: UpdateData;
|
|
|
|
|
2023-08-25 15:32:06 +02:00
|
|
|
handle(IpcEvents.UPDATER_GET_DATA, () => updateData);
|
|
|
|
handle(IpcEvents.UPDATER_DOWNLOAD, () => {
|
2023-06-21 14:46:40 +02:00
|
|
|
const portable = !!process.env.PORTABLE_EXECUTABLE_FILE;
|
|
|
|
|
2023-04-10 22:53:44 +02:00
|
|
|
const { assets } = updateData.release;
|
|
|
|
const url = (() => {
|
|
|
|
switch (process.platform) {
|
|
|
|
case "win32":
|
2023-06-21 14:46:40 +02:00
|
|
|
return assets.find(a => {
|
|
|
|
if (!a.name.endsWith(".exe")) return false;
|
|
|
|
|
|
|
|
const isSetup = a.name.includes("Setup");
|
|
|
|
return portable ? !isSetup : isSetup;
|
|
|
|
})!.browser_download_url;
|
2023-04-10 22:53:44 +02:00
|
|
|
case "darwin":
|
2023-07-27 08:16:17 +08:00
|
|
|
return assets.find(a =>
|
|
|
|
process.arch === "arm64"
|
|
|
|
? a.name.endsWith("-arm64-mac.zip")
|
|
|
|
: a.name.endsWith("-mac.zip") && !a.name.includes("arm64")
|
|
|
|
)!.browser_download_url;
|
2023-04-10 22:53:44 +02:00
|
|
|
case "linux":
|
|
|
|
return updateData.release.html_url;
|
|
|
|
default:
|
|
|
|
throw new Error(`Unsupported platform: ${process.platform}`);
|
|
|
|
}
|
|
|
|
})();
|
|
|
|
|
|
|
|
shell.openExternal(url);
|
|
|
|
});
|
|
|
|
|
2023-08-25 15:32:06 +02:00
|
|
|
handle(IpcEvents.UPDATE_IGNORE, () => {
|
2024-01-07 02:26:18 +01:00
|
|
|
State.store.skippedUpdate = updateData.latestVersion;
|
2023-04-10 22:53:44 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
function isOutdated(oldVersion: string, newVersion: string) {
|
|
|
|
const oldParts = oldVersion.split(".");
|
|
|
|
const newParts = newVersion.split(".");
|
|
|
|
|
|
|
|
if (oldParts.length !== newParts.length)
|
|
|
|
throw new Error(`Incompatible version strings (old: ${oldVersion}, new: ${newVersion})`);
|
|
|
|
|
|
|
|
for (let i = 0; i < oldParts.length; i++) {
|
|
|
|
const oldPart = Number(oldParts[i]);
|
|
|
|
const newPart = Number(newParts[i]);
|
|
|
|
|
|
|
|
if (isNaN(oldPart) || isNaN(newPart))
|
|
|
|
throw new Error(`Invalid version string (old: ${oldVersion}, new: ${newVersion})`);
|
|
|
|
|
|
|
|
if (oldPart < newPart) return true;
|
|
|
|
if (oldPart > newPart) return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function checkUpdates() {
|
2023-10-22 23:10:25 +08:00
|
|
|
if (Settings.store.checkUpdates === false) return;
|
|
|
|
|
2023-04-10 22:53:44 +02:00
|
|
|
try {
|
2023-07-13 19:03:13 +02:00
|
|
|
const raw = await githubGet("/repos/Vencord/Vesktop/releases/latest");
|
2023-04-10 22:53:44 +02:00
|
|
|
const data = JSON.parse(raw.toString("utf-8")) as ReleaseData;
|
|
|
|
|
|
|
|
const oldVersion = app.getVersion();
|
|
|
|
const newVersion = data.tag_name.replace(/^v/, "");
|
2023-06-26 01:42:51 +02:00
|
|
|
updateData = {
|
|
|
|
currentVersion: oldVersion,
|
|
|
|
latestVersion: newVersion,
|
|
|
|
release: data
|
|
|
|
};
|
2023-04-10 22:53:44 +02:00
|
|
|
|
2024-01-07 02:26:18 +01:00
|
|
|
if (State.store.skippedUpdate !== newVersion && isOutdated(oldVersion, newVersion)) {
|
2023-04-10 22:53:44 +02:00
|
|
|
openNewUpdateWindow();
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
console.error("AppUpdater: Failed to check for updates\n", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function openNewUpdateWindow() {
|
|
|
|
const win = new BrowserWindow({
|
2023-06-26 01:42:51 +02:00
|
|
|
width: 500,
|
|
|
|
autoHideMenuBar: true,
|
|
|
|
alwaysOnTop: true,
|
2023-04-10 22:53:44 +02:00
|
|
|
webPreferences: {
|
2023-06-26 01:42:51 +02:00
|
|
|
preload: join(__dirname, "updaterPreload.js"),
|
|
|
|
nodeIntegration: false,
|
|
|
|
contextIsolation: true,
|
|
|
|
sandbox: true
|
2023-09-25 06:49:54 +05:30
|
|
|
},
|
|
|
|
icon: ICON_PATH
|
2023-04-10 22:53:44 +02:00
|
|
|
});
|
|
|
|
|
2023-06-26 01:42:51 +02:00
|
|
|
makeLinksOpenExternally(win);
|
|
|
|
|
2023-06-23 17:20:54 +02:00
|
|
|
win.loadFile(join(VIEW_DIR, "updater.html"));
|
2023-04-10 22:53:44 +02:00
|
|
|
}
|