diff --git a/package.json b/package.json index f8db86fdf3..6342845cc6 100644 --- a/package.json +++ b/package.json @@ -7,7 +7,8 @@ "scripts": { "sync:orama": "pnpm build && node scripts/sync-orama.mjs", "dev": "vite dev", - "build": "vite build", + "build": "vite build && pnpm build:llms", + "build:llms": "node scripts/build-llms.mjs", "start": "vite start", "e2e": "npx playwright test", "format": "prettier . --write", diff --git a/scripts/build-llms.mjs b/scripts/build-llms.mjs new file mode 100644 index 0000000000..df941c56ad --- /dev/null +++ b/scripts/build-llms.mjs @@ -0,0 +1,52 @@ +// Post-processes the llms files that solidbase writes into the build output. +// The 1.x index is the canonical https://docs.solidjs.com/llms.txt; the first +// line under its title points tools at the Solid 2.0 files, which live on +// their own host until 2.0 is stable. +import { readFile, writeFile } from "node:fs/promises"; +import { dirname, resolve } from "node:path"; +import { fileURLToPath } from "node:url"; + +const ROOT = resolve(dirname(fileURLToPath(import.meta.url)), ".."); +const OUTPUT_DIR = resolve(ROOT, process.argv[2] ?? "dist"); +const V2_ORIGIN = "https://v2.solidjs.com"; +const V2_POINTER = `> Solid 2.0 (release candidate) documentation for tools: ${V2_ORIGIN}/llms.txt and ${V2_ORIGIN}/llms-full.txt`; +const FILES = ["llms.txt", "llms-full.txt"]; + +async function readIfPresent(filePath) { + try { + return await readFile(filePath, "utf8"); + } catch (error) { + if (error.code === "ENOENT") return undefined; + throw error; + } +} + +// Inserts the pointer as a blockquote directly under the level-one heading, +// where the llms.txt convention keeps its summary line. +function addV2Pointer(fileName, content) { + if (content.includes(V2_POINTER)) return content; + + const lines = content.split("\n"); + if (!lines[0]?.startsWith("# ")) { + throw new Error(`${fileName} must start with a level-one heading`); + } + + lines.splice(1, 0, "", V2_POINTER); + return lines.join("\n"); +} + +const updated = []; +for (const fileName of FILES) { + const filePath = resolve(OUTPUT_DIR, fileName); + const content = await readIfPresent(filePath); + if (content === undefined) continue; + + await writeFile(filePath, addV2Pointer(fileName, content), "utf8"); + updated.push(fileName); +} + +if (updated.length === 0) { + throw new Error(`No llms files found in ${OUTPUT_DIR}`); +} + +console.log(`Added the Solid 2.0 pointer to ${updated.join(" and ")}.`);