Skip to content

Commit 119e1d8

Browse files
committed
feat: added version check for updater
1 parent 75aadd8 commit 119e1d8

File tree

5 files changed

+56
-14
lines changed

5 files changed

+56
-14
lines changed

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@
132132
"@types/node": "^20.14.8",
133133
"@types/react": "^18.3.12",
134134
"@types/react-dom": "^18.3.1",
135+
"@types/semver": "^7.5.8",
135136
"@vitejs/plugin-react": "^4.3.1",
136137
"autoprefixer": "^10.4.20",
137138
"cross-env": "^7.0.3",

pnpm-lock.yaml

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/main/trpc/internal.api.ts

+9-8
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import secureStore from '@main/secureStore'
2+
import { checkForUpdates, setUpdateHandledByFrontend } from '@main/updater'
23
import { TRPCError } from '@trpc/server'
34
import { shell } from 'electron'
45
import { autoUpdater } from 'electron-updater'
@@ -56,22 +57,22 @@ export const internalRouter = router({
5657
.mutation(async ({ input: { path: filePath } }) => {
5758
await shell.openPath(filePath)
5859
}),
59-
checkUpdate: publicProcedure
60-
.input(z.boolean().default(false))
61-
.mutation(async ({ input: notifyIfUpdateAvailable }) => {
62-
if (notifyIfUpdateAvailable) return await autoUpdater.checkForUpdatesAndNotify()
63-
return await autoUpdater.checkForUpdates()
64-
}),
60+
checkUpdate: publicProcedure.mutation(async () => {
61+
return await checkForUpdates()
62+
}),
6563
downloadUpdate: publicProcedure.mutation(() => {
6664
try {
67-
return autoUpdater.downloadUpdate()
65+
return autoUpdater.downloadUpdate().then((s) => {
66+
setUpdateHandledByFrontend(true)
67+
return s
68+
})
6869
} catch (ex: any) {
6970
throw new TRPCError({ message: ex.message, code: 'INTERNAL_SERVER_ERROR' })
7071
}
7172
}),
7273
quitAndInstallUpdate: publicProcedure.mutation(() => {
7374
try {
74-
return autoUpdater.quitAndInstall()
75+
return autoUpdater.quitAndInstall(false, true)
7576
} catch (ex: any) {
7677
throw new TRPCError({ message: ex.message, code: 'INTERNAL_SERVER_ERROR' })
7778
}

src/main/updater/index.ts

+32-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,29 @@
1-
import { BrowserWindow, dialog } from 'electron'
1+
import { appStore } from '@main/stores/app.store'
2+
import { isProduction } from '@shared/config'
3+
import { app, BrowserWindow, dialog } from 'electron'
24
import { autoUpdater } from 'electron-updater'
5+
import semver from 'semver'
6+
const [GITHUB_AUTHOR, GITHUB_REPOSITORY] = import.meta.env.VITE_GITHUB_REPOSITORY.split('/', 2)
7+
let updateQueuedInFrontend = false
8+
export const setUpdateHandledByFrontend = (value: boolean) => (updateQueuedInFrontend = value)
9+
export function isUpdateInRange(ver: string) {
10+
if (!isProduction) return true
11+
return semver.gtr(ver, app.getVersion(), {
12+
includePrerelease: appStore.store.beta
13+
})
14+
}
15+
export function checkForUpdates() {
16+
return autoUpdater
17+
.checkForUpdates()
18+
.then((info) => (info && isUpdateInRange(info.updateInfo.version) && info) || null)
19+
}
20+
export function checkForUpdatesAndNotify() {}
321
export function attachAutoUpdaterIPC(win: BrowserWindow) {
4-
autoUpdater.on('update-available', (info) => win.webContents.send('update-available', info))
22+
autoUpdater.on(
23+
'update-available',
24+
(info) =>
25+
info && isUpdateInRange(info.version) && win.webContents.send('update-available', info)
26+
)
527
autoUpdater.on('update-not-available', (info) => {
628
win.webContents.send('update-available', false)
729
win.webContents.send('update-checking', false)
@@ -14,6 +36,7 @@ export function attachAutoUpdaterIPC(win: BrowserWindow) {
1436
win.webContents.send('update-checking', new Date().toISOString())
1537
)
1638
autoUpdater.signals.updateDownloaded(async (x) => {
39+
if (updateQueuedInFrontend) return
1740
const releaseNotes = (
1841
typeof x.releaseNotes === 'string'
1942
? x.releaseNotes
@@ -36,4 +59,11 @@ export function attachAutoUpdaterIPC(win: BrowserWindow) {
3659
else if (response === 1) autoUpdater.autoInstallOnAppQuit = true
3760
})
3861
})
62+
63+
autoUpdater.setFeedURL({
64+
provider: 'github',
65+
owner: GITHUB_AUTHOR,
66+
repo: GITHUB_REPOSITORY
67+
})
68+
autoUpdater.autoDownload = false
3969
}

src/renderer/src/pages/sections/about.tsx

+6-4
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,11 @@ export default function AboutTab() {
4040
const updateNow = await new Promise<boolean>((resolve) => {
4141
toast.info('A new version has been found', {
4242
action: (
43-
<div className="flex gap-1">
44-
<Button className="h-6 px-1" onClick={() => resolve(true)}>
43+
<div className="flex gap-1 ml-auto">
44+
<Button className="h-6 px-2 text-xs" variant={"ghost"} onClick={() => resolve(true)}>
4545
Update Now
4646
</Button>
47-
<Button className="h-6 px-1" onClick={() => resolve(false)}>
47+
<Button className="h-6 px-2 text-xs" variant={"ghost"} onClick={() => resolve(false)}>
4848
Later
4949
</Button>
5050
</div>
@@ -56,6 +56,8 @@ export default function AboutTab() {
5656
if (updateNow) {
5757
toast.loading('Downloading update...', {
5858
id,
59+
action: null,
60+
description: null,
5961
dismissible: false,
6062
duration: 0
6163
})
@@ -64,7 +66,7 @@ export default function AboutTab() {
6466
})
6567
await quitAndInstallUpdate()
6668
} else {
67-
toast.info('Update postponed.', { id, duration: 5000 })
69+
toast.info('Update postponed.', { id, duration: 5000, description: null })
6870
}
6971
}
7072
} finally {

0 commit comments

Comments
 (0)