tabs/src/Updater.ts

69 lines
2.4 KiB
TypeScript
Raw Normal View History

2020-05-21 07:38:44 +02:00
import {autoUpdater, UpdateInfo} from "electron-updater";
2020-05-22 08:51:37 +02:00
import {dialog, shell} from "electron";
import Config from "./Config";
import BrowserWindow = Electron.BrowserWindow;
export default class Updater {
2020-05-22 08:51:37 +02:00
private readonly config: Config;
2020-05-21 07:38:44 +02:00
private updateInfo?: UpdateInfo;
2020-05-22 08:51:37 +02:00
public constructor(config: Config) {
this.config = config;
// Configure auto updater
autoUpdater.autoDownload = false;
autoUpdater.on('error', err => {
2020-05-21 07:38:44 +02:00
console.log('Error while checking for updates', err);
});
autoUpdater.on('update-available', v => {
2020-05-21 07:38:44 +02:00
console.log('Update available', v);
});
autoUpdater.on('update-not-available', () => {
2020-05-21 07:38:44 +02:00
console.log('No update available.');
});
}
2020-05-22 08:51:37 +02:00
public async checkForUpdates(force: boolean = false): Promise<UpdateInfo | void> {
if (force || !this.updateInfo) {
this.updateInfo = (await autoUpdater.checkForUpdates()).updateInfo;
}
2020-05-22 08:51:37 +02:00
if (this.updateInfo.version !== this.getCurrentVersion().raw) {
return this.updateInfo;
}
}
2020-05-22 08:51:37 +02:00
public getCurrentVersion() {
return autoUpdater.currentVersion;
}
2020-05-22 08:51:37 +02:00
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}`);
}
}
}
}