/** * Packages `blume build` output for the assets-only Cloudflare Worker. * * Two things have to be reconciled: * * 1. `deployment.base: '/docs'` makes Blume emit URLs like `/docs/foo`, but it * does NOT nest the output — pages land in `dist/foo/index.html`. The worker * is asked for `/docs/foo` (the website proxies the full path through), so * the build is moved under `build/docs/` to line up with the served paths. * * 2. Astro's own `redirects` are unusable under a base: it prefixes a * redirect's source but not its target, emitting `url=/guides` rather than * `/docs/guides`. So the redirects are written here as a Cloudflare * `_redirects` file, which must sit at the root of the assets directory. */ import { readdirSync } from 'node:fs'; import { cp, mkdir, rm, 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 dist = resolve(root, 'dist'); const out = resolve(root, 'build'); const content = resolve(root, 'content/docs'); const slugs = (dir) => readdirSync(resolve(content, dir), { withFileTypes: true }) .filter((e) => e.isFile() && e.name.endsWith('.mdx') && e.name !== 'index.mdx') .map((e) => e.name.replace(/\.mdx$/, '')); // Guides moved from `/docs/` to `/docs/guides/` (Blume scopes a tab's // sidebar by route prefix). The old URLs are public, so forward them. const rootSlugs = new Set(slugs('.')); const GUIDE_REDIRECTS = slugs('guides') // A slug that is also a root page would shadow it — `_redirects` beats assets. .filter((slug) => !rootSlugs.has(slug)) .map((slug) => `/docs/${slug} /docs/guides/${slug} 301`) .join('\n'); const REDIRECTS = `# Generated by scripts/package-assets.mjs — do not edit. /docs/ /docs 301 /docs/home /docs 301 /docs/how-to-guides/* /docs/:splat 301 /docs/api/reference /docs/api 301 /docs/contributor-guides /docs/guides/contributing 301 /docs/contributor-guides/* /docs/guides/contributing/:splat 301 /docs/self-hosted/configuration/sso /docs/self-hosted/enterprise 301 ${GUIDE_REDIRECTS} `; await rm(out, { recursive: true, force: true }); await mkdir(out, { recursive: true }); await cp(dist, resolve(out, 'docs'), { recursive: true }); await writeFile(resolve(out, '_redirects'), REDIRECTS); console.log('[docs] Packaged dist/ -> build/docs/ with _redirects');