Refactor Updater

This commit is contained in:
Alice Gaudon 2020-05-22 08:51:37 +02:00
parent 8683140003
commit 8c5674447c
2 changed files with 52 additions and 47 deletions

View File

@ -1,9 +1,16 @@
import {autoUpdater, UpdateInfo} from "electron-updater"; import {autoUpdater, UpdateInfo} from "electron-updater";
import {dialog, shell} from "electron";
import Config from "./Config";
import BrowserWindow = Electron.BrowserWindow;
export default class Updater { export default class Updater {
private readonly config: Config;
private updateInfo?: UpdateInfo; private updateInfo?: UpdateInfo;
constructor() { public constructor(config: Config) {
this.config = config;
// Configure auto updater
autoUpdater.autoDownload = false; autoUpdater.autoDownload = false;
autoUpdater.on('error', err => { autoUpdater.on('error', err => {
console.log('Error while checking for updates', err); console.log('Error while checking for updates', err);
@ -16,26 +23,46 @@ export default class Updater {
}); });
} }
/** public async checkForUpdates(force: boolean = false): Promise<UpdateInfo | void> {
* @param {Function} callback if (force || !this.updateInfo) {
*/ this.updateInfo = (await autoUpdater.checkForUpdates()).updateInfo;
checkForUpdates(callback: UpdateCheckCallback) {
if (this.updateInfo) {
callback(this.updateInfo.version !== this.getCurrentVersion().raw, this.updateInfo);
return;
} }
autoUpdater.checkForUpdates().then(r => { if (this.updateInfo.version !== this.getCurrentVersion().raw) {
this.updateInfo = r.updateInfo; return this.updateInfo;
callback(r.updateInfo.version !== this.getCurrentVersion().raw, r.updateInfo); }
}).catch(err => {
callback(false, err);
});
} }
getCurrentVersion() { public getCurrentVersion() {
return autoUpdater.currentVersion; return autoUpdater.currentVersion;
} }
}
export type UpdateCheckCallback = (available: boolean, data: UpdateInfo) => void; public async checkAndPromptForUpdates(mainWindow: BrowserWindow): Promise<void> {
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}`);
}
}
}
}

View File

@ -1,7 +1,7 @@
import fs from "fs"; import fs from "fs";
import path from "path"; import path from "path";
import SingleInstance from "single-instance"; 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 Meta from "./Meta";
import Config from "./Config"; import Config from "./Config";
@ -13,7 +13,7 @@ const resourcesDir = path.resolve(__dirname, '../resources');
const iconPath = path.resolve(resourcesDir, 'logo.png'); const iconPath = path.resolve(resourcesDir, 'logo.png');
const config = new Config(); const config = new Config();
const updater = new Updater(); const updater = new Updater(config);
const devMode = Meta.isDevMode(); const devMode = Meta.isDevMode();
@ -41,31 +41,9 @@ function toggleMainWindow() {
async function createWindow() { async function createWindow() {
// Check for updates // Check for updates
updater.checkForUpdates((available, updateInfo) => { updater.checkAndPromptForUpdates(window!).then(() => {
if (available && updateInfo.version !== config.updateCheckSkip) { console.log('Update check successful.');
dialog.showMessageBox(window!, { }).catch(console.error);
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);
}
});
// System tray // System tray
console.log('Loading system Tray'); console.log('Loading system Tray');
@ -259,9 +237,9 @@ async function createWindow() {
let checkForUpdatesListener: () => void; let checkForUpdatesListener: () => void;
ipcMain.on('checkForUpdates', checkForUpdatesListener = () => { ipcMain.on('checkForUpdates', checkForUpdatesListener = () => {
updater.checkForUpdates((available, version) => { updater.checkForUpdates().then(updateInfo => {
settingsWindow!.webContents.send('updateStatus', available, version); settingsWindow!.webContents.send('updateStatus', typeof updateInfo === 'object', updateInfo);
}); }).catch(console.error);
}); });
let saveConfigListener: (e: Event, data: any) => void; let saveConfigListener: (e: Event, data: any) => void;