Skip to content

Commit

Permalink
feat: add auto-updating
Browse files Browse the repository at this point in the history
  • Loading branch information
aentwist committed Jun 16, 2024
1 parent 463bdac commit b518222
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 4 deletions.
5 changes: 1 addition & 4 deletions ui/.releaserc.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@
[
"@semantic-release/github",
{
"assets": [
"out/make/{deb,rpm}/x64/auto-afk*",
"out/make/squirrel.windows/x64/AutoAFK*"
]
"assets": ["out/make/{deb,rpm,squirrel.windows}/x64/*"]
}
]
]
Expand Down
4 changes: 4 additions & 0 deletions ui/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import fs from "fs/promises";
import path from "path";
import { Api } from "./api";
import type { RootState } from "./stores";
import { registerUpdateHandlers, startUpdatePolling } from "./update";

// Handle creating/removing shortcuts on Windows when installing/uninstalling.
if (require("electron-squirrel-startup")) {
Expand Down Expand Up @@ -46,6 +47,7 @@ const createWindow = () => {
app.on("ready", () => {
registerHandlers();
createWindow();
startUpdatePolling();
});

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

// Send outgoing messages
Api.getInstance().registerCommands();

registerUpdateHandlers();
}
38 changes: 38 additions & 0 deletions ui/src/update.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { app, autoUpdater, dialog } from "electron";

const SERVER = "https://hazel-khaki-seven.vercel.app";
const UPDATE_POLLING_INTERVAL_MS = 15 * 60 * 1000; // 15 minutes

/** Checks for and downloads updates */
export function startUpdatePolling() {
const url = `${SERVER}/update/${process.platform}/${app.getVersion()}`;
autoUpdater.setFeedURL({ url });
// setInterval(() => {
// TODO: Do NOT force users to install updates
// See https://github.com/electron/electron/issues/16561
// However, in the meantime it is better to force update than never update...
autoUpdater.checkForUpdates();
// }, UPDATE_POLLING_INTERVAL_MS);
}

export function registerUpdateHandlers() {
autoUpdater.on("update-downloaded", (event, releaseNotes, releaseName) => {
const dialogOpts = {
type: "info",
buttons: ["Restart", "Later"],
title: "Application Update",
message: process.platform === "win32" ? releaseNotes : releaseName,
detail:
"A new version has been downloaded. Restart the application to apply the updates.",
};

dialog.showMessageBox(dialogOpts).then((returnValue) => {
if (returnValue.response === 0) autoUpdater.quitAndInstall();
});
});

autoUpdater.on("error", (message) => {
console.error("There was a problem updating the application");
console.error(message);
});
}

0 comments on commit b518222

Please sign in to comment.