From adfaa8b12c1adf00f09104debc8af92fef33149d Mon Sep 17 00:00:00 2001 From: perf3ct Date: Fri, 11 Jul 2025 21:11:05 +0000 Subject: [PATCH] fix(docs): unpack the OpenAPI spec files, and have Swagger UI read from the unpacked files --- apps/desktop/electron-forge/forge.config.ts | 4 +++- apps/server/src/routes/api_docs.ts | 16 +++++++++++++--- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/apps/desktop/electron-forge/forge.config.ts b/apps/desktop/electron-forge/forge.config.ts index b433ee4d7..7093a88eb 100644 --- a/apps/desktop/electron-forge/forge.config.ts +++ b/apps/desktop/electron-forge/forge.config.ts @@ -37,7 +37,9 @@ const config: ForgeConfig = { executableName: EXECUTABLE_NAME, name: PRODUCT_NAME, overwrite: true, - asar: true, + asar: { + unpack: "{**/node_modules/swagger-ui-dist/**,**/assets/*.yaml,**/assets/*.json}" + }, icon: path.join(APP_ICON_PATH, "icon"), ...macosSignConfiguration, windowsSign: windowsSignConfiguration, diff --git a/apps/server/src/routes/api_docs.ts b/apps/server/src/routes/api_docs.ts index 122ae44b0..6e0fe4096 100644 --- a/apps/server/src/routes/api_docs.ts +++ b/apps/server/src/routes/api_docs.ts @@ -3,12 +3,22 @@ import swaggerUi from "swagger-ui-express"; import { join } from "path"; import yaml from "js-yaml"; import type { JsonObject } from "swagger-ui-express"; -import { readFileSync } from "fs"; +import { readFileSync, existsSync } from "fs"; import { RESOURCE_DIR } from "../services/resource_dir"; export default function register(app: Application) { - const etapiDocument = yaml.load(readFileSync(join(RESOURCE_DIR, "etapi.openapi.yaml"), "utf8")) as JsonObject; - const apiDocument = JSON.parse(readFileSync(join(RESOURCE_DIR, "openapi.json"), "utf-8")); + // In packaged Electron apps, check if we need to read from the unpacked directory + let resourceDir = RESOURCE_DIR; + if (resourceDir.includes('app.asar')) { + const unpackedDir = RESOURCE_DIR.replace('app.asar', 'app.asar.unpacked'); + // Check if the unpacked directory has our files + if (existsSync(join(unpackedDir, "etapi.openapi.yaml"))) { + resourceDir = unpackedDir; + } + } + + const etapiDocument = yaml.load(readFileSync(join(resourceDir, "etapi.openapi.yaml"), "utf8")) as JsonObject; + const apiDocument = JSON.parse(readFileSync(join(resourceDir, "openapi.json"), "utf-8")); app.use( "/etapi/docs/",