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() {} }