Branchwell

PulseCanvas / website / scripts / prepare-cloudflare.mjs

1import { readdir, rm, writeFile } from 'node:fs/promises';
2import { join, resolve } from 'node:path';
3import { execFileSync } from 'node:child_process';
4import { mediaBaseUrl } from '../media.config.js';
5
6// Cloudflare Pages build: turns the Vite output in dist/ into the deployed site.
7// Videos, installers and source archives are served from Cloudflare R2 (media.config.js) and
8// never ship in the deploy; /downloads/* redirects to the same path on the media host.
9// Pages itself redirects /page and /page/index.html to /page/ and serves 404.html with a 404.
10// The legacy pulsecanvas.si host is sent to pulsecanvas.ai by a Redirect Rule in the
11// pulsecanvas.si zone that leaves appcast.xml, mac-release.json, downloads/ and .well-known/ alone.
12const dist = resolve('dist');
13
14async function prune(directory) {
15 for (const entry of await readdir(directory, { withFileTypes: true })) {
16 const path = join(directory, entry.name);
17 if (entry.isDirectory()) await prune(path);
18 else if (/\.(mp4|m4v|mov|webm)$/i.test(entry.name)) await rm(path);
19 }
20}
21await rm(join(dist, 'downloads'), { recursive: true, force: true });
22await prune(dist);
23
24await writeFile(join(dist, '_redirects'), [
25 '/terms /licenses/ 301',
26 '/terms/ /licenses/ 301',
27 '/terms/index.html /licenses/ 301',
28 // Retired tip pages: PulseCanvas no longer takes tips.
29 '/tips /help/ 301',
30 '/tips/ /help/ 301',
31 '/tips/index.html /help/ 301',
32 '/donate /help/ 301',
33 '/donate/ /help/ 301',
34 '/donate/index.html /help/ 301',
35 '/support /help/ 301',
36 '/support/ /help/ 301',
37 '/support/index.html /help/ 301',
38 // Old site video URLs (search results, shared links) point at the same file in R2.
39 `/media/* ${mediaBaseUrl}/:splat 308`,
40 `/downloads/* ${mediaBaseUrl}/downloads/:splat 308`,
41 '',
42].join('\n'));
43
44await writeFile(join(dist, '_headers'), [
45 '/appcast.xml',
46 ' Cache-Control: no-cache',
47 '/mac-release.json',
48 ' Cache-Control: no-cache',
49 '/assets/*',
50 ' Cache-Control: public, max-age=31536000, immutable',
51 '/images/*',
52 ' Cache-Control: public, max-age=86400',
53 // Pages serves og-review.html at /og-review.
54 '/og-review',
55 ' X-Robots-Tag: noindex, nofollow',
56 '/og-review.html',
57 ' X-Robots-Tag: noindex, nofollow',
58 '',
59].join('\n'));
60
61// Keep download routing (and the removed /careers, answered with a 404) reserved for Pages Functions; never route ordinary site visits.
62await writeFile(join(dist, '_routes.json'), JSON.stringify({
63 version: 1, include: ['/downloads/*', '/careers', '/careers/'], exclude: [],
64}, null, 2) + '\n');
65
66// Generate the review from this exact package and fail if its assets are absent.
67execFileSync('python3', [resolve('scripts/create-og-review.py'), dist], { stdio: 'inherit' });
68execFileSync('python3', [resolve('scripts/check-seo.py'), dist], { stdio: 'inherit' });
69console.log('Cloudflare Pages site prepared in dist/.');