import { readdir, rm, writeFile } from 'node:fs/promises'; import { join, resolve } from 'node:path'; import { execFileSync } from 'node:child_process'; import { mediaBaseUrl } from '../media.config.js'; // Cloudflare Pages build: turns the Vite output in dist/ into the deployed site. // Videos, installers and source archives are served from Cloudflare R2 (media.config.js) and // never ship in the deploy; /downloads/* redirects to the same path on the media host. // Pages itself redirects /page and /page/index.html to /page/ and serves 404.html with a 404. // The legacy pulsecanvas.si host is sent to pulsecanvas.ai by a Redirect Rule in the // pulsecanvas.si zone that leaves appcast.xml, mac-release.json, downloads/ and .well-known/ alone. const dist = resolve('dist'); async function prune(directory) { for (const entry of await readdir(directory, { withFileTypes: true })) { const path = join(directory, entry.name); if (entry.isDirectory()) await prune(path); else if (/\.(mp4|m4v|mov|webm)$/i.test(entry.name)) await rm(path); } } await rm(join(dist, 'downloads'), { recursive: true, force: true }); await prune(dist); await writeFile(join(dist, '_redirects'), [ '/terms /licenses/ 301', '/terms/ /licenses/ 301', '/terms/index.html /licenses/ 301', // Retired tip pages: PulseCanvas no longer takes tips. '/tips /help/ 301', '/tips/ /help/ 301', '/tips/index.html /help/ 301', '/donate /help/ 301', '/donate/ /help/ 301', '/donate/index.html /help/ 301', '/support /help/ 301', '/support/ /help/ 301', '/support/index.html /help/ 301', // Old site video URLs (search results, shared links) point at the same file in R2. `/media/* ${mediaBaseUrl}/:splat 308`, `/downloads/* ${mediaBaseUrl}/downloads/:splat 308`, '', ].join('\n')); await writeFile(join(dist, '_headers'), [ '/appcast.xml', ' Cache-Control: no-cache', '/mac-release.json', ' Cache-Control: no-cache', '/assets/*', ' Cache-Control: public, max-age=31536000, immutable', '/images/*', ' Cache-Control: public, max-age=86400', // Pages serves og-review.html at /og-review. '/og-review', ' X-Robots-Tag: noindex, nofollow', '/og-review.html', ' X-Robots-Tag: noindex, nofollow', '', ].join('\n')); // Keep download routing (and the removed /careers, answered with a 404) reserved for Pages Functions; never route ordinary site visits. await writeFile(join(dist, '_routes.json'), JSON.stringify({ version: 1, include: ['/downloads/*', '/careers', '/careers/'], exclude: [], }, null, 2) + '\n'); // Generate the review from this exact package and fail if its assets are absent. execFileSync('python3', [resolve('scripts/create-og-review.py'), dist], { stdio: 'inherit' }); execFileSync('python3', [resolve('scripts/check-seo.py'), dist], { stdio: 'inherit' }); console.log('Cloudflare Pages site prepared in dist/.');