diff --git a/dev/main.tsx b/dev/main.tsx index e5233a1..ce20564 100644 --- a/dev/main.tsx +++ b/dev/main.tsx @@ -13,40 +13,20 @@ import { ZoomScanIcon, } from "./icons/index.ts"; -function sceneTreeBuilder( - current: BrowserFolder, - entries: string[], - index: number, - ref: string[], -): void { - console.log(entries); - - const next = current.has(entries[0]) - ? (current.get(entries[0]) as BrowserFolder) - : (new Map() as BrowserFolder); - - if (entries.length > 1) { - current.set(entries.shift() as string, next); - sceneTreeBuilder(next, entries, index, ref); - } else { - // if (entries[0] !== "index.ts") { - // return; - // } - current.set(entries[0], entries[0]); - } -} -async function fetchSceneIndex(): Promise { +async function fetchScenes(): Promise { const indexRef: string[] = await (await fetch("/api/scenes")).json(); - return indexRef.reduce((root, entry, index) => { - const entries = `scenes${entry}`.split("/"); - sceneTreeBuilder(root, entries, index, indexRef); + const scenes = indexRef.reduce((root, entry) => { + const [name] = entry.substring(1).split("/"); + root.set(name, `${name}/data.json`); return root; }, new Map() as BrowserFolder); + + return new Map([["scenes", scenes]]); } -const root = await fetchSceneIndex(); +const root = await fetchScenes(); function adjustGameContainer(gameContainer: HTMLElement, width: number): void { gameContainer.style = `position: relative; top: 0; left: ${width}px; width: ${document.body.offsetWidth - width}px;`; diff --git a/dev/server/middleware.ts b/dev/server/middleware.ts index 4d69189..2b4429a 100644 --- a/dev/server/middleware.ts +++ b/dev/server/middleware.ts @@ -1,16 +1,16 @@ import { readdirSync, writeFileSync } from "node:fs"; import type { IncomingMessage, ServerResponse } from "node:http"; -function buildBrowserIndex(name: string, dir: string): string[] { +function buildBrowserIndex(dir: string): string[] { const dirents = readdirSync(dir, { withFileTypes: true, recursive: true, }); return dirents - .filter((dirent) => dirent.isFile() && dirent.name !== name) + .filter((dirent) => dirent.isFile()) .map((dirent) => { - const path = dirent.parentPath.substring(dir.length); + const path = dirent.parentPath.substring(dir.length - 2); return `${path}/${dirent.name}`; }); } @@ -23,7 +23,7 @@ function get(req: IncomingMessage, res: ServerResponse): void { res.end(); } - const sceneData = buildBrowserIndex("scenes", "scenes"); // FIXME + const sceneData = buildBrowserIndex(`.${req.url}`); res.writeHead(200, { "Content-Type": "application/json" }); res.end(JSON.stringify(sceneData));