67 lines
3.2 KiB
JavaScript
67 lines
3.2 KiB
JavaScript
#!/usr/bin/env node
|
|
// Roadmap-Viewer: liefert ROADMAP.md, KLICKTESTS.md und testberichte/ der angegebenen Projekte (nur lesend)
|
|
// und die gebaute Oberfläche aus dist/.
|
|
//
|
|
// Aufruf: node server.mjs <projektordner> [weitere Projektordner …] (Port: env PORT, Standard 4177)
|
|
import { createServer } from "node:http";
|
|
import { readFile, readdir, stat } from "node:fs/promises";
|
|
import { basename, extname, join, normalize, resolve } from "node:path";
|
|
import { fileURLToPath } from "node:url";
|
|
|
|
const here = fileURLToPath(new URL(".", import.meta.url));
|
|
const dist = join(here, "dist");
|
|
const port = Number(process.env.PORT ?? 4177);
|
|
const host = process.env.HOST ?? "127.0.0.1";
|
|
|
|
const projects = process.argv.slice(2).map((p) => resolve(p));
|
|
if (!projects.length) {
|
|
console.error("Aufruf: node server.mjs <projektordner> [weitere …]");
|
|
process.exit(1);
|
|
}
|
|
const list = () => projects.map((p, i) => ({ id: String(i), name: basename(p), path: p }));
|
|
const dirOf = (id) => projects[Number(id)];
|
|
|
|
const text = async (file) => { try { return await readFile(file, "utf8"); } catch { return null; } };
|
|
const MIME = { ".html": "text/html; charset=utf-8", ".js": "text/javascript; charset=utf-8", ".css": "text/css; charset=utf-8", ".svg": "image/svg+xml", ".json": "application/json; charset=utf-8" };
|
|
|
|
function send(res, code, body, type = "text/plain; charset=utf-8") {
|
|
res.writeHead(code, { "content-type": type, "cache-control": "no-store" });
|
|
res.end(body);
|
|
}
|
|
|
|
const server = createServer(async (req, res) => {
|
|
const url = new URL(req.url ?? "/", "http://x");
|
|
if (req.method !== "GET") return send(res, 405, "nur GET");
|
|
|
|
if (url.pathname === "/api/projects") return send(res, 200, JSON.stringify(list()), MIME[".json"]);
|
|
|
|
const m = url.pathname.match(/^\/api\/(roadmap|klicktests|reports)$/);
|
|
if (m) {
|
|
const dir = dirOf(url.searchParams.get("project") ?? "0");
|
|
if (!dir) return send(res, 404, "unbekanntes Projekt");
|
|
if (m[1] === "roadmap" || m[1] === "klicktests") {
|
|
const name = m[1] === "roadmap" ? "ROADMAP.md" : "KLICKTESTS.md";
|
|
const t = await text(join(dir, name));
|
|
return t === null ? send(res, 404, `${name} fehlt in ${dir}`) : send(res, 200, t);
|
|
}
|
|
let files = [];
|
|
try { files = (await readdir(join(dir, "testberichte"))).filter((f) => f.endsWith(".md")).sort(); } catch { /* kein Ordner: keine Berichte */ }
|
|
const out = [];
|
|
for (const file of files) out.push({ file, text: (await text(join(dir, "testberichte", file))) ?? "" });
|
|
return send(res, 200, JSON.stringify(out), MIME[".json"]);
|
|
}
|
|
|
|
// Oberfläche aus dist/ (ohne Ausbruch aus dem Ordner)
|
|
const rel = normalize(decodeURIComponent(url.pathname)).replace(/^([/\\])+/, "");
|
|
let file = join(dist, rel || "index.html");
|
|
if (!file.startsWith(dist)) return send(res, 403, "verboten");
|
|
try { if ((await stat(file)).isDirectory()) file = join(file, "index.html"); } catch { file = join(dist, "index.html"); }
|
|
try { send(res, 200, await readFile(file), MIME[extname(file)] ?? "application/octet-stream"); }
|
|
catch { send(res, 404, "dist/ fehlt: erst `npm run build`"); }
|
|
});
|
|
|
|
server.listen(port, host, () => {
|
|
console.log(`Roadmap-Viewer auf http://${host}:${port}`);
|
|
for (const p of list()) console.log(` ${p.name}: ${p.path}`);
|
|
});
|