/** * Runs after `astro build` and completes cloudhal.dev's `dist/` (ADR-0037, ADR-0039): * * - `static/` (`_headers`, robots.txt) and `_redirects` from `src/lib/redirects.ts`; * - `/schemas/v1/.schema.json` at the permanent `$id` URLs, plus `index.json`; the build * fails when a schema's `$id` is not its URL here; * - every page as Markdown at `.md` and `/index.md`, links absolute; * - `/llms.txt`, `/llms-full.txt` and one pair per section (llmstxt.org); * - the docs MCP's search index (`/_mcp/search-index.json`, read by `src/worker`); * - the root-signed key list at `/.well-known/cloudhal/keys.json` and `.sig`, from the repo's * `security/` once the root ceremony has made them (RFC-0046 ยง9; `yanked.json` is served live by * `src/worker/trust.ts`). */ import { existsSync } from "node:fs"; import { cp, mkdir, readdir, readFile, writeFile } from "node:fs/promises"; import path from "node:path"; import { fileURLToPath } from "node:url"; import { allPages, type Page } from "../src/lib/pages.ts"; import { redirectLines } from "../src/lib/redirects.ts"; import { ORIGIN } from "../src/lib/repo-docs.ts"; import { buildSearchIndex, SEARCH_INDEX_PATH } from "../src/lib/search-index.ts"; import { SECTIONS, sectionOf, urlOf } from "../src/lib/sections.ts"; const APP = path.resolve(path.dirname(fileURLToPath(import.meta.url)), ".."); const REPO = path.resolve(APP, "../.."); const SCHEMAS = path.join(REPO, "packages/core-contracts/schemas"); const DIST = path.join(APP, "dist"); interface SchemaEntry { readonly name: string; readonly url: string; readonly title: string; } async function copySchemas(): Promise { const out = path.join(DIST, "schemas/v1"); await mkdir(out, { recursive: true }); const entries: SchemaEntry[] = []; for (const file of (await readdir(SCHEMAS)).filter((f) => f.endsWith(".schema.json")).sort()) { const text = await readFile(path.join(SCHEMAS, file), "utf8"); const schema = JSON.parse(text) as { $id?: string; title?: string }; const url = `${ORIGIN}/schemas/v1/${file}`; if (schema.$id !== url) throw new Error(`${file}: $id is ${schema.$id}, expected ${url}`); await writeFile(path.join(out, file), text); entries.push({ name: file, url, title: schema.title ?? file }); } await writeFile( path.join(out, "index.json"), `${JSON.stringify({ version: 1, schemas: entries }, null, 2)}\n`, ); return entries; } function markdownOf(page: Page): string { const front = [ "---", `title: ${JSON.stringify(page.title)}`, `description: ${JSON.stringify(page.description)}`, `url: ${urlOf(page)}`, ...(page.file ? [`source: https://github.com/cloudhal-dev/cloudhal/blob/main/${page.file}`] : []), "---", "", ]; return `${front.join("\n")}# ${page.title}\n\n${page.body.trim()}\n`; } async function writeMarkdown(pages: readonly Page[]): Promise { for (const page of pages) { const md = markdownOf(page); const base = page.id === "index" ? "index" : page.id; await mkdir(path.dirname(path.join(DIST, `${base}.md`)), { recursive: true }); await writeFile(path.join(DIST, `${base}.md`), md); if (page.id !== "index") { await mkdir(path.join(DIST, page.id), { recursive: true }); await writeFile(path.join(DIST, page.id, "index.md"), md); } } } const linkLine = (page: Page) => `- [${page.title}](${urlOf(page)}.md): ${page.description}`; function llmsIndex(pages: readonly Page[], schemas: readonly SchemaEntry[]): string { const lines = [ "# Cloudhal", "", "> Agent-native site platform: sites are typed graphs, agents change them through an MCP server behind a policy engine, and every change is a snapshot you can diff, preview and roll back. Runs on Cloudflare or self-hosted. Open source, TypeScript.", "", "Every page below is Markdown. The full text is in /llms-full.txt; each section also has its own llms.txt and llms-full.txt.", "", "## Start", "", ...pages.filter((p) => !p.id.startsWith("docs/") || p.id === "docs").map(linkLine), ]; for (const section of SECTIONS) { lines.push("", `## ${section.title}`, ""); lines.push(...pages.filter((p) => p.id !== "docs" && sectionOf(p) === section).map(linkLine)); } lines.push("", "## JSON Schemas (v1)", "", ...schemas.map((s) => `- [${s.title}](${s.url})`), ""); return lines.join("\n"); } const fullText = (pages: readonly Page[]) => pages.map(markdownOf).join("\n\n"); async function writeLlms(pages: readonly Page[], schemas: readonly SchemaEntry[]): Promise { await writeFile(path.join(DIST, "llms.txt"), llmsIndex(pages, schemas)); await writeFile(path.join(DIST, "llms-full.txt"), fullText(pages)); for (const section of SECTIONS) { const own = pages.filter((p) => p.id !== "docs" && sectionOf(p) === section); const dir = path.join(DIST, "docs", section.key); await mkdir(dir, { recursive: true }); await writeFile( path.join(dir, "llms.txt"), [`# Cloudhal: ${section.title}`, "", ...own.map(linkLine), ""].join("\n"), ); await writeFile(path.join(dir, "llms-full.txt"), fullText(own)); } } /** `security/keys.json` and `.sig`, when both exist, at `/.well-known/cloudhal/`. */ async function copyKeyList(): Promise { const files = ["keys.json", "keys.json.sig"].map((f) => path.join(REPO, "security", f)); if (!files.every((f) => existsSync(f))) return false; const out = path.join(DIST, ".well-known/cloudhal"); await mkdir(out, { recursive: true }); for (const f of files) await cp(f, path.join(out, path.basename(f))); return true; } await cp(path.join(APP, "static"), DIST, { recursive: true }); const keyList = await copyKeyList(); await writeFile(path.join(DIST, "_redirects"), `${redirectLines().join("\n")}\n`); const schemas = await copySchemas(); const pages = allPages(REPO, true); await writeMarkdown(pages); await writeLlms(pages, schemas); await mkdir(path.dirname(path.join(DIST, SEARCH_INDEX_PATH)), { recursive: true }); await writeFile(path.join(DIST, SEARCH_INDEX_PATH), JSON.stringify(buildSearchIndex(pages))); console.log( `cloudhal.dev: ${pages.length} pages, ${schemas.length} schemas${keyList ? ", keys.json" : ""}`, );