44 lines
1.5 KiB
TypeScript
44 lines
1.5 KiB
TypeScript
import fs from "node:fs";
|
|
import path from "node:path";
|
|
import fg from "fast-glob";
|
|
|
|
function readFileSafe(filePath: string): string {
|
|
return fs.readFileSync(filePath, "utf-8");
|
|
}
|
|
|
|
export function getResource(archRoot: string, uri: string) {
|
|
// manifest://<domain>
|
|
if (uri.startsWith("manifest://")) {
|
|
const domain = uri.replace("manifest://", "");
|
|
const mf = path.join(archRoot, domain, "manifest.yaml");
|
|
if (!fs.existsSync(mf)) return { ok: false, error: `Not found: ${uri}` };
|
|
return { ok: true, uri, mimeType: "application/yaml", content: readFileSafe(mf) };
|
|
}
|
|
|
|
// views://<domain>/<viewfile-without-ext>
|
|
if (uri.startsWith("views://")) {
|
|
const rest = uri.replace("views://", "");
|
|
const [domain, viewName] = rest.split("/", 2);
|
|
const domainDir = path.join(archRoot, domain);
|
|
const matches = fg.sync([`views/${viewName}.md`], { cwd: domainDir, absolute: true });
|
|
if (!matches.length) return { ok: false, error: `Not found: ${uri}` };
|
|
return {
|
|
ok: true,
|
|
uri,
|
|
mimeType: "text/markdown",
|
|
content: readFileSafe(matches[0]!),
|
|
};
|
|
}
|
|
|
|
// register://<domain>/<relative-path>
|
|
if (uri.startsWith("register://")) {
|
|
const rest = uri.replace("register://", "");
|
|
const [domain, rel] = rest.split("/", 2);
|
|
const filePath = path.join(archRoot, domain, rel);
|
|
if (!fs.existsSync(filePath)) return { ok: false, error: `Not found: ${uri}` };
|
|
return { ok: true, uri, mimeType: "application/yaml", content: readFileSafe(filePath) };
|
|
}
|
|
|
|
return { ok: false, error: `Unsupported uri: ${uri}` };
|
|
}
|