-
Notifications
You must be signed in to change notification settings - Fork 409
Expand file tree
/
Copy pathAppUpdater.ts
More file actions
97 lines (89 loc) · 3.57 KB
/
Copy pathAppUpdater.ts
File metadata and controls
97 lines (89 loc) · 3.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import { getLogger } from '@shared/logger/main'
import { app, BrowserWindow } from 'electron'
import { AppUpdater as _AppUpdater, autoUpdater, UpdateCheckResult, CancellationToken, UpdateInfo } from 'electron-updater'
import { IpcChannel } from '@shared/IpcChannel'
// import { isDev } from '@main/constant'
// import path from 'path'
const logger = getLogger('AppUpdater')
export default class AppUpdater {
autoUpdater: _AppUpdater = autoUpdater
private updateCheckResult: UpdateCheckResult | null = null
private cancellationToken: CancellationToken = new CancellationToken()
constructor(mainWindow: BrowserWindow) {
autoUpdater.logger = logger
autoUpdater.forceDevUpdateConfig = false
// if (isDev) {
// const devConfigPath = path.join(process.cwd(), 'dev-app-update.yml')
// autoUpdater.updateConfigPath = devConfigPath
// // updateConfig
// logger.info('use dev update config2', autoUpdater.updateConfigPath)
// }
autoUpdater.autoDownload = false
autoUpdater.autoInstallOnAppQuit = false
autoUpdater.on('checking-for-update', () => {
logger.info('Checking for update...')
})
autoUpdater.on('update-available', (info: UpdateInfo) => {
logger.info('Update available', info)
mainWindow.webContents.send(IpcChannel.UpdateAvailable, info)
})
autoUpdater.on('update-not-available', () => {
logger.info('Update not available.')
mainWindow.webContents.send(IpcChannel.UpdateNotAvailable)
})
autoUpdater.on('error', (err) => {
logger.info('Update error.', err)
mainWindow.webContents.send(IpcChannel.UpdateError, err)
})
autoUpdater.on('update-downloaded', (info: UpdateInfo) => {
logger.info('update downloaded', info)
mainWindow.webContents.send(IpcChannel.UpdateDownloaded, info)
})
}
public async checkForUpdates() {
if (!app.isPackaged) {
logger.info('Skip update check in development mode')
return {
currentVersion: app.getVersion(),
updateInfo: null
}
}
try {
this.updateCheckResult = await this.autoUpdater.checkForUpdates()
if (this.updateCheckResult?.isUpdateAvailable && !this.autoUpdater.autoDownload) {
// 如果 autoDownload 为 false,则需要再调用下面的函数触发下
// do not use await, because it will block the return of this function
logger.info('downloadUpdate manual by check for updates', this.cancellationToken)
void this.autoUpdater.downloadUpdate(this.cancellationToken).catch((error) => {
logger.error('Failed to download update:', error as Error)
})
}
logger.info(
`update check result: ${this.updateCheckResult?.isUpdateAvailable}, channel: ${this.autoUpdater.channel}, currentVersion: ${this.autoUpdater.currentVersion}`
)
return {
currentVersion: this.autoUpdater.currentVersion,
updateInfo: this.updateCheckResult?.isUpdateAvailable ? this.updateCheckResult?.updateInfo : null
}
} catch (error) {
logger.error('Failed to check for update:', error as Error)
return {
currentVersion: app.getVersion(),
updateInfo: null
}
}
}
public downloadUpdate() {
this.autoUpdater.downloadUpdate()
}
public quitAndInstall() {
setImmediate(() => autoUpdater.quitAndInstall())
}
public cancelDownload() {
this.cancellationToken.cancel()
this.cancellationToken = new CancellationToken()
if (this.autoUpdater.autoDownload) {
this.updateCheckResult?.cancellationToken?.cancel()
}
}
}