familyfedie-website/scripts/copy-static-assets.mjs
Loyyd f85aca7d58
Some checks are pending
/ deploy (push) Waiting to run
Remove .html from internal links
2026-07-02 19:44:40 +02:00

66 lines
2.2 KiB
JavaScript

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.`);