Branchwell

PulseCanvas / website / scripts / prepare-deploy.mjs

1import { cp, mkdir, rm, writeFile } from 'node:fs/promises';
2import { relative, resolve, sep } from 'node:path';
3import { execFileSync } from 'node:child_process';
4import { seoPages, websiteUrl } from '../seo.config.js';
5import { mediaBaseUrl } from '../media.config.js';
6
7// Package only the built website; local captures and application files stay private.
8// Videos, installers and source archives are served from Cloudflare R2 (media.config.js) and
9// never ship in the deploy; /downloads/* redirects to the same path on the media host.
10const output = resolve('.vercel/output');
11const staticDirectory = resolve(output, 'static');
12await rm(output, { recursive: true, force: true });
13await mkdir(output, { recursive: true });
14await cp(resolve('dist'), staticDirectory, {
15 recursive: true,
16 filter: (source) => !/\.(mp4|m4v|mov|webm)$/i.test(source)
17 && relative(resolve('dist'), source).split(sep)[0] !== 'downloads',
18});
19await writeFile(resolve(output, 'config.json'), JSON.stringify({
20 version: 3,
21 routes: [
22 // Installed Creators builds and old direct downloads still use the legacy host.
23 {
24 src: '^(/(?!(?:appcast\\.xml|mac-release\\.json)$|downloads/|\\.well-known/).*)$',
25 has: [{ type: 'host', value: '^(?:www\\.)?pulsecanvas\\.si$' }],
26 status: 308,
27 headers: { Location: `${websiteUrl}$1` },
28 },
29 { src: '^/$', dest: '/index.html' },
30 { src: '^/(?:donate|tips|support)(?:/index\\.html|/)?$', status: 301, headers: { Location: '/help/' } },
31 // Old site video URLs (search results, shared links) point at the same file in R2.
32 { src: '^/media/(.+\\.mp4)$', status: 308, headers: { Location: `${mediaBaseUrl}/$1` } },
33 { src: '^/(appcast\\.xml|mac-release\\.json)$', headers: { 'Cache-Control': 'no-cache' }, continue: true },
34 { src: '^/downloads/(.+)$', status: 308, headers: { Location: `${mediaBaseUrl}/downloads/$1` } },
35 { src: '^/assets/(.*)$', headers: { 'Cache-Control': 'public, max-age=31536000, immutable' }, continue: true },
36 { src: '^/(images|media)/(.*)$', headers: { 'Cache-Control': 'public, max-age=86400' }, continue: true },
37 { src: '^/(.*)/index\\.html$', status: 308, headers: { Location: '/$1/' } },
38 { src: '^/index\\.html$', status: 308, headers: { Location: '/' } },
39 ...Object.keys(seoPages).filter(path => path !== '/').flatMap(path => [
40 { src: `^${path.slice(0, -1)}$`, status: 308, headers: { Location: path } },
41 { src: `^${path}$`, dest: `${path}index.html` },
42 ]),
43 { src: '^/404\\.html$', dest: '/404.html', status: 404 },
44 { handle: 'filesystem' },
45 { src: '^/.*$', dest: '/404.html', status: 404 },
46 ],
47}, null, 2) + '\n');
48// Generate the review from this exact package and fail if its assets are absent.
49execFileSync('python3', [resolve('scripts/create-og-review.py')], { stdio: 'inherit' });
50execFileSync('python3', [resolve('scripts/check-seo.py'), staticDirectory], { stdio: 'inherit' });
51console.log('Static Vercel deployment prepared in .vercel/output.');