/**
* Replaces CDN references in HTML files with local /vendor/ paths.
* Usage: bun run scripts/patch-cdn-to-vendor.ts
*/
import { readdirSync, readFileSync, writeFileSync } from "fs";
import path from "path";
const PUBLIC = path.resolve(import.meta.dir, "..", "public");
// ── Replacement patterns ───────────────────────────────────────────
type Replacement = [search: string | RegExp, replace: string];
const replacements: Replacement[] = [
// ── Google Fonts preconnect lines (remove entirely) ──
[/\s*\s*\n/g, "\n"],
[/\s*\s*\n/g, ""],
// ── Google Fonts Inter 400;600;700 → local fontsource ──
[
/\s*\s*\n?/g,
`\n \n \n \n`,
],
// ── Google Fonts Inter 400;500;600;700 (Desktop variant) ──
[
//g,
`\n \n `,
],
// ── Google Fonts Inter + JetBrains Mono (console.html) ──
[
/\s*\s*\n?/g,
`\n \n \n \n \n \n`,
],
// ── Font Awesome CSS ──
[
/\s*/g,
`\n `,
],
// Single-line variant (Desktop)
[
//g,
``,
],
// ── Flag Icons CSS ──
[
/\s*/g,
`\n `,
],
// ── anime.js ──
[
/\s*`,
],
// ── msgpackr ──
[
/`,
],
// ── highlight.js CSS ──
[
/\s*/g,
`\n `,
],
// ── highlight.js core + all language scripts → single bundle ──
[
/`,
],
// ── CodeMirror CSS ──
[
/\s*/g,
`\n `,
],
[
/\s*/g,
`\n `,
],
// ── CodeMirror JS ──
[
/`,
],
[
/`,
],
[
/`,
],
[
/`,
],
// ── Ace Editor ──
[
/`,
],
// ── Chart.js ──
[
/`,
],
// ── Leaflet CSS ──
[
/\s*/g,
`\n `,
],
// ── Leaflet JS ──
[
/`,
],
];
// ── Apply to all HTML files ────────────────────────────────────────
const htmlFiles = readdirSync(PUBLIC)
.filter((f) => f.endsWith(".html"))
.map((f) => path.join(PUBLIC, f));
let totalChanges = 0;
for (const file of htmlFiles) {
let content = readFileSync(file, "utf-8");
const original = content;
for (const [search, replace] of replacements) {
content = content.replace(search, replace);
}
if (content !== original) {
writeFileSync(file, content, "utf-8");
const name = path.basename(file);
console.log(` ✓ ${name}`);
totalChanges++;
}
}
console.log(`\nPatched ${totalChanges} HTML files.`);