c6f3eff8c4
Static imports from '../../../../docs/' resolve correctly in the monorepo but break when the package is installed from npm -- the docs/ directory at the repo root is not included in the published tarball. Switch the generator to read each file at build time and emit the content as inlined string literals, eliminating all runtime file resolution.
41 lines
1.2 KiB
TypeScript
41 lines
1.2 KiB
TypeScript
#!/usr/bin/env bun
|
|
|
|
import { Glob } from "bun";
|
|
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 glob = new Glob("**/*.md");
|
|
const entries: string[] = [];
|
|
for await (const relativePath of glob.scan(docsDir)) {
|
|
entries.push(relativePath.split(path.sep).join("/"));
|
|
}
|
|
entries.sort();
|
|
|
|
const docsWithContent = await Promise.all(
|
|
entries.map(async (relativePath) => ({
|
|
relativePath,
|
|
content: await Bun.file(path.join(docsDir, relativePath)).text(),
|
|
}))
|
|
);
|
|
|
|
const filenamesLiteral = JSON.stringify(entries);
|
|
|
|
const mapEntries = docsWithContent
|
|
.map(({ relativePath, content }) => `\t${JSON.stringify(relativePath)}: ${JSON.stringify(content)},`)
|
|
.join("\n");
|
|
const output = [
|
|
"// Auto-generated by scripts/generate-docs-index.ts - DO NOT EDIT",
|
|
"",
|
|
`export const EMBEDDED_DOC_FILENAMES: readonly string[] = ${filenamesLiteral};`,
|
|
"",
|
|
`export const EMBEDDED_DOCS: Readonly<Record<string, string>> = {`,
|
|
`${mapEntries}`,
|
|
`};`,
|
|
"",
|
|
].join("\n");
|
|
|
|
await Bun.write(outputPath, output);
|
|
console.log(`Generated ${path.relative(process.cwd(), outputPath)} (${entries.length} docs)`);
|