From 7a2e2ada4c5eb1f2b89bfefbe018b94c6900a6a1 Mon Sep 17 00:00:00 2001 From: CanbiZ <47820557+MickLesk@users.noreply.github.com> Date: Mon, 3 Mar 2025 12:06:50 +0100 Subject: [PATCH] fix paths --- .../__tests__/public/validate-json.test.ts | 22 ++++++++++++++----- frontend/src/app/api/categories/route.ts | 11 +++++++++- 2 files changed, 26 insertions(+), 7 deletions(-) diff --git a/frontend/src/__tests__/public/validate-json.test.ts b/frontend/src/__tests__/public/validate-json.test.ts index 562926a..ab73292 100644 --- a/frontend/src/__tests__/public/validate-json.test.ts +++ b/frontend/src/__tests__/public/validate-json.test.ts @@ -4,7 +4,17 @@ import path from "path"; import { ScriptSchema, type Script } from "@/app/json-editor/_schemas/schemas"; import { Metadata } from "@/lib/types"; -const jsonDir = path.join(__dirname, '../../json'); +const publicJsonPath = path.join(process.cwd(), 'public/json'); +const getJsonDirectory = async () => { + if (!(await fs.stat(publicJsonPath).catch(() => null))) { + throw new Error(`JSON path file not found: ${publicJsonPath}`); + } + const jsonPath = (await fs.readFile(publicJsonPath, "utf-8")).trim(); + return path.resolve(process.cwd(), jsonPath); +}; + + +const jsonDir = await getJsonDirectory(); const metadataFileName = "metadata.json"; const encoding = "utf-8"; @@ -15,7 +25,7 @@ describe.each(fileNames)("%s", async (fileName) => { let script: Script; beforeAll(async () => { - const filePath = path.resolve(jsonDir, fileName); + const filePath = path.resolve(jsonDir, fileName); const fileContent = await fs.readFile(filePath, encoding) script = JSON.parse(fileContent); }) @@ -36,7 +46,7 @@ describe(`${metadataFileName}`, async () => { let metadata: Metadata; beforeAll(async () => { - const filePath = path.resolve(jsonDir, metadataFileName); + const filePath = path.resolve(jsonDir, metadataFileName); const fileContent = await fs.readFile(filePath, encoding) metadata = JSON.parse(fileContent); }) @@ -45,9 +55,9 @@ describe(`${metadataFileName}`, async () => { // TODO: create zod schema for metadata. Move zod schemas to /lib/types.ts assert(metadata.categories.length > 0); metadata.categories.forEach((category) => { - assert.isString(category.name) - assert.isNumber(category.id) - assert.isNumber(category.sort_order) + assert.isString(category.name) + assert.isNumber(category.id) + assert.isNumber(category.sort_order) }); }); }) diff --git a/frontend/src/app/api/categories/route.ts b/frontend/src/app/api/categories/route.ts index 44791d9..b603d9c 100644 --- a/frontend/src/app/api/categories/route.ts +++ b/frontend/src/app/api/categories/route.ts @@ -5,7 +5,16 @@ import path from "path"; export const dynamic = "force-static"; -const jsonDir = path.join(__dirname, '../../json'); +const publicJsonPath = path.join(process.cwd(), 'public/json'); +const getJsonDirectory = async () => { + if (!(await fs.stat(publicJsonPath).catch(() => null))) { + throw new Error(`JSON path file not found: ${publicJsonPath}`); + } + const jsonPath = (await fs.readFile(publicJsonPath, "utf-8")).trim(); + return path.resolve(process.cwd(), jsonPath); +}; + +const jsonDir = await getJsonDirectory(); const metadataFileName = "metadata.json"; const encoding = "utf-8";