From 16c233e83b87f1c40a4744e9c0c5424985ebd74f Mon Sep 17 00:00:00 2001 From: WINBIGFOX Date: Sat, 26 Apr 2025 15:51:57 +0200 Subject: [PATCH 1/4] 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. --- .../js/electron-plugin/dist/server/api.js | 2 + .../dist/server/api/autoUpdater.js | 49 +++++++++++++++ .../js/electron-plugin/src/server/api.ts | 2 + .../src/server/api/autoUpdater.ts | 59 +++++++++++++++++++ 4 files changed, 112 insertions(+) create mode 100644 resources/js/electron-plugin/dist/server/api/autoUpdater.js create mode 100644 resources/js/electron-plugin/src/server/api/autoUpdater.ts diff --git a/resources/js/electron-plugin/dist/server/api.js b/resources/js/electron-plugin/dist/server/api.js index 28c2a246..56ab8326 100644 --- a/resources/js/electron-plugin/dist/server/api.js +++ b/resources/js/electron-plugin/dist/server/api.js @@ -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"; @@ -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); diff --git a/resources/js/electron-plugin/dist/server/api/autoUpdater.js b/resources/js/electron-plugin/dist/server/api/autoUpdater.js new file mode 100644 index 00000000..742b33fe --- /dev/null +++ b/resources/js/electron-plugin/dist/server/api/autoUpdater.js @@ -0,0 +1,49 @@ +import express from "express"; +import electronUpdater from "electron-updater"; +import { notifyLaravel } from "../utils.js"; +const router = express.Router(); +const { autoUpdater } = electronUpdater; +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("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; diff --git a/resources/js/electron-plugin/src/server/api.ts b/resources/js/electron-plugin/src/server/api.ts index 0baa33ea..5ff685e5 100644 --- a/resources/js/electron-plugin/src/server/api.ts +++ b/resources/js/electron-plugin/src/server/api.ts @@ -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"; @@ -43,6 +44,7 @@ async function startAPIServer(randomSecret: string): Promise { 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); diff --git a/resources/js/electron-plugin/src/server/api/autoUpdater.ts b/resources/js/electron-plugin/src/server/api/autoUpdater.ts new file mode 100644 index 00000000..21477997 --- /dev/null +++ b/resources/js/electron-plugin/src/server/api/autoUpdater.ts @@ -0,0 +1,59 @@ +import express from "express"; +import electronUpdater, { UpdateDownloadedEvent } from "electron-updater"; +import { notifyLaravel } from "../utils.js"; + +const router = express.Router(); +const { autoUpdater } = electronUpdater; + +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("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; From 00ce0337e2b7eb009f185349fc8e776860df6d16 Mon Sep 17 00:00:00 2001 From: WINBIGFOX Date: Mon, 28 Apr 2025 21:44:48 +0200 Subject: [PATCH 2/4] 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. --- .../dist/server/api/autoUpdater.js | 12 ++++++ .../src/server/api/autoUpdater.ts | 41 +++++++++++++------ .../js/electron-plugin/tests/api.test.ts | 11 +++++ 3 files changed, 51 insertions(+), 13 deletions(-) diff --git a/resources/js/electron-plugin/dist/server/api/autoUpdater.js b/resources/js/electron-plugin/dist/server/api/autoUpdater.js index 742b33fe..87e80fb5 100644 --- a/resources/js/electron-plugin/dist/server/api/autoUpdater.js +++ b/resources/js/electron-plugin/dist/server/api/autoUpdater.js @@ -34,6 +34,18 @@ autoUpdater.addListener("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`, diff --git a/resources/js/electron-plugin/src/server/api/autoUpdater.ts b/resources/js/electron-plugin/src/server/api/autoUpdater.ts index 21477997..de862bd1 100644 --- a/resources/js/electron-plugin/src/server/api/autoUpdater.ts +++ b/resources/js/electron-plugin/src/server/api/autoUpdater.ts @@ -1,5 +1,8 @@ import express from "express"; -import electronUpdater, { UpdateDownloadedEvent } from "electron-updater"; +import electronUpdater, { + ProgressInfo, + UpdateDownloadedEvent, +} from "electron-updater"; import { notifyLaravel } from "../utils.js"; const router = express.Router(); @@ -42,18 +45,30 @@ autoUpdater.addListener("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, - }, - }); - }, -); + 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; diff --git a/resources/js/electron-plugin/tests/api.test.ts b/resources/js/electron-plugin/tests/api.test.ts index ec4bd95d..6a6c330b 100644 --- a/resources/js/electron-plugin/tests/api.test.ts +++ b/resources/js/electron-plugin/tests/api.test.ts @@ -2,6 +2,17 @@ import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest'; import startAPIServer, { APIProcess } from "../src/server/api"; import axios from "axios"; +// Mock für electron-updater +vi.mock('electron-updater', () => ({ + default: { + autoUpdater: { + checkForUpdates: vi.fn(), + quitAndInstall: vi.fn(), + addListener: vi.fn(), + }, + }, +})); + let apiServer: APIProcess; describe('API test', () => { From 1de495c5af7225aee0581e0e6b7a207bd6b837bf Mon Sep 17 00:00:00 2001 From: WINBIGFOX Date: Mon, 28 Apr 2025 21:50:28 +0200 Subject: [PATCH 3/4] Update api.test.ts --- resources/js/electron-plugin/tests/api.test.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/resources/js/electron-plugin/tests/api.test.ts b/resources/js/electron-plugin/tests/api.test.ts index 6a6c330b..38c7ef18 100644 --- a/resources/js/electron-plugin/tests/api.test.ts +++ b/resources/js/electron-plugin/tests/api.test.ts @@ -2,7 +2,6 @@ import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest'; import startAPIServer, { APIProcess } from "../src/server/api"; import axios from "axios"; -// Mock für electron-updater vi.mock('electron-updater', () => ({ default: { autoUpdater: { From 3b68c518806b48c0566c2bda047aee4812edb4d2 Mon Sep 17 00:00:00 2001 From: WINBIGFOX Date: Wed, 30 Apr 2025 09:46:24 +0200 Subject: [PATCH 4/4] 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. --- .../js/electron-plugin/dist/server/api/autoUpdater.js | 3 +-- .../js/electron-plugin/src/server/api/autoUpdater.ts | 7 ++----- resources/js/electron-plugin/tests/api.test.ts | 10 ++++------ 3 files changed, 7 insertions(+), 13 deletions(-) diff --git a/resources/js/electron-plugin/dist/server/api/autoUpdater.js b/resources/js/electron-plugin/dist/server/api/autoUpdater.js index 87e80fb5..60808cac 100644 --- a/resources/js/electron-plugin/dist/server/api/autoUpdater.js +++ b/resources/js/electron-plugin/dist/server/api/autoUpdater.js @@ -1,8 +1,7 @@ import express from "express"; -import electronUpdater from "electron-updater"; +import { autoUpdater } from "electron-updater"; import { notifyLaravel } from "../utils.js"; const router = express.Router(); -const { autoUpdater } = electronUpdater; router.post("/check-for-updates", (req, res) => { autoUpdater.checkForUpdates(); res.sendStatus(200); diff --git a/resources/js/electron-plugin/src/server/api/autoUpdater.ts b/resources/js/electron-plugin/src/server/api/autoUpdater.ts index de862bd1..d1d08fec 100644 --- a/resources/js/electron-plugin/src/server/api/autoUpdater.ts +++ b/resources/js/electron-plugin/src/server/api/autoUpdater.ts @@ -1,12 +1,9 @@ import express from "express"; -import electronUpdater, { - ProgressInfo, - UpdateDownloadedEvent, -} from "electron-updater"; +import { autoUpdater } from "electron-updater"; +import type { ProgressInfo, UpdateDownloadedEvent } from "electron-updater"; import { notifyLaravel } from "../utils.js"; const router = express.Router(); -const { autoUpdater } = electronUpdater; router.post("/check-for-updates", (req, res) => { autoUpdater.checkForUpdates(); diff --git a/resources/js/electron-plugin/tests/api.test.ts b/resources/js/electron-plugin/tests/api.test.ts index 6a6c330b..95bfd894 100644 --- a/resources/js/electron-plugin/tests/api.test.ts +++ b/resources/js/electron-plugin/tests/api.test.ts @@ -4,12 +4,10 @@ import axios from "axios"; // Mock für electron-updater vi.mock('electron-updater', () => ({ - default: { - autoUpdater: { - checkForUpdates: vi.fn(), - quitAndInstall: vi.fn(), - addListener: vi.fn(), - }, + autoUpdater: { + checkForUpdates: vi.fn(), + quitAndInstall: vi.fn(), + addListener: vi.fn(), }, }));