Refactor Updater
This commit is contained in:
parent
8683140003
commit
8c5674447c
@ -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<UpdateInfo | void> {
|
||||
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<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}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
38
src/main.ts
38
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;
|
||||
|
Loading…
Reference in New Issue
Block a user