partey_workadventure/desktop/electron/src/auto-updater.ts

97 lines
2.6 KiB
TypeScript
Raw Normal View History

2022-02-17 18:09:57 +01:00
import { app, dialog } from "electron";
import { autoUpdater } from "electron-updater";
import log from "electron-log";
import * as isDev from "electron-is-dev";
import * as util from "util";
import { createAndShowNotification } from "./notification";
const sleep = util.promisify(setTimeout);
let isCheckPending = false;
let isManualRequestedUpdate = false;
export async function checkForUpdates() {
2022-02-18 23:03:05 +01:00
if (isCheckPending) {
return;
}
2022-02-17 18:09:57 +01:00
2022-02-18 23:03:05 +01:00
// Don't do auto-updates in development
if (isDev) {
return;
}
2022-02-17 18:09:57 +01:00
2022-02-18 23:03:05 +01:00
// check for updates right away
await autoUpdater.checkForUpdates();
2022-02-17 18:09:57 +01:00
2022-02-18 23:03:05 +01:00
isCheckPending = false;
2022-02-17 18:09:57 +01:00
}
export async function manualRequestUpdateCheck() {
2022-02-18 23:03:05 +01:00
isManualRequestedUpdate = true;
2022-02-17 18:09:57 +01:00
2022-02-18 23:03:05 +01:00
createAndShowNotification({
body: "Checking for updates ...",
});
2022-02-17 18:09:57 +01:00
2022-02-18 23:03:05 +01:00
await checkForUpdates();
isManualRequestedUpdate = false;
2022-02-17 18:09:57 +01:00
}
2022-02-17 19:48:08 +01:00
function init() {
2022-02-18 23:03:05 +01:00
autoUpdater.logger = log;
autoUpdater.on("update-downloaded", ({ releaseNotes, releaseName }) => {
(async () => {
const dialogOpts = {
type: "question",
buttons: ["Install and Restart", "Install Later"],
defaultId: 0,
title: "WorkAdventure - Update",
message: process.platform === "win32" ? releaseNotes : releaseName,
detail: "A new version has been downloaded. Restart the application to apply the updates.",
};
const { response } = await dialog.showMessageBox(dialogOpts);
if (response === 0) {
await sleep(1000);
autoUpdater.quitAndInstall();
// Force app to quit. This is just a workaround, ideally autoUpdater.quitAndInstall() should relaunch the app.
// app.confirmedExitPrompt = true;
app.quit();
}
})();
2022-02-17 18:09:57 +01:00
});
2022-02-18 23:03:05 +01:00
if (process.platform === "linux" && !process.env.APPIMAGE) {
autoUpdater.autoDownload = false;
autoUpdater.autoInstallOnAppQuit = false;
autoUpdater.on("update-available", () => {
createAndShowNotification({
title: "WorkAdventure - Update available",
body: "Please go to our website and install the newest version",
});
});
2022-02-17 18:09:57 +01:00
}
2022-02-18 23:03:05 +01:00
autoUpdater.on("update-not-available", () => {
if (isManualRequestedUpdate) {
createAndShowNotification({
body: "No update available.",
});
}
});
checkForUpdates();
2022-02-17 18:09:57 +01:00
2022-02-18 23:03:05 +01:00
// run update check every hour again
setInterval(() => checkForUpdates, 1000 * 60 * 1);
2022-02-17 18:09:57 +01:00
}
2022-02-17 19:48:08 +01:00
export default {
2022-02-18 23:03:05 +01:00
init,
2022-02-17 19:48:08 +01:00
};