Skip to content

Commit f882dd4

Browse files
WINBIGFOXPeteBishwhip
authored andcommitted
Add auto-updater API to Electron server (#213)
* Add auto-updater API to Electron server Integrates new auto-updater routes for checking updates and installing them. Also includes event listeners to notify Laravel about update states like availability, download completion, and errors. This enhances the app's ability to manage updates seamlessly. * Add download-progress listener to autoUpdater This commit introduces a new "download-progress" event listener in the autoUpdater module. It forwards progress details, such as percentage and speed, to Laravel via the notifyLaravel function. Additionally, the electron-updater module is now mocked in tests for better coverage. * Update api.test.ts * Refactor electron-updater imports for consistency and clarity Simplified the import syntax by directly importing `autoUpdater` from `electron-updater` instead of using the default export. Adjusted related mock implementation and type imports to align with this change, improving code readability and maintainability.
1 parent ffe39e6 commit f882dd4

File tree

5 files changed

+143
-0
lines changed

5 files changed

+143
-0
lines changed

resources/js/electron-plugin/dist/server/api.js

+2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import middleware from "./api/middleware.js";
1414
import clipboardRoutes from "./api/clipboard.js";
1515
import alertRoutes from "./api/alert.js";
1616
import appRoutes from "./api/app.js";
17+
import autoUpdaterRoutes from "./api/autoUpdater.js";
1718
import screenRoutes from "./api/screen.js";
1819
import dialogRoutes from "./api/dialog.js";
1920
import debugRoutes from "./api/debug.js";
@@ -44,6 +45,7 @@ function startAPIServer(randomSecret) {
4445
httpServer.use("/api/clipboard", clipboardRoutes);
4546
httpServer.use("/api/alert", alertRoutes);
4647
httpServer.use("/api/app", appRoutes);
48+
httpServer.use("/api/auto-updater", autoUpdaterRoutes);
4749
httpServer.use("/api/screen", screenRoutes);
4850
httpServer.use("/api/dialog", dialogRoutes);
4951
httpServer.use("/api/system", systemRoutes);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import express from "express";
2+
import { autoUpdater } from "electron-updater";
3+
import { notifyLaravel } from "../utils.js";
4+
const router = express.Router();
5+
router.post("/check-for-updates", (req, res) => {
6+
autoUpdater.checkForUpdates();
7+
res.sendStatus(200);
8+
});
9+
router.post("/quit-and-install", (req, res) => {
10+
autoUpdater.quitAndInstall();
11+
res.sendStatus(200);
12+
});
13+
autoUpdater.addListener("checking-for-update", () => {
14+
notifyLaravel("events", {
15+
event: `\\Native\\Laravel\\Events\\AutoUpdater\\CheckingForUpdate`,
16+
});
17+
});
18+
autoUpdater.addListener("update-available", () => {
19+
notifyLaravel("events", {
20+
event: `\\Native\\Laravel\\Events\\AutoUpdater\\UpdateAvailable`,
21+
});
22+
});
23+
autoUpdater.addListener("update-not-available", () => {
24+
notifyLaravel("events", {
25+
event: `\\Native\\Laravel\\Events\\AutoUpdater\\UpdateNotAvailable`,
26+
});
27+
});
28+
autoUpdater.addListener("error", (error) => {
29+
notifyLaravel("events", {
30+
event: `\\Native\\Laravel\\Events\\AutoUpdater\\Error`,
31+
payload: {
32+
error: error,
33+
},
34+
});
35+
});
36+
autoUpdater.addListener("download-progress", (progressInfo) => {
37+
notifyLaravel("events", {
38+
event: `\\Native\\Laravel\\Events\\AutoUpdater\\DownloadProgress`,
39+
payload: {
40+
total: progressInfo.total,
41+
delta: progressInfo.delta,
42+
transferred: progressInfo.transferred,
43+
percent: progressInfo.percent,
44+
bytesPerSecond: progressInfo.bytesPerSecond,
45+
},
46+
});
47+
});
48+
autoUpdater.addListener("update-downloaded", (event) => {
49+
notifyLaravel("events", {
50+
event: `\\Native\\Laravel\\Events\\AutoUpdater\\UpdateDownloaded`,
51+
payload: {
52+
version: event.version,
53+
downloadedFile: event.downloadedFile,
54+
releaseDate: event.releaseDate,
55+
releaseNotes: event.releaseNotes,
56+
releaseName: event.releaseName,
57+
},
58+
});
59+
});
60+
export default router;

resources/js/electron-plugin/src/server/api.ts

+2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import middleware from "./api/middleware.js";
66
import clipboardRoutes from "./api/clipboard.js";
77
import alertRoutes from "./api/alert.js";
88
import appRoutes from "./api/app.js";
9+
import autoUpdaterRoutes from "./api/autoUpdater.js";
910
import screenRoutes from "./api/screen.js";
1011
import dialogRoutes from "./api/dialog.js";
1112
import debugRoutes from "./api/debug.js";
@@ -43,6 +44,7 @@ async function startAPIServer(randomSecret: string): Promise<APIProcess> {
4344
httpServer.use("/api/clipboard", clipboardRoutes);
4445
httpServer.use("/api/alert", alertRoutes);
4546
httpServer.use("/api/app", appRoutes);
47+
httpServer.use("/api/auto-updater", autoUpdaterRoutes);
4648
httpServer.use("/api/screen", screenRoutes);
4749
httpServer.use("/api/dialog", dialogRoutes);
4850
httpServer.use("/api/system", systemRoutes);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import express from "express";
2+
import { autoUpdater } from "electron-updater";
3+
import type { ProgressInfo, UpdateDownloadedEvent } from "electron-updater";
4+
import { notifyLaravel } from "../utils.js";
5+
6+
const router = express.Router();
7+
8+
router.post("/check-for-updates", (req, res) => {
9+
autoUpdater.checkForUpdates();
10+
res.sendStatus(200);
11+
});
12+
13+
router.post("/quit-and-install", (req, res) => {
14+
autoUpdater.quitAndInstall();
15+
res.sendStatus(200);
16+
});
17+
18+
autoUpdater.addListener("checking-for-update", () => {
19+
notifyLaravel("events", {
20+
event: `\\Native\\Laravel\\Events\\AutoUpdater\\CheckingForUpdate`,
21+
});
22+
});
23+
24+
autoUpdater.addListener("update-available", () => {
25+
notifyLaravel("events", {
26+
event: `\\Native\\Laravel\\Events\\AutoUpdater\\UpdateAvailable`,
27+
});
28+
});
29+
30+
autoUpdater.addListener("update-not-available", () => {
31+
notifyLaravel("events", {
32+
event: `\\Native\\Laravel\\Events\\AutoUpdater\\UpdateNotAvailable`,
33+
});
34+
});
35+
36+
autoUpdater.addListener("error", (error) => {
37+
notifyLaravel("events", {
38+
event: `\\Native\\Laravel\\Events\\AutoUpdater\\Error`,
39+
payload: {
40+
error: error,
41+
},
42+
});
43+
});
44+
45+
autoUpdater.addListener("download-progress", (progressInfo: ProgressInfo) => {
46+
notifyLaravel("events", {
47+
event: `\\Native\\Laravel\\Events\\AutoUpdater\\DownloadProgress`,
48+
payload: {
49+
total: progressInfo.total,
50+
delta: progressInfo.delta,
51+
transferred: progressInfo.transferred,
52+
percent: progressInfo.percent,
53+
bytesPerSecond: progressInfo.bytesPerSecond,
54+
},
55+
});
56+
});
57+
58+
autoUpdater.addListener("update-downloaded", (event: UpdateDownloadedEvent) => {
59+
notifyLaravel("events", {
60+
event: `\\Native\\Laravel\\Events\\AutoUpdater\\UpdateDownloaded`,
61+
payload: {
62+
version: event.version,
63+
downloadedFile: event.downloadedFile,
64+
releaseDate: event.releaseDate,
65+
releaseNotes: event.releaseNotes,
66+
releaseName: event.releaseName,
67+
},
68+
});
69+
});
70+
71+
export default router;

resources/js/electron-plugin/tests/api.test.ts

+8
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@ import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';
22
import startAPIServer, { APIProcess } from "../src/server/api";
33
import axios from "axios";
44

5+
vi.mock('electron-updater', () => ({
6+
autoUpdater: {
7+
checkForUpdates: vi.fn(),
8+
quitAndInstall: vi.fn(),
9+
addListener: vi.fn(),
10+
},
11+
}));
12+
513
let apiServer: APIProcess;
614

715
describe('API test', () => {

0 commit comments

Comments
 (0)