From da34b709edb08915aef1f89696ee2b4a8bfdd385 Mon Sep 17 00:00:00 2001 From: Alexander Drozdov Date: Tue, 24 Mar 2020 20:21:38 +0200 Subject: [PATCH] rework updater --- src/main/tray.ts | 12 ++++----- src/main/updates.ts | 63 +++++++++++++++++++++++++++++---------------- 2 files changed, 47 insertions(+), 28 deletions(-) diff --git a/src/main/tray.ts b/src/main/tray.ts index 5a10fd58..ba8e0e06 100644 --- a/src/main/tray.ts +++ b/src/main/tray.ts @@ -1,6 +1,6 @@ import path from 'path' import { app, Tray, Menu, ipcMain, MenuItem, MenuItemConstructorOptions, shell, nativeImage } from 'electron' -import { checkForUpdates } from './updates' +import { checkForUpdates, UpdateState } from './updates' import { config } from './config' import { win } from './window' import { League } from '@/ipc/types' @@ -55,7 +55,7 @@ export function createTray () { rebuildContextMenu() } -function rebuildContextMenu () { +export function rebuildContextMenu () { const contextMenu = Menu.buildFromTemplate([ ...leaguesMenuItem(), { @@ -70,10 +70,10 @@ function rebuildContextMenu () { enabled: false }, { - label: 'Check for updates', - click: () => { - checkForUpdates(true) - } + label: UpdateState.canCheck ? 'Check for updates' : UpdateState.status, + sublabel: UpdateState.canCheck ? UpdateState.status : undefined, + enabled: UpdateState.canCheck, + click: checkForUpdates }, { label: 'Open data folder', diff --git a/src/main/updates.ts b/src/main/updates.ts index a2e0cce9..c5da89c0 100644 --- a/src/main/updates.ts +++ b/src/main/updates.ts @@ -1,34 +1,53 @@ import { autoUpdater } from 'electron-updater' -import { Notification } from 'electron' import { logger } from './logger' +import { rebuildContextMenu } from './tray' -let _manual = false +export const UpdateState = { + canCheck: true, + status: '' +} -autoUpdater.on('update-available', () => { - new Notification({ - title: 'Awakened PoE Trade', - body: 'New update found and is downloading in the background now' - }).show() - - logger.info('Update is downloading', { source: 'updater' }) +autoUpdater.on('update-available', (info: { version: string }) => { + UpdateState.canCheck = false + if (autoUpdater.autoDownload) { + UpdateState.status = `Downloading v${info.version} ...` + } else { + UpdateState.status = `Update v${info.version} available on Github` + } + rebuildContextMenu() }) autoUpdater.on('update-not-available', () => { - if (_manual) { - new Notification({ - title: 'Awakened PoE Trade', - body: 'You already have the latest version' - }).show() - } - - logger.info('No updates available', { source: 'updater' }) + UpdateState.canCheck = true + UpdateState.status = 'No updates available' + rebuildContextMenu() }) -export async function checkForUpdates (manual: boolean = false) { - if (process.platform === 'darwin') return +autoUpdater.on('error', () => { + UpdateState.canCheck = true + UpdateState.status = 'Something went wrong, check logs' + rebuildContextMenu() +}) - _manual = manual - autoUpdater.checkForUpdatesAndNotify() +autoUpdater.on('update-downloaded', (info: { version: string }) => { + UpdateState.canCheck = false + UpdateState.status = `v${info.version} will be installed on exit` + rebuildContextMenu() +}) - logger.info('Checking for updates', { source: 'updater', manual }) +// on('download-progress') https://github.com/electron-userland/electron-builder/issues/2521 + +export async function checkForUpdates () { + autoUpdater.logger = logger + autoUpdater.autoDownload = !process.env.PORTABLE_EXECUTABLE_DIR // https://www.electron.build/configuration/nsis.html#portable + + UpdateState.canCheck = false + UpdateState.status = 'Checking for update...' + rebuildContextMenu() + + try { + await autoUpdater.checkForUpdates() + } catch { + // handled by event + } }