#!/usr/bin/env node // One-shot codemod: rewrite every "/portraits/." reference in // the app source to its Cloudflare Images delivery URL. hero-* images use // the high-res `print` variant; everything else uses `public`. Only edits // the three app files that reference portraits — NOT scripts/ (those read // the local source files). Idempotent: skips a file already migrated. import fs from 'node:fs'; import path from 'node:path'; import { fileURLToPath } from 'node:url'; const ROOT = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..'); function loadEnvLocal() { const p = path.join(ROOT, '.env.local'); for (const raw of fs.readFileSync(p, 'utf8').split('\n')) { const line = raw.trim(); if (!line || line.startsWith('#')) continue; const eq = line.indexOf('='); if (eq < 0) continue; const k = line.slice(0, eq).trim(); let v = line.slice(eq + 1).trim(); if ((v.startsWith('"') && v.endsWith('"')) || (v.startsWith("'") && v.endsWith("'"))) v = v.slice(1, -1); if (!(k in process.env)) process.env[k] = v; } } loadEnvLocal(); const HASH = process.env.CF_IMAGES_HASH; if (!HASH) { console.error('CF_IMAGES_HASH missing'); process.exit(1); } const CDN = `https://imagedelivery.net/${HASH}`; const TARGETS = ['app/page.tsx', 'app/data/styleCatalog.ts', 'app/data/transformPairs.ts']; const RE = /\/portraits\/([A-Za-z0-9._\-/]+?\.(?:png|jpe?g|webp))/g; let total = 0; for (const rel of TARGETS) { const file = path.join(ROOT, rel); let s = fs.readFileSync(file, 'utf8'); if (s.includes('imagedelivery.net')) { console.log(` skip (already migrated): ${rel}`); continue; } let n = 0; s = s.replace(RE, (_m, p1) => { n++; const variant = p1.split('/').pop().startsWith('hero-') ? 'print' : 'public'; return `${CDN}/portraits/${p1}/${variant}`; }); fs.writeFileSync(file, s); total += n; console.log(` ${rel}: ${n} refs rewritten`); } console.log(`Total: ${total} references → ${CDN}/portraits//`);