fix(coding-agent): inline embedded docs to avoid global install path breakage

This commit is contained in:
can1357
2026-02-20 06:08:43 +01:00
parent 880891b013
commit cd5676a36d
2 changed files with 55 additions and 129 deletions
@@ -5,24 +5,7 @@ import * as path from "node:path";
const docsDir = new URL("../../../docs/", import.meta.url).pathname;
const outputPath = new URL("../src/internal-urls/docs-index.generated.ts", import.meta.url).pathname;
const importBase = "../../../../docs";
function toIdentifier(relativePath: string): string {
const withoutExt = relativePath.replace(/\.md$/i, "");
const parts = withoutExt
.split(/[^a-zA-Z0-9]+/)
.filter(Boolean)
.map((part, index) => {
if (index === 0) {
return part.toLowerCase();
}
return part.charAt(0).toUpperCase() + part.slice(1).toLowerCase();
});
const base = parts.length > 0 ? parts.join("") : "doc";
const safeBase = /^[0-9]/.test(base) ? `doc${base}` : base;
return `${safeBase}Md`;
}
const glob = new Glob("**/*.md");
const entries: string[] = [];
@@ -31,26 +14,17 @@ for await (const relativePath of glob.scan(docsDir)) {
}
entries.sort();
const usedIdentifiers = new Set<string>();
const docs = entries.map((relativePath) => {
let identifier = toIdentifier(relativePath);
let suffix = 2;
while (usedIdentifiers.has(identifier)) {
identifier = `${toIdentifier(relativePath)}${suffix}`;
suffix++;
}
usedIdentifiers.add(identifier);
return { relativePath, identifier };
});
docs.sort((a, b) => a.identifier.localeCompare(b.identifier) || a.relativePath.localeCompare(b.relativePath));
const docs = [...entries];
const imports = docs
.map(({ relativePath, identifier }) => `import ${identifier} from "${importBase}/${relativePath}" with { type: "text" };`)
.join("\n");
const mapEntries = await Promise.all(
docs.map(async (relativePath) => {
const content = await Bun.file(path.join(docsDir, relativePath)).text();
return `\t"${relativePath}": ${JSON.stringify(content)},`;
}),
);
const mapEntries = docs.map(({ relativePath, identifier }) => `\t"${relativePath}": ${identifier},`).join("\n");
const output = `// Auto-generated by scripts/generate-docs-index.ts - DO NOT EDIT\n\n${imports}\n\nexport const EMBEDDED_DOCS: Readonly<Record<string, string>> = {\n${mapEntries}\n};\n\nexport const EMBEDDED_DOC_FILENAMES = Object.keys(EMBEDDED_DOCS).sort();\n`;
const output = `// Auto-generated by scripts/generate-docs-index.ts - DO NOT EDIT\n\nexport const EMBEDDED_DOCS: Readonly<Record<string, string>> = {\n${mapEntries.join("\n")}\n};\n\nexport const EMBEDDED_DOC_FILENAMES = Object.keys(EMBEDDED_DOCS).sort();\n`;
await Bun.write(outputPath, output);
console.log(`Generated ${path.relative(process.cwd(), outputPath)} (${docs.length} docs)`);