Skip to content

Commit b518222

Browse files
committed
feat: add auto-updating
1 parent 463bdac commit b518222

File tree

3 files changed

+43
-4
lines changed

3 files changed

+43
-4
lines changed

ui/.releaserc.json

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,7 @@
99
[
1010
"@semantic-release/github",
1111
{
12-
"assets": [
13-
"out/make/{deb,rpm}/x64/auto-afk*",
14-
"out/make/squirrel.windows/x64/AutoAFK*"
15-
]
12+
"assets": ["out/make/{deb,rpm,squirrel.windows}/x64/*"]
1613
}
1714
]
1815
]

ui/src/main.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import fs from "fs/promises";
33
import path from "path";
44
import { Api } from "./api";
55
import type { RootState } from "./stores";
6+
import { registerUpdateHandlers, startUpdatePolling } from "./update";
67

78
// Handle creating/removing shortcuts on Windows when installing/uninstalling.
89
if (require("electron-squirrel-startup")) {
@@ -46,6 +47,7 @@ const createWindow = () => {
4647
app.on("ready", () => {
4748
registerHandlers();
4849
createWindow();
50+
startUpdatePolling();
4951
});
5052

5153
// Quit when all windows are closed, except on macOS. There, it's common
@@ -92,4 +94,6 @@ function registerHandlers(): void {
9294

9395
// Send outgoing messages
9496
Api.getInstance().registerCommands();
97+
98+
registerUpdateHandlers();
9599
}

ui/src/update.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { app, autoUpdater, dialog } from "electron";
2+
3+
const SERVER = "https://hazel-khaki-seven.vercel.app";
4+
const UPDATE_POLLING_INTERVAL_MS = 15 * 60 * 1000; // 15 minutes
5+
6+
/** Checks for and downloads updates */
7+
export function startUpdatePolling() {
8+
const url = `${SERVER}/update/${process.platform}/${app.getVersion()}`;
9+
autoUpdater.setFeedURL({ url });
10+
// setInterval(() => {
11+
// TODO: Do NOT force users to install updates
12+
// See https://github.com/electron/electron/issues/16561
13+
// However, in the meantime it is better to force update than never update...
14+
autoUpdater.checkForUpdates();
15+
// }, UPDATE_POLLING_INTERVAL_MS);
16+
}
17+
18+
export function registerUpdateHandlers() {
19+
autoUpdater.on("update-downloaded", (event, releaseNotes, releaseName) => {
20+
const dialogOpts = {
21+
type: "info",
22+
buttons: ["Restart", "Later"],
23+
title: "Application Update",
24+
message: process.platform === "win32" ? releaseNotes : releaseName,
25+
detail:
26+
"A new version has been downloaded. Restart the application to apply the updates.",
27+
};
28+
29+
dialog.showMessageBox(dialogOpts).then((returnValue) => {
30+
if (returnValue.response === 0) autoUpdater.quitAndInstall();
31+
});
32+
});
33+
34+
autoUpdater.on("error", (message) => {
35+
console.error("There was a problem updating the application");
36+
console.error(message);
37+
});
38+
}

0 commit comments

Comments
 (0)