129 lines
4.1 KiB
TypeScript
129 lines
4.1 KiB
TypeScript
import { Router } from "express";
|
|
import {
|
|
getDirectoryTree,
|
|
readFile,
|
|
writeFile,
|
|
deleteFile,
|
|
searchFiles,
|
|
} from "../services/editor.service.js";
|
|
import { lookupResref, getResrefCount } from "../nwscript/resref-index.js";
|
|
import { lookupTlk, getTlkCount } from "../nwscript/tlk-index.js";
|
|
import { get2DAFile, list2DAFiles, get2DARow } from "../nwscript/twoda-index.js";
|
|
import { getSchema } from "../gff/schema.js";
|
|
|
|
const router = Router();
|
|
|
|
router.get("/tree/:repo", async (req, res) => {
|
|
try {
|
|
const tree = await getDirectoryTree(req.params.repo);
|
|
res.json(tree);
|
|
} catch (err: unknown) {
|
|
const message = err instanceof Error ? err.message : "Unknown error";
|
|
res.status(500).json({ error: message });
|
|
}
|
|
});
|
|
|
|
router.get("/file/:repo/*path", async (req, res) => {
|
|
try {
|
|
const filePath = Array.isArray(req.params.path) ? req.params.path.join("/") : req.params.path;
|
|
const content = await readFile(req.params.repo, filePath);
|
|
res.json({ content });
|
|
} catch (err: unknown) {
|
|
const message = err instanceof Error ? err.message : "Unknown error";
|
|
res.status(404).json({ error: message });
|
|
}
|
|
});
|
|
|
|
router.put("/file/:repo/*path", async (req, res) => {
|
|
try {
|
|
const filePath = Array.isArray(req.params.path) ? req.params.path.join("/") : req.params.path;
|
|
await writeFile(req.params.repo, filePath, req.body.content);
|
|
res.json({ ok: true });
|
|
} catch (err: unknown) {
|
|
const message = err instanceof Error ? err.message : "Unknown error";
|
|
res.status(500).json({ error: message });
|
|
}
|
|
});
|
|
|
|
router.delete("/file/:repo/*path", async (req, res) => {
|
|
try {
|
|
const filePath = Array.isArray(req.params.path) ? req.params.path.join("/") : req.params.path;
|
|
await deleteFile(req.params.repo, filePath);
|
|
res.json({ ok: true });
|
|
} catch (err: unknown) {
|
|
const message = err instanceof Error ? err.message : "Unknown error";
|
|
res.status(500).json({ error: message });
|
|
}
|
|
});
|
|
|
|
router.post("/search", async (req, res) => {
|
|
const { repo, query, regex, caseSensitive, includePattern, excludePattern, maxResults } = req.body;
|
|
if (!repo || !query) return res.status(400).json({ error: "repo and query required" });
|
|
try {
|
|
const results = await searchFiles(repo, query, { regex, caseSensitive, includePattern, excludePattern, maxResults });
|
|
res.json(results);
|
|
} catch (err: unknown) {
|
|
const message = err instanceof Error ? err.message : "Unknown error";
|
|
res.status(500).json({ error: message });
|
|
}
|
|
});
|
|
|
|
router.get("/resref/:resref", (req, res) => {
|
|
const entry = lookupResref(req.params.resref);
|
|
if (entry) {
|
|
res.json(entry);
|
|
} else {
|
|
res.status(404).json({ error: "resref not found" });
|
|
}
|
|
});
|
|
|
|
router.get("/resref-count", (_req, res) => {
|
|
res.json({ count: getResrefCount() });
|
|
});
|
|
|
|
router.get("/tlk/:id", (req, res) => {
|
|
const id = parseInt(req.params.id, 10);
|
|
if (isNaN(id)) return res.status(400).json({ error: "invalid id" });
|
|
const text = lookupTlk(id);
|
|
if (text !== undefined) {
|
|
res.json({ id, text });
|
|
} else {
|
|
res.status(404).json({ error: "TLK entry not found" });
|
|
}
|
|
});
|
|
|
|
router.get("/tlk-count", (_req, res) => {
|
|
res.json({ count: getTlkCount() });
|
|
});
|
|
|
|
router.get("/2da", (_req, res) => {
|
|
res.json({ files: list2DAFiles() });
|
|
});
|
|
|
|
router.get("/2da/:name", (req, res) => {
|
|
const file = get2DAFile(req.params.name);
|
|
if (!file) return res.status(404).json({ error: "2DA file not found" });
|
|
res.json({ columns: file.columns, rowCount: file.rows.size });
|
|
});
|
|
|
|
router.get("/2da/:name/:row", (req, res) => {
|
|
const row = parseInt(req.params.row, 10);
|
|
if (isNaN(row)) return res.status(400).json({ error: "invalid row" });
|
|
const data = get2DARow(req.params.name, row);
|
|
if (!data) return res.status(404).json({ error: "row not found" });
|
|
res.json(data);
|
|
});
|
|
|
|
router.get("/gff-schema/:type", async (req, res) => {
|
|
try {
|
|
const schema = await getSchema(req.params.type);
|
|
if (!schema) return res.status(404).json({ error: "unknown GFF type" });
|
|
res.json(schema);
|
|
} catch (err: unknown) {
|
|
const message = err instanceof Error ? err.message : "Unknown error";
|
|
res.status(500).json({ error: message });
|
|
}
|
|
});
|
|
|
|
export default router;
|