feat: add resref auto-lookup index for NWScript editor
This commit is contained in:
@@ -0,0 +1,54 @@
|
|||||||
|
import fs from "fs/promises";
|
||||||
|
import path from "path";
|
||||||
|
|
||||||
|
interface ResrefEntry {
|
||||||
|
resref: string;
|
||||||
|
type: string;
|
||||||
|
filePath: string;
|
||||||
|
displayName?: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
const index = new Map<string, ResrefEntry>();
|
||||||
|
|
||||||
|
export async function buildResrefIndex(repoPath: string): Promise<void> {
|
||||||
|
index.clear();
|
||||||
|
try {
|
||||||
|
await scanDir(repoPath, repoPath);
|
||||||
|
} catch {
|
||||||
|
// repo not cloned yet — that's fine
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function scanDir(dir: string, rootPath: string): Promise<void> {
|
||||||
|
let entries;
|
||||||
|
try {
|
||||||
|
entries = await fs.readdir(dir, { withFileTypes: true });
|
||||||
|
} catch {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
for (const entry of entries) {
|
||||||
|
const fullPath = path.join(dir, entry.name);
|
||||||
|
if (entry.isDirectory()) {
|
||||||
|
if (entry.name === "node_modules" || entry.name === ".git" || entry.name === ".nasher") continue;
|
||||||
|
await scanDir(fullPath, rootPath);
|
||||||
|
} else if (entry.name.endsWith(".json")) {
|
||||||
|
const match = entry.name.match(/^(.+)\.(utc|uti|utp|are|dlg|utm|ute)\.json$/);
|
||||||
|
if (match) {
|
||||||
|
const [, resref, type] = match;
|
||||||
|
index.set(resref, {
|
||||||
|
resref,
|
||||||
|
type,
|
||||||
|
filePath: path.relative(rootPath, fullPath),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export function lookupResref(resref: string): ResrefEntry | undefined {
|
||||||
|
return index.get(resref);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function getResrefCount(): number {
|
||||||
|
return index.size;
|
||||||
|
}
|
||||||
@@ -5,6 +5,7 @@ import {
|
|||||||
writeFile,
|
writeFile,
|
||||||
deleteFile,
|
deleteFile,
|
||||||
} from "../services/editor.service.js";
|
} from "../services/editor.service.js";
|
||||||
|
import { lookupResref, getResrefCount } from "../nwscript/resref-index.js";
|
||||||
|
|
||||||
const router = Router();
|
const router = Router();
|
||||||
|
|
||||||
@@ -48,4 +49,17 @@ router.delete("/file/:repo/*path", async (req, res) => {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
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() });
|
||||||
|
});
|
||||||
|
|
||||||
export default router;
|
export default router;
|
||||||
|
|||||||
Reference in New Issue
Block a user