← back to Commercialrealestate

scripts/apply-crcp-theme.js

30 lines

#!/usr/bin/env node
// TK-10703 Phase 2: idempotently add the canonical CRCP day/night theme to the theme-less pages.
// ADDITIVE ONLY — inserts 3 things and touches nothing else:
//   1) a pre-paint head boot that applies the saved theme (key 'crcp-theme', default dark)
//   2) <link href="/crcp-theme.css"> right before </head> (loads LAST so its html[data-theme] tokens win)
//   3) <script src="/crcp-theme.js" defer> before </body> (auto-injects the ☀️/🌙 toggle into the header)
// Skips any page already carrying crcp-theme.js (so the 5 already-themed pages are untouched).
const fs = require('fs'), path = require('path');
const PUB = path.join(__dirname, '..', 'public');
const PAGES = process.argv.slice(2);
const BOOT = `<script>(function(){try{var t=localStorage.getItem('crcp-theme');document.documentElement.setAttribute('data-theme',t==='light'?'light':'dark');}catch(e){document.documentElement.setAttribute('data-theme','dark');}})();</script>`;
const CSS  = `  <link rel="stylesheet" href="/crcp-theme.css"><!-- CRCP day/night tokens: load LAST so html[data-theme] wins -->`;
const JS   = `  <script src="/crcp-theme.js" defer></script><!-- CRCP day/night toggle (auto-injects into header) -->`;
let done = 0, skip = 0, miss = 0;
for (const name of PAGES) {
  const p = path.join(PUB, name + '.html');
  if (!fs.existsSync(p)) { console.log(`MISS  ${name}`); miss++; continue; }
  let h = fs.readFileSync(p, 'utf8');
  if (h.includes('crcp-theme.js')) { console.log(`SKIP  ${name} (already themed)`); skip++; continue; }
  const before = h;
  if (h.includes('<head>')) h = h.replace('<head>', '<head>\n' + BOOT);
  else { console.log(`MISS  ${name} (no <head>)`); miss++; continue; }
  if (!h.includes('crcp-theme.css')) h = h.replace('</head>', CSS + '\n</head>');
  h = h.replace('</body>', JS + '\n</body>');
  if (h === before) { console.log(`SKIP  ${name} (no anchors changed)`); skip++; continue; }
  fs.writeFileSync(p, h);
  console.log(`THEME ${name}`); done++;
}
console.log(`\n-- themed:${done} skipped:${skip} missing:${miss} --`);