From 8c5674447ce6363f546928f609eca3d9e29a6d77 Mon Sep 17 00:00:00 2001 From: Alice Gaudon Date: Fri, 22 May 2020 08:51:37 +0200 Subject: [PATCH] Refactor Updater --- src/Updater.ts | 61 ++++++++++++++++++++++++++++++++++++-------------- src/main.ts | 38 +++++++------------------------ 2 files changed, 52 insertions(+), 47 deletions(-) diff --git a/src/Updater.ts b/src/Updater.ts index 5cece7e..6e82d58 100644 --- a/src/Updater.ts +++ b/src/Updater.ts @@ -1,9 +1,16 @@ import {autoUpdater, UpdateInfo} from "electron-updater"; +import {dialog, shell} from "electron"; +import Config from "./Config"; +import BrowserWindow = Electron.BrowserWindow; export default class Updater { + private readonly config: Config; private updateInfo?: UpdateInfo; - constructor() { + public constructor(config: Config) { + this.config = config; + + // Configure auto updater autoUpdater.autoDownload = false; autoUpdater.on('error', err => { console.log('Error while checking for updates', err); @@ -16,26 +23,46 @@ export default class Updater { }); } - /** - * @param {Function} callback - */ - checkForUpdates(callback: UpdateCheckCallback) { - if (this.updateInfo) { - callback(this.updateInfo.version !== this.getCurrentVersion().raw, this.updateInfo); - return; + public async checkForUpdates(force: boolean = false): Promise { + if (force || !this.updateInfo) { + this.updateInfo = (await autoUpdater.checkForUpdates()).updateInfo; } - autoUpdater.checkForUpdates().then(r => { - this.updateInfo = r.updateInfo; - callback(r.updateInfo.version !== this.getCurrentVersion().raw, r.updateInfo); - }).catch(err => { - callback(false, err); - }); + if (this.updateInfo.version !== this.getCurrentVersion().raw) { + return this.updateInfo; + } } - getCurrentVersion() { + public getCurrentVersion() { return autoUpdater.currentVersion; } -} -export type UpdateCheckCallback = (available: boolean, data: UpdateInfo) => void; + public async checkAndPromptForUpdates(mainWindow: BrowserWindow): Promise { + const updateInfo = await this.checkForUpdates(true); + + if (updateInfo && updateInfo.version !== this.config.updateCheckSkip) { + const input = await dialog.showMessageBox(mainWindow, { + message: `Version ${updateInfo.version} of tabs is available. Do you wish to download this update?`, + buttons: [ + 'Cancel', + 'Download', + ], + checkboxChecked: false, + checkboxLabel: `Don't remind me for this version`, + cancelId: 0, + defaultId: 1, + type: 'question' + }); + + if (input.checkboxChecked) { + console.log('Skipping update download prompt for version', updateInfo.version); + this.config.updateCheckSkip = updateInfo.version; + this.config.save(); + } + + if (input.response === 1) { + await shell.openExternal(`https://github.com/ArisuOngaku/tabs/releases/download/v${updateInfo.version}/${updateInfo.path}`); + } + } + } +} diff --git a/src/main.ts b/src/main.ts index f8950fa..b82e435 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,7 +1,7 @@ import fs from "fs"; import path from "path"; import SingleInstance from "single-instance"; -import {app, BrowserWindow, dialog, ipcMain, Menu, shell, Tray} from "electron"; +import {app, BrowserWindow, ipcMain, Menu, shell, Tray} from "electron"; import Meta from "./Meta"; import Config from "./Config"; @@ -13,7 +13,7 @@ const resourcesDir = path.resolve(__dirname, '../resources'); const iconPath = path.resolve(resourcesDir, 'logo.png'); const config = new Config(); -const updater = new Updater(); +const updater = new Updater(config); const devMode = Meta.isDevMode(); @@ -41,31 +41,9 @@ function toggleMainWindow() { async function createWindow() { // Check for updates - updater.checkForUpdates((available, updateInfo) => { - if (available && updateInfo.version !== config.updateCheckSkip) { - dialog.showMessageBox(window!, { - message: `Version ${updateInfo.version} of tabs is available. Do you wish to download this update?`, - buttons: [ - 'Cancel', - 'Download', - ], - checkboxChecked: false, - checkboxLabel: `Don't remind me for this version`, - cancelId: 0, - defaultId: 1, - type: 'question' - }).then(e => { - if (e.checkboxChecked) { - console.log('Skipping update check for version', updateInfo.version); - config.updateCheckSkip = updateInfo.version; - config.save(); - } - if (e.response === 1) { - return shell.openExternal(`https://github.com/ArisuOngaku/tabs/releases/download/v${updateInfo.version}/${updateInfo.path}`); - } - }).catch(console.error); - } - }); + updater.checkAndPromptForUpdates(window!).then(() => { + console.log('Update check successful.'); + }).catch(console.error); // System tray console.log('Loading system Tray'); @@ -259,9 +237,9 @@ async function createWindow() { let checkForUpdatesListener: () => void; ipcMain.on('checkForUpdates', checkForUpdatesListener = () => { - updater.checkForUpdates((available, version) => { - settingsWindow!.webContents.send('updateStatus', available, version); - }); + updater.checkForUpdates().then(updateInfo => { + settingsWindow!.webContents.send('updateStatus', typeof updateInfo === 'object', updateInfo); + }).catch(console.error); }); let saveConfigListener: (e: Event, data: any) => void;