const fs = require('node:fs'); const path = require('node:path'); const { execSync } = require('node:child_process'); const SRC_DIR = path.join(__dirname, '../src'); const DIST_DIR = path.join(__dirname, '../dist'); const APPS_DIR = path.join(SRC_DIR, 'apps'); // Standard Navigation Template // Note: We use absolute paths (/) so they work from any depth, assuming Cloudflare Pages root. const NAV_TEMPLATE = ` `; function cleanDist() { if (fs.existsSync(DIST_DIR)) { fs.rmSync(DIST_DIR, { recursive: true, force: true }); } fs.mkdirSync(DIST_DIR); } function processHtml(filePath, outPath) { let content = fs.readFileSync(filePath, 'utf-8'); // Simple regex-based injection. // 1. Remove existing (if any) to avoid duplication. // 2. Inject new NAV_TEMPLATE at the start of . // Remove existing nav content = content.replace(//i, ''); // Inject new nav if (content.includes(']*)>/i, ` ${NAV_TEMPLATE}`); } else { // Fallback if no body tag found (unlikely for valid HTML) content = NAV_TEMPLATE + content; } fs.writeFileSync(outPath, content); console.log(`Processed ${filePath} -> ${outPath}`); } function buildLanding() { const landingSrc = path.join(__dirname, '..', 'src', 'landing', 'index.html'); const landingDist = path.join(DIST_DIR, 'index.html'); if (fs.existsSync(landingSrc)) { processHtml(landingSrc, landingDist); } else { console.error("Landing page not found!"); } } function buildApps() { if (!fs.existsSync(APPS_DIR)) return; const apps = fs.readdirSync(APPS_DIR); apps.forEach(app => { const appSrc = path.join(APPS_DIR, app); const appDist = path.join(DIST_DIR, app); if (!fs.statSync(appSrc).isDirectory()) return; fs.mkdirSync(appDist, { recursive: true }); // Check if it's a Vite app (has package.json and vite.config.ts) if (fs.existsSync(path.join(appSrc, 'package.json')) && fs.existsSync(path.join(appSrc, 'vite.config.ts'))) { console.log(`Building Vite app: ${app}...`); try { // Install dependencies if needed if (!fs.existsSync(path.join(appSrc, 'node_modules'))) { console.log(`Installing dependencies for ${app}...`); execSync('npm install', { cwd: appSrc, stdio: 'inherit' }); } // Build execSync('npm run build', { cwd: appSrc, stdio: 'inherit' }); // Copy dist contents const viteDist = path.join(appSrc, 'dist'); if (fs.existsSync(viteDist)) { fs.cpSync(viteDist, appDist, { recursive: true }); } else { console.error(`Vite build failed to produce 'dist' for ${app}`); } } catch (e) { console.error(`Failed to build ${app}:`, e); } } else { // Standard HTML app console.log(`Processing HTML app: ${app}...`); const files = fs.readdirSync(appSrc); files.forEach(file => { const srcFile = path.join(appSrc, file); const distFile = path.join(appDist, file); if (file.endsWith('.html')) { processHtml(srcFile, distFile); } else { fs.cpSync(srcFile, distFile, { recursive: true }); } }); } }); } function createRedirect(filename, target) { const content = ` Redirecting...

If you are not redirected automatically, follow this link.

`; const filePath = path.join(DIST_DIR, filename); fs.writeFileSync(filePath, content); console.log(`Created static redirect: ${filename} -> ${target}`); } function main() { console.log("Starting Build..."); cleanDist(); buildLanding(); buildApps(); // Create static redirects for legacy paths createRedirect('game.html', '/hexarow/'); createRedirect('german.html', '/german-trainer/'); console.log("Build Complete!"); } main();