Refactor: auto-update functionality with user prompts and download progress

This commit is contained in:
MasterAcnolo
2026-04-12 16:57:39 +02:00
parent 011e0a703a
commit 75fdf91fcd
6 changed files with 106 additions and 42 deletions

View File

@@ -1,47 +1,83 @@
const { autoUpdater } = require("electron-updater");
const { app, Notification, shell} = require("electron");
const { dialog } = require("electron");
const { logger } = require("../server/logger");
function AutoUpdater() {
autoUpdater.autoDownload = false;
autoUpdater.autoInstallOnAppQuit = false;
autoUpdater.on("update-available", (info) => {
logger.info(`New Version Available : ${info.version}`);
new Notification({
title: "Freedom Loader",
body: `New Version Available : ${info.version}. Application will restart`
}).show();
function initAutoUpdater(mainWindow) {
autoUpdater.on("update-available", async (info) => {
logger.info(`Update available: ${info.version}`);
const { response } = await dialog.showMessageBox(mainWindow, {
type: "info",
title: "Update Available",
message: `Version ${info.version} is available.`,
detail: "Would you like to download and install it now?",
buttons: ["Install Update", "Maybe Later"],
defaultId: 0,
cancelId: 1,
});
if (response === 0) {
autoUpdater.downloadUpdate();
} else {
mainWindow?.webContents.executeJavaScript(
`window.showUpdateBadge && window.showUpdateBadge("${info.version}")`
);
}
});
autoUpdater.on("update-downloaded", (info) => {
logger.info(`Update Downloaded : ${info.version}`);
new Notification({
title: "Freedom Loader",
body: `Update ${info.version} downloaded.`
}).on("click", () =>
shell.openExternal("https://github.com/MasterAcnolo/Freedom-Loader/releases/latest")
).show();
autoUpdater.on("download-progress", (progress) => {
logger.info(`Download progress: ${Math.round(progress.percent)}%`);
mainWindow?.webContents.send("update-progress", {
percent: Math.round(progress.percent),
speed: progress.bytesPerSecond,
});
});
setTimeout(() => {
autoUpdater.quitAndInstall();
}, 5000);
autoUpdater.on("update-downloaded", async (info) => {
logger.info(`Update downloaded: ${info.version}`);
const { response } = await dialog.showMessageBox(mainWindow, {
type: "info",
title: "Update Ready",
message: `Version ${info.version} has been downloaded.`,
detail: "The application will restart to apply the update.",
buttons: ["Restart & Install", "Later"],
defaultId: 0,
cancelId: 1,
});
if (response === 0) autoUpdater.quitAndInstall();
});
autoUpdater.on("error", (err) => {
logger.error("Auto Update Error :", err.message);
new Notification({
title: "Freedom Loader - Update Error",
body: err.message
}).show();
logger.error("Auto update error:", err.message);
dialog.showErrorBox("Update Error", err.message);
});
app.whenReady().then(async () => {
try {
await autoUpdater.checkForUpdates();
logger.info("Update check completed");
} catch (err) {
logger.error("Error during update check :", err.message);
}
});
checkForUpdates();
}
module.exports = { AutoUpdater };
async function checkForUpdates() {
try {
await autoUpdater.checkForUpdates();
} catch (err) {
logger.error("Update check failed:", err.message);
}
}
async function downloadUpdate() {
try {
await autoUpdater.downloadUpdate();
} catch (err) {
logger.error("Download failed:", err.message);
}
}
function installUpdate() {
autoUpdater.quitAndInstall();
}
module.exports = { initAutoUpdater, downloadUpdate, installUpdate };