From ab6512ade409c63ad490e0505733f4dbafb1285b Mon Sep 17 00:00:00 2001 From: purocean Date: Sun, 11 Aug 2019 00:44:30 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=8D=87=E7=BA=A7=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/src/app.ts | 6 ++-- app/src/updater.ts | 77 +++++++++++++++++++--------------------------- 2 files changed, 36 insertions(+), 47 deletions(-) diff --git a/app/src/app.ts b/app/src/app.ts index 8b587c07..f49c2f8e 100644 --- a/app/src/app.ts +++ b/app/src/app.ts @@ -225,8 +225,10 @@ if (!gotTheLock) { tray.on('click', showWindow) tray.setContextMenu(contextMenu) - // 检查更新 - updater.init() + updater.init(() => { + // 立即升级,退出程序 + app.exit(0) + }) setTimeout(() => { updater.autoCheckForUpdates() }, 1000) diff --git a/app/src/updater.ts b/app/src/updater.ts index d21bdc72..d13291e8 100644 --- a/app/src/updater.ts +++ b/app/src/updater.ts @@ -9,42 +9,10 @@ autoUpdater.logger = logger const store = new Store() -const checkForUpdatesAvailable = app.isPackaged - let progressBar: any = null - -autoUpdater.on('error', e => { - progressBar && (progressBar.detail = '下载失败: ' + e) -}) - -autoUpdater.on('download-progress', e => { - if (progressBar) { - progressBar.value = e.percent - progressBar.detail = '下载中…… ' + e.percent.toFixed(2) + '%' - } -}) - -autoUpdater.on('update-downloaded', () => { - progressBar && progressBar.close() - - dialog.showMessageBox({ - cancelId: 999, - type: 'question', - buttons: ['立即安装', '推迟'], - defaultId: 0, - message: '新版本下载完成,是否要立即安装?' - }).then(result => { - if (result.response === 0) { - setImmediate(() => { - autoUpdater.quitAndInstall() - }) - } - }) -}) - let cancellationToken: CancellationToken = null -const init = () => { +const init = (call: () => void) => { autoUpdater.setFeedURL({ provider: 'github', owner: 'purocean', repo: 'yn'}) autoUpdater.autoDownload = false @@ -91,19 +59,40 @@ const init = () => { store.set('dontCheckUpdates', true) } }) + + autoUpdater.on('error', e => { + progressBar && (progressBar.detail = '下载失败: ' + e) + }) + + autoUpdater.on('download-progress', e => { + if (progressBar) { + progressBar.value = e.percent + progressBar.detail = '下载中…… ' + e.percent.toFixed(2) + '%' + } + }) + + autoUpdater.on('update-downloaded', () => { + progressBar && progressBar.close() + + dialog.showMessageBox({ + cancelId: 999, + type: 'question', + buttons: ['立即安装', '推迟'], + defaultId: 0, + message: '新版本下载完成,是否要立即安装?' + }).then(result => { + if (result.response === 0) { + setImmediate(() => { + autoUpdater.quitAndInstall() + call() + }) + } + }) + }) } const checkForUpdates = () => { store.set('dontCheckUpdates', false) - if (!checkForUpdatesAvailable) { - dialog.showMessageBox({ - type: 'info', - title: '检查更新不可用', - message: '安装到系统的应用才可检查更新', - }) - return false - } - autoUpdater.once('update-not-available', () => { dialog.showMessageBox({ type: 'info', @@ -117,9 +106,7 @@ const checkForUpdates = () => { const autoCheckForUpdates = () => { if (!store.get('dontCheckUpdates')) { - if (checkForUpdatesAvailable) { - autoUpdater.checkForUpdates() - } + autoUpdater.checkForUpdates() } }