2020-05-21 07:38:44 +02:00
|
|
|
import {autoUpdater, UpdateInfo} from "electron-updater";
|
2020-09-29 20:43:41 +02:00
|
|
|
import {dialog} from "electron";
|
2020-05-22 08:51:37 +02:00
|
|
|
import Config from "./Config";
|
2020-06-15 14:48:52 +02:00
|
|
|
import Application from "./Application";
|
2020-09-29 20:43:41 +02:00
|
|
|
import {SemVer} from "semver";
|
|
|
|
import BrowserWindow = Electron.BrowserWindow;
|
2020-05-19 10:54:00 +02:00
|
|
|
|
|
|
|
export default class Updater {
|
2020-05-22 08:51:37 +02:00
|
|
|
private readonly config: Config;
|
2020-06-15 14:48:52 +02:00
|
|
|
private readonly application: Application;
|
2020-05-21 07:38:44 +02:00
|
|
|
private updateInfo?: UpdateInfo;
|
2020-05-19 10:54:00 +02:00
|
|
|
|
2020-06-15 14:48:52 +02:00
|
|
|
public constructor(config: Config, application: Application) {
|
2020-05-22 08:51:37 +02:00
|
|
|
this.config = config;
|
2020-06-15 14:48:52 +02:00
|
|
|
this.application = application;
|
2020-05-22 08:51:37 +02:00
|
|
|
|
|
|
|
// Configure auto updater
|
2020-05-19 10:54:00 +02:00
|
|
|
autoUpdater.autoDownload = false;
|
|
|
|
autoUpdater.on('error', err => {
|
2020-05-21 07:38:44 +02:00
|
|
|
console.log('Error while checking for updates', err);
|
2020-05-19 10:54:00 +02:00
|
|
|
});
|
|
|
|
autoUpdater.on('update-available', v => {
|
2020-05-21 07:38:44 +02:00
|
|
|
console.log('Update available', v);
|
2020-05-19 10:54:00 +02:00
|
|
|
});
|
|
|
|
autoUpdater.on('update-not-available', () => {
|
2020-05-21 07:38:44 +02:00
|
|
|
console.log('No update available.');
|
2020-05-19 10:54:00 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
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-19 11:21:12 +02:00
|
|
|
}
|
|
|
|
|
2020-05-22 08:51:37 +02:00
|
|
|
if (this.updateInfo.version !== this.getCurrentVersion().raw) {
|
|
|
|
return this.updateInfo;
|
|
|
|
}
|
2020-05-19 10:54:00 +02:00
|
|
|
}
|
|
|
|
|
2020-09-29 20:43:41 +02:00
|
|
|
public getCurrentVersion(): SemVer {
|
2020-05-19 10:54:00 +02:00
|
|
|
return autoUpdater.currentVersion;
|
|
|
|
}
|
2020-05-19 11:21:12 +02:00
|
|
|
|
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, {
|
2020-06-15 14:48:52 +02:00
|
|
|
message: `Version ${updateInfo.version} of tabs is available. Do you wish to install this update?`,
|
2020-05-22 08:51:37 +02:00
|
|
|
buttons: [
|
|
|
|
'Cancel',
|
2020-06-15 14:48:52 +02:00
|
|
|
'Install',
|
2020-05-22 08:51:37 +02:00
|
|
|
],
|
|
|
|
checkboxChecked: false,
|
|
|
|
checkboxLabel: `Don't remind me for this version`,
|
|
|
|
cancelId: 0,
|
|
|
|
defaultId: 1,
|
2020-09-29 20:43:41 +02:00
|
|
|
type: 'question',
|
2020-05-22 08:51:37 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
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) {
|
2020-06-15 14:48:52 +02:00
|
|
|
await this.application.stop();
|
|
|
|
await autoUpdater.downloadUpdate();
|
|
|
|
autoUpdater.quitAndInstall();
|
2020-05-22 08:51:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|