11/**
22 * 自动更新核心模块
33 *
4- * 仅检测新版本并通知用户,不做自动下载/安装。
5- * 用户通过 GitHub Releases 页面手动下载覆盖安装。
6- *
4+ * 检测新版本 → 自动后台下载 → 用户确认后重启安装。
75 * 仅在打包后的生产环境中工作。
86 */
97
@@ -45,6 +43,11 @@ export async function checkForUpdates(): Promise<void> {
4543 }
4644}
4745
46+ /** 退出并安装已下载的更新 */
47+ export function quitAndInstall ( ) : void {
48+ autoUpdater . quitAndInstall ( false , true )
49+ }
50+
4851/** 清理更新器资源(定时器等) */
4952export function cleanupUpdater ( ) : void {
5053 if ( checkInterval ) {
@@ -61,17 +64,16 @@ export function cleanupUpdater(): void {
6164export function initAutoUpdater ( mainWindow : BrowserWindow ) : void {
6265 win = mainWindow
6366
64- // 配置 electron-updater 日志,转发到 console
6567 autoUpdater . logger = {
6668 info : ( ...args : unknown [ ] ) => console . log ( '[更新-updater]' , ...args ) ,
6769 warn : ( ...args : unknown [ ] ) => console . warn ( '[更新-updater]' , ...args ) ,
6870 error : ( ...args : unknown [ ] ) => console . error ( '[更新-updater]' , ...args ) ,
6971 debug : ( ...args : unknown [ ] ) => console . log ( '[更新-updater:debug]' , ...args ) ,
7072 }
7173
72- // 禁用自动下载和自动安装,仅做版本检测
73- autoUpdater . autoDownload = false
74- autoUpdater . autoInstallOnAppQuit = false
74+ // 自动下载,退出时自动安装
75+ autoUpdater . autoDownload = true
76+ autoUpdater . autoInstallOnAppQuit = true
7577
7678 // 监听更新事件
7779 autoUpdater . on ( 'checking-for-update' , ( ) => {
@@ -90,6 +92,27 @@ export function initAutoUpdater(mainWindow: BrowserWindow): void {
9092 } )
9193 } )
9294
95+ autoUpdater . on ( 'download-progress' , ( progress ) => {
96+ setStatus ( {
97+ status : 'downloading' ,
98+ version : ( currentStatus as { version ?: string } ) . version || '' ,
99+ progress : {
100+ percent : progress . percent ,
101+ transferred : progress . transferred ,
102+ total : progress . total ,
103+ bytesPerSecond : progress . bytesPerSecond ,
104+ } ,
105+ } )
106+ } )
107+
108+ autoUpdater . on ( 'update-downloaded' , ( info ) => {
109+ console . log ( '[更新] 下载完成:' , info . version )
110+ setStatus ( {
111+ status : 'downloaded' ,
112+ version : info . version ,
113+ } )
114+ } )
115+
93116 autoUpdater . on ( 'update-not-available' , ( ) => {
94117 console . log ( '[更新] 已是最新版本' )
95118 setStatus ( { status : 'not-available' } )
@@ -124,5 +147,5 @@ export function initAutoUpdater(mainWindow: BrowserWindow): void {
124147 win = null
125148 } )
126149
127- console . log ( '[更新] 版本检测模块已初始化(仅检测,不自动下载/安装 )' )
150+ console . log ( '[更新] 自动更新模块已初始化(自动下载,用户确认后安装 )' )
128151}
0 commit comments