Skip to content

Commit 5f443f0

Browse files
committed
chore(update): configure GitHub auto-updates
1 parent 56fa3bd commit 5f443f0

3 files changed

Lines changed: 54 additions & 3 deletions

File tree

.github/workflows/release.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,5 +53,7 @@ jobs:
5353
files: |
5454
dist/**/interview-coder-cn-*.dmg
5555
dist/**/interview-coder-cn-*.exe
56+
dist/**/latest-mac.yml
57+
dist/**/latest-windows.yml
5658
env:
5759
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

electron-builder.yml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ appImage:
3939
artifactName: ${name}-${version}.${ext}
4040
npmRebuild: false
4141
publish:
42-
provider: generic
43-
url: https://example.com/auto-updates
42+
provider: github
43+
owner: ooboqoo
44+
repo: interview-coder-cn
4445
electronDownload:
4546
mirror: https://npmmirror.com/mirrors/electron/

src/main/index.ts

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import 'dotenv/config'
2-
import { app, BrowserWindow, globalShortcut } from 'electron'
2+
import { app, BrowserWindow, globalShortcut, dialog } from 'electron'
3+
import { autoUpdater } from 'electron-updater'
34

45
// Swallow AbortError from user-initiated stream cancellations to keep console clean
56
function isAbortError(error: unknown): boolean {
@@ -39,6 +40,53 @@ app.whenReady().then(() => {
3940

4041
createWindow()
4142

43+
// Configure auto-updater: prompt to download when update is available
44+
try {
45+
autoUpdater.autoDownload = false
46+
autoUpdater.on('update-available', async () => {
47+
const result = await dialog.showMessageBox({
48+
type: 'info',
49+
buttons: ['立即下载', '稍后'],
50+
defaultId: 0,
51+
cancelId: 1,
52+
title: '发现新版本',
53+
message: '检测到新版本可用。',
54+
detail: '现在下载并安装更新吗?'
55+
})
56+
if (result.response === 0) {
57+
autoUpdater.downloadUpdate().catch((err) => console.error(err))
58+
}
59+
})
60+
61+
autoUpdater.on('error', (error) => {
62+
console.error('Auto update error:', error)
63+
})
64+
65+
autoUpdater.on('update-not-available', () => {
66+
// no-op
67+
})
68+
69+
autoUpdater.on('update-downloaded', async () => {
70+
const res = await dialog.showMessageBox({
71+
type: 'info',
72+
buttons: ['立即重启', '稍后'],
73+
defaultId: 0,
74+
cancelId: 1,
75+
title: '更新已就绪',
76+
message: '更新已下载完成。',
77+
detail: '是否立即重启以应用更新?'
78+
})
79+
if (res.response === 0) {
80+
setImmediate(() => autoUpdater.quitAndInstall())
81+
}
82+
})
83+
84+
// Trigger the check after window creation
85+
autoUpdater.checkForUpdates().catch((err) => console.error(err))
86+
} catch (e) {
87+
console.error('Failed to initialize auto-updater:', e)
88+
}
89+
4290
app.on('activate', function () {
4391
// On macOS it's common to re-create a window in the app when the
4492
// dock icon is clicked and there are no other windows open.

0 commit comments

Comments
 (0)