refactor: 迁移至 electron-vite 官方推荐目录结构
- electron/* -> src/main/* (主进程) - electron/preload.ts -> src/preload/index.ts (预加载) - src/electron-api.d.ts -> src/preload/index.d.ts (类型声明) - src/* -> src/renderer/src/* (渲染进程) - src/index.html -> src/renderer/index.html - electron.vite.config.ts 简化为自动入口检测 (零配置) - tsconfig.app.json -> tsconfig.web.json, 更新 includes/paths - 构建产物 dist-electron/ -> out/ (修复 main/preload 输出覆盖 bug) - 更新 package.json main 字段 + electron-builder files - 更新 .gitignore + eslint ignores - 更新 AGENTS.md 架构文档
This commit is contained in:
@@ -0,0 +1,74 @@
|
||||
// electron-updater is CommonJS with a lazy getter for `autoUpdater`, so a named
|
||||
// ESM import fails at runtime under Node ESM (cjs-module-lexer can't detect it).
|
||||
// Use the default interop import and access the instance lazily inside initUpdater
|
||||
// (after app.whenReady), matching the original intended evaluation timing.
|
||||
import electronUpdater from 'electron-updater'
|
||||
import { BrowserWindow, ipcMain } from 'electron'
|
||||
|
||||
let mainWindow: BrowserWindow | null = null
|
||||
|
||||
export function initUpdater(win: BrowserWindow): void {
|
||||
mainWindow = win
|
||||
const autoUpdater = electronUpdater.autoUpdater
|
||||
|
||||
// 检查更新
|
||||
ipcMain.handle('updater:check', async () => {
|
||||
try {
|
||||
const result = await autoUpdater.checkForUpdates()
|
||||
return result?.updateInfo ? { available: true, version: result.updateInfo.version } : { available: false }
|
||||
} catch {
|
||||
return { available: false }
|
||||
}
|
||||
})
|
||||
|
||||
// 下载更新
|
||||
ipcMain.handle('updater:download', async () => {
|
||||
try {
|
||||
await autoUpdater.downloadUpdate()
|
||||
return { success: true }
|
||||
} catch (err: any) {
|
||||
return { success: false, error: err.message }
|
||||
}
|
||||
})
|
||||
|
||||
// 安装更新
|
||||
ipcMain.handle('updater:install', () => {
|
||||
autoUpdater.quitAndInstall()
|
||||
})
|
||||
|
||||
autoUpdater.autoDownload = false
|
||||
|
||||
autoUpdater.on('checking-for-update', () => {
|
||||
mainWindow?.webContents.send('updater:status', { status: 'checking' })
|
||||
})
|
||||
|
||||
autoUpdater.on('update-available', (info) => {
|
||||
mainWindow?.webContents.send('updater:status', {
|
||||
status: 'available',
|
||||
version: info.version,
|
||||
releaseNotes: info.releaseNotes,
|
||||
})
|
||||
})
|
||||
|
||||
autoUpdater.on('update-not-available', () => {
|
||||
mainWindow?.webContents.send('updater:status', { status: 'not-available' })
|
||||
})
|
||||
|
||||
autoUpdater.on('download-progress', (progress) => {
|
||||
mainWindow?.webContents.send('updater:status', {
|
||||
status: 'downloading',
|
||||
percent: progress.percent,
|
||||
transferred: progress.transferred,
|
||||
total: progress.total,
|
||||
bytesPerSecond: progress.bytesPerSecond,
|
||||
})
|
||||
})
|
||||
|
||||
autoUpdater.on('update-downloaded', () => {
|
||||
mainWindow?.webContents.send('updater:status', { status: 'downloaded' })
|
||||
})
|
||||
|
||||
autoUpdater.on('error', (err) => {
|
||||
mainWindow?.webContents.send('updater:status', { status: 'error', message: err.message })
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user