diff --git a/scripts/verify-anchor-aliases-in-dist.mjs b/scripts/verify-anchor-aliases-in-dist.mjs new file mode 100644 index 0000000..7960820 --- /dev/null +++ b/scripts/verify-anchor-aliases-in-dist.mjs @@ -0,0 +1,63 @@ +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)`);