gui/modules/desktop/electron/libs/auto-updater.ts
ABevier 544cf09b7c
Auto updates on the top bar (#488)
* Auto updates on the top bar

---------

Co-authored-by: neil molina <neil@neils-MacBook-Pro.local>
2023-04-22 10:42:00 +08:00

61 lines
1.9 KiB
TypeScript

import { type AppUpdater, autoUpdater } from "electron-updater";
import * as log from "electron-log";
import { BrowserWindow } from "electron";
type AutoUpdateStatus = {
status: "up-to-date" | "available" | "ready";
version?: string;
};
autoUpdater.logger = log;
let window: BrowserWindow;
// keep the last status to resend to the window when it's opened becuase the store is destroyed when the window is closed
let lastStatus: AutoUpdateStatus = { status: "up-to-date" };
export const getUpdater = () => autoUpdater;
export function checkUpdater(mainWindow: BrowserWindow): AppUpdater {
window = mainWindow;
autoUpdater.checkForUpdatesAndNotify();
return autoUpdater;
}
// The auto update runs in the background so the window might not be open when the status changes
// When the update store gets created as part of the window it will request the latest status.
export function getAutoUpdateStatus() {
return lastStatus;
}
function sendStatusToWindow(status: AutoUpdateStatus) {
lastStatus = status;
window?.webContents.send("app-update-status", status);
}
autoUpdater.on("checking-for-update", () => {
log.info("checking for tea gui update");
});
autoUpdater.on("update-available", (info) => {
sendStatusToWindow({ status: "available" });
});
autoUpdater.on("update-not-available", () => {
log.info("no update for tea gui");
sendStatusToWindow({ status: "up-to-date" });
});
autoUpdater.on("error", (err) => {
log.error("auto update:", err);
});
autoUpdater.on("download-progress", (progressObj) => {
let log_message = "Download speed: " + progressObj.bytesPerSecond;
log_message = log_message + " - Downloaded " + progressObj.percent + "%";
log_message = log_message + " (" + progressObj.transferred + "/" + progressObj.total + ")";
log.info("tea gui:", log_message);
});
autoUpdater.on("update-downloaded", (info) => {
sendStatusToWindow({ status: "ready", version: info.version });
});