Files
archicratie-edition/scripts/verify-anchor-aliases-in-dist.mjs
Archicratia 800226a404
All checks were successful
CI / build-and-anchors (push) Successful in 54s
ci: verify anchor aliases are injected into dist
2026-01-23 09:47:25 +01:00

64 lines
1.8 KiB
JavaScript
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import fs from "node:fs";
import path from "node:path";
const CWD = process.cwd();
const ALIASES_PATH = path.join(CWD, "src", "anchors", "anchor-aliases.json");
const DIST_DIR = path.join(CWD, "dist");
if (!fs.existsSync(ALIASES_PATH)) {
console.log(" Pas d'aliases => skip verify.");
process.exit(0);
}
if (!fs.existsSync(DIST_DIR)) {
console.error("❌ dist/ introuvable. Lance d'abord le build.");
process.exit(1);
}
const aliases = JSON.parse(fs.readFileSync(ALIASES_PATH, "utf8"));
let checked = 0;
let failed = 0;
for (const [route, mapping] of Object.entries(aliases)) {
const htmlPath = path.join(DIST_DIR, route.replace(/^\//, ""), "index.html");
if (!fs.existsSync(htmlPath)) {
console.error(`❌ HTML introuvable pour route=${route} => ${htmlPath}`);
failed++;
continue;
}
const html = fs.readFileSync(htmlPath, "utf8");
for (const [oldId, newId] of Object.entries(mapping)) {
checked++;
const oldNeedle = `id="${oldId}"`;
const newNeedle = `id="${newId}"`;
const oldPos = html.indexOf(oldNeedle);
const newPos = html.indexOf(newNeedle);
if (newPos === -1) {
console.error(`❌ newId absent: route=${route} newId=${newId}`);
failed++;
continue;
}
if (oldPos === -1) {
console.error(`❌ alias oldId non injecté: route=${route} oldId=${oldId} (newId=${newId})`);
failed++;
continue;
}
if (oldPos > newPos) {
console.error(`❌ alias placé APRÈS newId: route=${route} oldId=${oldId} newId=${newId}`);
failed++;
continue;
}
}
}
if (failed) {
console.error(`❌ verify-anchor-aliases-in-dist: failed=${failed} checked=${checked}`);
process.exit(1);
}
console.log(`✅ verify-anchor-aliases-in-dist: checked=${checked} (all good)`);