← back to Dw Fleet Registry
fix-header-inject.mjs
59 lines
#!/usr/bin/env node
// FIX: the fanout injected dw-header before the FIRST '</body>' — which on many sites
// is a fake </body> inside a doc-comment ("Inject before … </body> in every DW-family
// sister site."). Result: the dw-header <script> tags sit INSIDE the comment → never run
// → old header shows. This removes the mis-placed block and re-injects it before the
// REAL final </body>, outside any comment. Preserves each site's existing DwHeaderConfig.
import fs from 'node:fs';
import os from 'node:os';
import path from 'node:path';
import { execSync } from 'node:child_process';
const PROJECTS = path.join(os.homedir(), 'Projects');
const targets = fs.readFileSync('/tmp/fleet-targets.txt', 'utf8').split('\n').filter(Boolean);
const sh = (c, cwd) => { try { return execSync(c, { cwd, stdio: ['ignore','pipe','pipe'] }).toString().trim(); } catch { return null; } };
let fixed = 0, ok = 0;
for (const slug of targets) {
const idx = path.join(PROJECTS, slug, 'public', 'index.html');
if (!fs.existsSync(idx)) continue;
let html = fs.readFileSync(idx, 'utf8');
if (!html.includes('/dw-header.js')) continue;
// preserve the existing per-site config
const cfgM = html.match(/<script>\s*window\.DwHeaderConfig=\{[\s\S]*?\};?\s*<\/script>/);
const cfg = cfgM ? cfgM[0] : null;
// is it already correctly placed? (script tag appears AFTER the last real </body>-12 chars, i.e. just before final </body>, and NOT inside a comment)
const lastBody = html.lastIndexOf('</body>');
const scriptIdx = html.lastIndexOf('<script src="/dw-header.js"');
const alreadyOk = scriptIdx > 0 && scriptIdx < lastBody && (lastBody - scriptIdx) < 400 && !insideComment(html, scriptIdx);
if (alreadyOk) { ok++; continue; }
// strip every existing config script + dw-header script tag (wherever they are, incl. inside the comment)
html = html.replace(/<script>\s*window\.DwHeaderConfig=\{[\s\S]*?\};?\s*<\/script>\s*/g, '');
html = html.replace(/<script src="\/dw-header\.js"[^>]*><\/script>\s*/g, '');
// re-inject before the REAL final </body>
const cfgBlock = (cfg || `<script>window.DwHeaderConfig={siteName:${JSON.stringify(slug)}};</script>`);
const inject = `${cfgBlock}\n<script src="/dw-header.js" defer></script>\n`;
const lb = html.lastIndexOf('</body>');
if (lb < 0) { console.log(` ${slug}: no </body>?! skipped`); continue; }
html = html.slice(0, lb) + inject + html.slice(lb);
fs.writeFileSync(idx, html);
const dir = path.join(PROJECTS, slug);
sh('git checkout -B style-rebuild-pilot', dir);
sh('git add public/index.html', dir);
const c = sh(`git -c user.email="steve@designerwallcoverings.com" -c user.name="Steve Abrams" commit -q -m "FIX: dw-header was injected inside a doc-comment (never ran); re-inject before the real </body>" && git rev-parse --short HEAD`, dir);
fixed++;
console.log(` fixed ${slug.padEnd(24)} ${c || ''}`);
}
function insideComment(s, i) {
const open = s.lastIndexOf('<!--', i), close = s.lastIndexOf('-->', i);
return open > close; // last <!-- is after last --> before i → we're inside a comment
}
console.log(`\n${fixed} sites re-injected · ${ok} already correctly placed`);