11import { BrowserWindow , ipcMain , app } from 'electron' ;
2- import type { UpdateInfo as ElectronUpdateInfo , AppUpdater } from 'electron-updater' ;
2+ import type { UpdateInfo as ElectronUpdateInfo } from 'electron-updater' ;
3+ import { autoUpdater } from 'electron-updater' ;
34
45// 简化的更新信息类型(用于 IPC 传输)
56interface SimpleUpdateInfo {
@@ -51,7 +52,6 @@ function toSimpleInfo(info: ElectronUpdateInfo): SimpleUpdateInfo {
5152}
5253
5354let mainWindow : BrowserWindow | null = null ;
54- let autoUpdater : AppUpdater | null = null ;
5555
5656export interface UpdateStatus {
5757 status : 'checking' | 'available' | 'not-available' | 'downloading' | 'downloaded' | 'error' ;
@@ -60,37 +60,15 @@ export interface UpdateStatus {
6060 error ?: string ;
6161}
6262
63- async function getAutoUpdater ( ) : Promise < AppUpdater | null > {
64- if ( autoUpdater ) return autoUpdater ;
65-
66- try {
67- const module = await import ( 'electron-updater' ) ;
68- autoUpdater = module . autoUpdater ;
69-
70- // 禁用自动下载,让用户选择
71- if ( autoUpdater ) {
72- autoUpdater . autoDownload = false ;
73- autoUpdater . autoInstallOnAppQuit = true ;
74- }
75-
76- return autoUpdater ;
77- } catch ( error ) {
78- console . error ( 'Failed to load electron-updater:' , error ) ;
79- return null ;
80- }
81- }
82-
83- export async function initUpdater ( win : BrowserWindow ) {
63+ export function initUpdater ( win : BrowserWindow ) {
8464 mainWindow = win ;
85- const updater = await getAutoUpdater ( ) ;
8665
87- if ( ! updater ) {
88- console . warn ( 'Auto updater not available' ) ;
89- return ;
90- }
66+ // 禁用自动下载,让用户选择
67+ autoUpdater . autoDownload = false ;
68+ autoUpdater . autoInstallOnAppQuit = true ;
9169
9270 // 检查更新出错
93- updater . on ( 'error' , ( error ) => {
71+ autoUpdater . on ( 'error' , ( error ) => {
9472 console . error ( 'Update error:' , error ) ;
9573 let message = ( error && ( error as Error ) . message ) || String ( error ) ;
9674 if ( message . includes ( 'ZIP file not provided' ) ) {
@@ -104,13 +82,13 @@ export async function initUpdater(win: BrowserWindow) {
10482 } ) ;
10583
10684 // 检查更新中
107- updater . on ( 'checking-for-update' , ( ) => {
85+ autoUpdater . on ( 'checking-for-update' , ( ) => {
10886 console . info ( 'Checking for update...' ) ;
10987 sendStatusToWindow ( { status : 'checking' } ) ;
11088 } ) ;
11189
11290 // 有可用更新
113- updater . on ( 'update-available' , ( info ) => {
91+ autoUpdater . on ( 'update-available' , ( info ) => {
11492 console . info ( 'Update available:' , info . version ) ;
11593 sendStatusToWindow ( {
11694 status : 'available' ,
@@ -119,7 +97,7 @@ export async function initUpdater(win: BrowserWindow) {
11997 } ) ;
12098
12199 // 没有可用更新
122- updater . on ( 'update-not-available' , ( info ) => {
100+ autoUpdater . on ( 'update-not-available' , ( info ) => {
123101 console . info ( 'Update not available, current version is latest' ) ;
124102 sendStatusToWindow ( {
125103 status : 'not-available' ,
@@ -128,7 +106,7 @@ export async function initUpdater(win: BrowserWindow) {
128106 } ) ;
129107
130108 // 下载进度
131- updater . on ( 'download-progress' , ( progress : ProgressInfo ) => {
109+ autoUpdater . on ( 'download-progress' , ( progress : ProgressInfo ) => {
132110 console . info ( `Download progress: ${ progress . percent . toFixed ( 2 ) } %` ) ;
133111 sendStatusToWindow ( {
134112 status : 'downloading' ,
@@ -137,7 +115,7 @@ export async function initUpdater(win: BrowserWindow) {
137115 } ) ;
138116
139117 // 下载完成
140- updater . on ( 'update-downloaded' , ( info ) => {
118+ autoUpdater . on ( 'update-downloaded' , ( info ) => {
141119 console . info ( 'Update downloaded:' , info . version ) ;
142120 sendStatusToWindow ( {
143121 status : 'downloaded' ,
@@ -167,11 +145,7 @@ export function registerUpdaterIPC() {
167145 return { success : false , error : 'Update check disabled in development mode' } ;
168146 }
169147 try {
170- const updater = await getAutoUpdater ( ) ;
171- if ( ! updater ) {
172- return { success : false , error : '自动更新模块加载失败,请前往 GitHub Releases 手动下载:https://github.com/legeling/PromptHub/releases' } ;
173- }
174- const result = await updater . checkForUpdates ( ) ;
148+ const result = await autoUpdater . checkForUpdates ( ) ;
175149 return { success : true , result } ;
176150 } catch ( error ) {
177151 const errMsg = ( error as Error ) . message || String ( error ) ;
@@ -185,11 +159,7 @@ export function registerUpdaterIPC() {
185159 return { success : false , error : 'Download disabled in development mode' } ;
186160 }
187161 try {
188- const updater = await getAutoUpdater ( ) ;
189- if ( ! updater ) {
190- return { success : false , error : '自动更新模块不可用,请前往 GitHub Releases 手动下载:https://github.com/legeling/PromptHub/releases' } ;
191- }
192- await updater . downloadUpdate ( ) ;
162+ await autoUpdater . downloadUpdate ( ) ;
193163 return { success : true } ;
194164 } catch ( error ) {
195165 const errMsg = ( error as Error ) . message || String ( error ) ;
@@ -200,10 +170,7 @@ export function registerUpdaterIPC() {
200170 // 安装更新并重启
201171 ipcMain . handle ( 'updater:install' , async ( ) => {
202172 if ( ! isDev ) {
203- const updater = await getAutoUpdater ( ) ;
204- if ( updater ) {
205- updater . quitAndInstall ( false , true ) ;
206- }
173+ autoUpdater . quitAndInstall ( false , true ) ;
207174 }
208175 } ) ;
209176}
0 commit comments