import fs from "node:fs"; import path from "node:path"; const repoRoot = process.cwd(); const distRoot = path.join(repoRoot, "dist"); const staticDirs = ["assets", "css", "js"]; function copyDir(source, destination) { fs.rmSync(destination, { recursive: true, force: true }); fs.mkdirSync(path.dirname(destination), { recursive: true }); fs.cpSync(source, destination, { recursive: true }); } function listHtmlFiles(dir) { return fs.readdirSync(dir, { withFileTypes: true }).flatMap((entry) => { const fullPath = path.join(dir, entry.name); if (entry.isDirectory()) { return listHtmlFiles(fullPath); } return entry.isFile() && entry.name.endsWith(".html") ? [fullPath] : []; }); } function materializeExtensionlessHtmlRoutes() { let routes = 0; for (const filePath of listHtmlFiles(distRoot)) { const relativePath = path.relative(distRoot, filePath); const extensionlessPath = relativePath.replace(/\.html$/, ""); const destination = path.join(distRoot, extensionlessPath, "index.html"); if (path.resolve(destination) === path.resolve(filePath)) { continue; } fs.mkdirSync(path.dirname(destination), { recursive: true }); fs.copyFileSync(filePath, destination); routes += 1; } return routes; } for (const dir of staticDirs) { copyDir(path.join(repoRoot, dir), path.join(distRoot, dir)); } const speechIndexSource = path.join(distRoot, "speeches.html"); const speechIndexDestination = path.join(distRoot, "speeches", "index.html"); const adminIndexSource = path.join(distRoot, "admin.html"); const adminIndexDestination = path.join(distRoot, "admin", "index.html"); if (fs.existsSync(speechIndexSource)) { fs.mkdirSync(path.dirname(speechIndexDestination), { recursive: true }); fs.copyFileSync(speechIndexSource, speechIndexDestination); } if (fs.existsSync(adminIndexSource)) { fs.mkdirSync(path.dirname(adminIndexDestination), { recursive: true }); fs.copyFileSync(adminIndexSource, adminIndexDestination); } const extensionlessRoutes = materializeExtensionlessHtmlRoutes(); console.log(`Copied ${staticDirs.join(", ")} into dist/.`); console.log(`Materialized ${extensionlessRoutes} extensionless HTML route aliases.`);