import crypto from 'node:crypto'; import fs from 'node:fs'; import path from 'node:path'; import { fileURLToPath, pathToFileURL } from 'node:url'; const projectRoot = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..'); const HEADER_RE = /^\/\* Hope Volunteer brand tokens[\s\S]*?\*\/\r?\n+/; const LICENSE_NAMES = new Set(['OFL.txt', 'OFL.md', 'LICENSE', 'LICENSE.txt', 'LICENSE.md']); /** * Copy tokens.css and fonts from BRAND_SRC into public/brand. * This is a development and build-prep step, not a runtime dependency. * A missing source keeps the files already in public/brand. */ export function syncBrand({ source, dest, syncedAt }) { const resolvedSource = path.resolve(source); const resolvedDest = path.resolve(dest); const warnings = []; if (!fs.existsSync(resolvedSource) || !fs.statSync(resolvedSource).isDirectory()) { warnings.push( `Brand source not found at ${resolvedSource}. Keeping the existing assets in ${resolvedDest}.`, ); return { copied: false, warnings, fonts: [] }; } fs.mkdirSync(resolvedDest, { recursive: true }); const tokensPath = path.join(resolvedSource, 'tokens.css'); if (!isFile(tokensPath)) { warnings.push(`No tokens.css in ${resolvedSource}. Keeping the current tokens.css.`); } else { const css = fs.readFileSync(tokensPath, 'utf8'); const body = css.replace(HEADER_RE, ''); const header = `/* Hope Volunteer brand tokens\n Source: ${resolvedSource}\n Synced: ${syncedAt}\n Generated by scripts/sync-brand.mjs. Do not add this header to font files.\n*/\n`; replaceFile(path.join(resolvedDest, 'tokens.css'), header + body); } const fontsDest = path.join(resolvedDest, 'fonts'); fs.mkdirSync(fontsDest, { recursive: true }); const fonts = []; const fontsSource = path.join(resolvedSource, 'fonts'); if (fs.existsSync(fontsSource) && fs.statSync(fontsSource).isDirectory()) { for (const entry of fs.readdirSync(fontsSource, { withFileTypes: true })) { if (!entry.isFile()) continue; const from = path.join(fontsSource, entry.name); const to = path.join(fontsDest, entry.name); replaceFile(to, fs.readFileSync(from)); fonts.push(fileRecord(entry.name, to)); } } const licenses = []; for (const name of LICENSE_NAMES) { const from = path.join(resolvedSource, name); if (!isFile(from)) continue; const to = path.join(resolvedDest, name); replaceFile(to, fs.readFileSync(from)); licenses.push(fileRecord(name, to)); } const manifest = { source: resolvedSource, syncedAt, note: fonts.length === 0 ? 'No self-hosted font files were in the source. The app falls back to system-ui and cursive. Files already in the destination that were not in the source were left in place.' : 'Files already in the destination that were not in the source were left in place.', fonts, licenses, }; replaceFile(path.join(resolvedDest, 'fonts.manifest.json'), `${JSON.stringify(manifest, null, 2)}\n`); return { copied: true, warnings, fonts }; } function replaceFile(destination, contents) { const temporary = `${destination}.next`; fs.writeFileSync(temporary, contents); fs.renameSync(temporary, destination); } function fileRecord(file, fullPath) { const bytes = fs.readFileSync(fullPath); return { file, bytes: bytes.length, sha256: crypto.createHash('sha256').update(bytes).digest('hex'), }; } function isFile(file) { return fs.existsSync(file) && fs.statSync(file).isFile(); } function readEnvFile(file) { if (!fs.existsSync(file)) return; for (const line of fs.readFileSync(file, 'utf8').split('\n')) { const trimmed = line.trim(); if (trimmed === '' || trimmed.startsWith('#')) continue; const eq = trimmed.indexOf('='); if (eq === -1) continue; const key = trimmed.slice(0, eq).trim(); let value = trimmed.slice(eq + 1).trim(); if ( (value.startsWith('"') && value.endsWith('"')) || (value.startsWith("'") && value.endsWith("'")) ) { value = value.slice(1, -1); } if (process.env[key] === undefined) process.env[key] = value; } } const entry = process.argv[1]; if (entry && import.meta.url === pathToFileURL(path.resolve(entry)).href) { readEnvFile(path.join(projectRoot, '.env')); const source = process.env.BRAND_SRC ?? '../Hope Food Pantry Website/brand'; const result = syncBrand({ source, dest: path.join(projectRoot, 'public/brand'), syncedAt: new Date().toISOString(), }); for (const warning of result.warnings) console.warn(warning); if (result.copied) console.log(`Brand sync wrote ${result.fonts.length} font file(s).`); }