1
1
/* eslint-disable class-methods-use-this */
2
- import { dialog , BrowserWindow } from 'electron' ;
2
+ import { dialog , BrowserWindow , shell } from 'electron' ;
3
3
import { autoUpdater } from 'electron-updater' ;
4
4
import log from 'electron-log' ;
5
5
6
6
export default class AppUpdater {
7
7
private mainWindow : BrowserWindow ;
8
8
9
+ private readonly isMac : boolean ;
10
+
9
11
constructor ( window : BrowserWindow ) {
10
12
this . mainWindow = window ;
13
+ this . isMac = process . platform === 'darwin' ;
11
14
this . init ( ) ;
12
15
}
13
16
@@ -18,53 +21,70 @@ export default class AppUpdater {
18
21
// 检查更新出错
19
22
autoUpdater . on ( 'error' , ( error ) => {
20
23
log . error ( [ '检查更新失败' , error ] ) ;
21
- dialog . showErrorBox ( '更新出错' , error . message ) ;
22
24
} ) ;
23
25
24
26
// 检测到新版本
25
27
autoUpdater . on ( 'update-available' , async ( info ) => {
26
28
log . info ( '检测到新版本:' , info ) ;
27
29
28
- // 显示更新确认对话框
29
- const { response } = await dialog . showMessageBox ( {
30
- type : 'info' ,
31
- title : '发现新版本' ,
32
- message : `发现新版本 ${ info . version } \n是否现在更新?` ,
33
- detail : info . releaseNotes ?. toString ( ) || '暂无更新说明' ,
34
- buttons : [ '现在更新' , '暂不更新' ] ,
35
- cancelId : 1 ,
36
- } ) ;
30
+ if ( this . isMac ) {
31
+ // macOS 平台直接打开下载页面
32
+ const { response } = await dialog . showMessageBox ( {
33
+ type : 'info' ,
34
+ title : '发现新版本' ,
35
+ message : `发现新版本 ${ info . version } \n是否前往下载页面?` ,
36
+ detail : info . releaseNotes ?. toString ( ) || '暂无更新说明' ,
37
+ buttons : [ '前往下载' , '暂不更新' ] ,
38
+ cancelId : 1 ,
39
+ } ) ;
37
40
38
- if ( response === 0 ) {
39
- // 用户同意更新,开始下载
40
- autoUpdater . downloadUpdate ( ) ;
41
+ if ( response === 0 ) {
42
+ // 打开 GitHub release 页面
43
+ shell . openExternal (
44
+ 'https://github.com/TrumanDu/toolkit/releases/latest' ,
45
+ ) ;
46
+ }
47
+ } else {
48
+ // Windows 平台使用热更新
49
+ const { response } = await dialog . showMessageBox ( {
50
+ type : 'info' ,
51
+ title : '发现新版本' ,
52
+ message : `发现新版本 ${ info . version } \n是否现在更新?` ,
53
+ detail : info . releaseNotes ?. toString ( ) || '暂无更新说明' ,
54
+ buttons : [ '现在更新' , '暂不更新' ] ,
55
+ cancelId : 1 ,
56
+ } ) ;
41
57
42
- // 显示进度条窗口
43
- this . mainWindow . webContents . send ( 'show-progress-window' ) ;
58
+ if ( response === 0 ) {
59
+ // 用户同意更新,开始下载
60
+ autoUpdater . downloadUpdate ( ) ;
61
+ // 显示进度条窗口
62
+ this . mainWindow . webContents . send ( 'show-progress-window' ) ;
63
+ }
44
64
}
45
65
} ) ;
46
66
47
67
// 没有可用更新
48
68
autoUpdater . on ( 'update-not-available' , ( ) => {
49
69
log . info ( '当前已是最新版本' ) ;
50
- dialog . showMessageBox ( {
51
- title : '检查更新' ,
52
- message : '当前已是最新版本' ,
53
- } ) ;
54
70
} ) ;
55
71
56
- // 更新下载进度
72
+ // 更新下载进度 (仅 Windows)
57
73
autoUpdater . on ( 'download-progress' , ( progressObj ) => {
58
- this . mainWindow . webContents . send ( 'update-progress' , {
59
- percent : progressObj . percent ,
60
- transferred : progressObj . transferred ,
61
- total : progressObj . total ,
62
- bytesPerSecond : progressObj . bytesPerSecond ,
63
- } ) ;
74
+ if ( ! this . isMac ) {
75
+ this . mainWindow . webContents . send ( 'update-progress' , {
76
+ percent : progressObj . percent ,
77
+ transferred : progressObj . transferred ,
78
+ total : progressObj . total ,
79
+ bytesPerSecond : progressObj . bytesPerSecond ,
80
+ } ) ;
81
+ }
64
82
} ) ;
65
83
66
- // 更新下载完成
84
+ // 更新下载完成 (仅 Windows)
67
85
autoUpdater . on ( 'update-downloaded' , async ( info ) => {
86
+ if ( this . isMac ) return ;
87
+
68
88
log . info ( '更新包下载完成:' , info ) ;
69
89
70
90
// 关闭进度条窗口
@@ -76,23 +96,26 @@ export default class AppUpdater {
76
96
title : '更新就绪' ,
77
97
message : '新版本已下载完成,是否现在安装?' ,
78
98
detail : '点击确定将重启应用并安装更新' ,
79
- buttons : [ '现在安装' , '稍后安装 ' ] ,
99
+ buttons : [ '现在安装' , '下次安装 ' ] ,
80
100
cancelId : 1 ,
81
101
} ) ;
82
102
83
103
if ( response === 0 ) {
84
104
// 用户同意安装,退出并安装
85
105
log . info ( '开始安装更新...' ) ;
86
106
autoUpdater . quitAndInstall ( false , true ) ;
107
+ } else {
108
+ log . info ( '下次安装...' ) ;
87
109
}
88
110
} ) ;
89
111
}
90
112
91
- // 检查更新
92
- public checkForUpdates ( ) : void {
93
- log . info ( '开始检查更新...' ) ;
94
- autoUpdater . checkForUpdates ( ) . catch ( ( error : Error ) => {
113
+ public async checkForUpdates ( ) : Promise < void > {
114
+ try {
115
+ log . info ( '开始检查更新...' ) ;
116
+ await autoUpdater . checkForUpdates ( ) ;
117
+ } catch ( error ) {
95
118
log . error ( '检查更新失败:' , error ) ;
96
- } ) ;
119
+ }
97
120
}
98
121
}
0 commit comments