ucap-lg-desktop/src/app/auto-updater.ts

74 lines
2.1 KiB
TypeScript
Raw Normal View History

2020-08-10 14:05:14 +09:00
import * as Electron from 'electron';
import log from 'electron-log';
import {
autoUpdater,
CancellationToken,
UpdateCheckResult
} from 'electron-updater';
import { AutoUpdaterApi } from '@ucap/electron-common';
import {
AutoUpdaterChannel,
BrowserWindowUtil,
BrowserWindowChannel
} from '@ucap/electron-core';
import { AppChannel } from '@ucap/electron-core';
@AutoUpdaterApi.AutoUpdaterSettings({})
export class AutoUpdater extends AutoUpdaterApi.ElectronAutoUpdater {
@AutoUpdaterApi.On(AutoUpdaterChannel.checkingForUpdate)
onCheckingForUpdate() {
log.info('Checking for update...');
}
@AutoUpdaterApi.On(AutoUpdaterChannel.updateAvailable)
onUpdateAvailable(info: any) {
log.info('Update available.', info);
}
@AutoUpdaterApi.On(AutoUpdaterChannel.updateNotAvailable)
onUpdateNotAvailable() {
log.info('Update not available.');
}
@AutoUpdaterApi.On(AutoUpdaterChannel.error)
onError(err) {
log.info('Error in autoUpdater. ' + err);
}
@AutoUpdaterApi.On(AutoUpdaterChannel.downloadProgress)
onDownloadProgress(progress: any) {
let logMessage = 'Download speed: ' + progress.bytesPerSecond;
logMessage = logMessage + ' - Downloaded ' + progress.percent + '%';
logMessage =
logMessage + ' (' + progress.transferred + '/' + progress.total + ')';
log.info(logMessage);
}
@AutoUpdaterApi.On(AutoUpdaterChannel.updateDownloaded)
onUpdateDownloaded() {
log.info('Update downloaded');
Electron.app.removeAllListeners(AppChannel.windowAllClosed);
const browserWindows = BrowserWindowUtil.all();
// https://github.com/electron-userland/electron-builder/issues/1604#issuecomment-372091881
browserWindows.forEach((browserWindow) => {
browserWindow.removeAllListeners(BrowserWindowChannel.close);
browserWindow.removeAllListeners(BrowserWindowChannel.closed);
});
setTimeout(() => {
autoUpdater.quitAndInstall(true, true);
}, 2000);
}
@AutoUpdaterApi.On(AutoUpdaterChannel.updateCancelled)
onUpdateCancelled() {}
@AutoUpdaterApi.On(AutoUpdaterChannel.beforeQuitForUpdate)
onBeforeQuitForUpdate() {}
}