diff --git a/resources/js/electron-plugin/dist/server/api.js b/resources/js/electron-plugin/dist/server/api.js index 28c2a24..56ab832 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 0000000..60808ca --- /dev/null +++ b/resources/js/electron-plugin/dist/server/api/autoUpdater.js @@ -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(); + 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; diff --git a/resources/js/electron-plugin/src/server/api.ts b/resources/js/electron-plugin/src/server/api.ts index 0baa33e..5ff685e 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 0000000..d1d08fe --- /dev/null +++ b/resources/js/electron-plugin/src/server/api/autoUpdater.ts @@ -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; diff --git a/resources/js/electron-plugin/tests/api.test.ts b/resources/js/electron-plugin/tests/api.test.ts index ec4bd95..ce4959c 100644 --- a/resources/js/electron-plugin/tests/api.test.ts +++ b/resources/js/electron-plugin/tests/api.test.ts @@ -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', () => {