Skip to content

Add auto-updater API to Electron server #213

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions resources/js/electron-plugin/dist/server/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import middleware from "./api/middleware.js";
import clipboardRoutes from "./api/clipboard.js";
import alertRoutes from "./api/alert.js";
import appRoutes from "./api/app.js";
import autoUpdaterRoutes from "./api/autoUpdater.js";
import screenRoutes from "./api/screen.js";
import dialogRoutes from "./api/dialog.js";
import debugRoutes from "./api/debug.js";
Expand Down Expand Up @@ -44,6 +45,7 @@ function startAPIServer(randomSecret) {
httpServer.use("/api/clipboard", clipboardRoutes);
httpServer.use("/api/alert", alertRoutes);
httpServer.use("/api/app", appRoutes);
httpServer.use("/api/auto-updater", autoUpdaterRoutes);
httpServer.use("/api/screen", screenRoutes);
httpServer.use("/api/dialog", dialogRoutes);
httpServer.use("/api/system", systemRoutes);
Expand Down
60 changes: 60 additions & 0 deletions resources/js/electron-plugin/dist/server/api/autoUpdater.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import express from "express";
import { autoUpdater } from "electron-updater";
import { notifyLaravel } from "../utils.js";
const router = express.Router();
router.post("/check-for-updates", (req, res) => {
autoUpdater.checkForUpdates();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Idea: If you add a guard clause here you might be able to prevent the download from happening multiple times.

Set a Boolean & make sure to reset it when the download finishes/fails

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it's such a bad idea, but I don't know how reliably it will work.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah it def needs some manual testing. We can always add it later if we want. What do you think @SRWieZ ?

res.sendStatus(200);
});
router.post("/quit-and-install", (req, res) => {
autoUpdater.quitAndInstall();
res.sendStatus(200);
});
autoUpdater.addListener("checking-for-update", () => {
notifyLaravel("events", {
event: `\\Native\\Laravel\\Events\\AutoUpdater\\CheckingForUpdate`,
});
});
autoUpdater.addListener("update-available", () => {
notifyLaravel("events", {
event: `\\Native\\Laravel\\Events\\AutoUpdater\\UpdateAvailable`,
});
});
autoUpdater.addListener("update-not-available", () => {
notifyLaravel("events", {
event: `\\Native\\Laravel\\Events\\AutoUpdater\\UpdateNotAvailable`,
});
});
autoUpdater.addListener("error", (error) => {
notifyLaravel("events", {
event: `\\Native\\Laravel\\Events\\AutoUpdater\\Error`,
payload: {
error: error,
},
});
});
autoUpdater.addListener("download-progress", (progressInfo) => {
notifyLaravel("events", {
event: `\\Native\\Laravel\\Events\\AutoUpdater\\DownloadProgress`,
payload: {
total: progressInfo.total,
delta: progressInfo.delta,
transferred: progressInfo.transferred,
percent: progressInfo.percent,
bytesPerSecond: progressInfo.bytesPerSecond,
},
});
});
autoUpdater.addListener("update-downloaded", (event) => {
notifyLaravel("events", {
event: `\\Native\\Laravel\\Events\\AutoUpdater\\UpdateDownloaded`,
payload: {
version: event.version,
downloadedFile: event.downloadedFile,
releaseDate: event.releaseDate,
releaseNotes: event.releaseNotes,
releaseName: event.releaseName,
},
});
});
export default router;
2 changes: 2 additions & 0 deletions resources/js/electron-plugin/src/server/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import middleware from "./api/middleware.js";
import clipboardRoutes from "./api/clipboard.js";
import alertRoutes from "./api/alert.js";
import appRoutes from "./api/app.js";
import autoUpdaterRoutes from "./api/autoUpdater.js";
import screenRoutes from "./api/screen.js";
import dialogRoutes from "./api/dialog.js";
import debugRoutes from "./api/debug.js";
Expand Down Expand Up @@ -43,6 +44,7 @@ async function startAPIServer(randomSecret: string): Promise<APIProcess> {
httpServer.use("/api/clipboard", clipboardRoutes);
httpServer.use("/api/alert", alertRoutes);
httpServer.use("/api/app", appRoutes);
httpServer.use("/api/auto-updater", autoUpdaterRoutes);
httpServer.use("/api/screen", screenRoutes);
httpServer.use("/api/dialog", dialogRoutes);
httpServer.use("/api/system", systemRoutes);
Expand Down
71 changes: 71 additions & 0 deletions resources/js/electron-plugin/src/server/api/autoUpdater.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import express from "express";
import { autoUpdater } from "electron-updater";
import type { ProgressInfo, UpdateDownloadedEvent } from "electron-updater";
import { notifyLaravel } from "../utils.js";

const router = express.Router();

router.post("/check-for-updates", (req, res) => {
autoUpdater.checkForUpdates();
res.sendStatus(200);
});

router.post("/quit-and-install", (req, res) => {
autoUpdater.quitAndInstall();
res.sendStatus(200);
});

autoUpdater.addListener("checking-for-update", () => {
notifyLaravel("events", {
event: `\\Native\\Laravel\\Events\\AutoUpdater\\CheckingForUpdate`,
});
});

autoUpdater.addListener("update-available", () => {
notifyLaravel("events", {
event: `\\Native\\Laravel\\Events\\AutoUpdater\\UpdateAvailable`,
});
});

autoUpdater.addListener("update-not-available", () => {
notifyLaravel("events", {
event: `\\Native\\Laravel\\Events\\AutoUpdater\\UpdateNotAvailable`,
});
});

autoUpdater.addListener("error", (error) => {
notifyLaravel("events", {
event: `\\Native\\Laravel\\Events\\AutoUpdater\\Error`,
payload: {
error: error,
},
});
});

autoUpdater.addListener("download-progress", (progressInfo: ProgressInfo) => {
notifyLaravel("events", {
event: `\\Native\\Laravel\\Events\\AutoUpdater\\DownloadProgress`,
payload: {
total: progressInfo.total,
delta: progressInfo.delta,
transferred: progressInfo.transferred,
percent: progressInfo.percent,
bytesPerSecond: progressInfo.bytesPerSecond,
},
});
});

autoUpdater.addListener("update-downloaded", (event: UpdateDownloadedEvent) => {
notifyLaravel("events", {
event: `\\Native\\Laravel\\Events\\AutoUpdater\\UpdateDownloaded`,
payload: {
version: event.version,
downloadedFile: event.downloadedFile,
releaseDate: event.releaseDate,
releaseNotes: event.releaseNotes,
releaseName: event.releaseName,
},
});
});

export default router;
8 changes: 8 additions & 0 deletions resources/js/electron-plugin/tests/api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@ import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';
import startAPIServer, { APIProcess } from "../src/server/api";
import axios from "axios";

vi.mock('electron-updater', () => ({
autoUpdater: {
checkForUpdates: vi.fn(),
quitAndInstall: vi.fn(),
addListener: vi.fn(),
},
}));

let apiServer: APIProcess;

describe('API test', () => {
Expand Down
Loading