← back to Dw Fleet Registry

fix-brand-names.mjs

45 lines

#!/usr/bin/env node
// Replace odd hardcoded header brand names (e.g. "Studio Castel") with the clean
// DOMAIN-derived name from site.config.json (no .com). Also updates the injected
// dw-header DwHeaderConfig siteName so the rebuilt header matches. Commits changed
// sites on the style-rebuild-pilot branch. Idempotent.
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; } };
const titleCase = s => s.replace(/(wallcoverings?|wallpapers?|walls?|fabric|covering)/gi, ' $1').replace(/\s+/g,' ').trim().replace(/\b\w/g, c => c.toUpperCase());

let changed = 0;
for (const slug of targets) {
  const dir = path.join(PROJECTS, slug);
  const idx = path.join(dir, 'public', 'index.html');
  if (!fs.existsSync(idx)) continue;
  // clean name = site.config.json siteName, else titleCased slug
  let name = titleCase(slug);
  try { const c = JSON.parse(fs.readFileSync(path.join(dir, 'site.config.json'), 'utf8')); if (c.siteName) name = c.siteName; } catch {}
  // if config siteName itself is an odd brand (Studio/Atelier/Maison/Casa…), fall back to domain
  if (/^(studio|atelier|maison|casa|house of|the)\b/i.test(name)) name = titleCase(slug);

  let html = fs.readFileSync(idx, 'utf8');
  const before = html;
  const esc = name.replace(/&/g, '&amp;').replace(/</g, '&lt;');
  // 1) old ns-name span text → clean name
  html = html.replace(/(<span class="ns-name"[^>]*>)[^<]*(<\/span>)/g, `$1${esc}$2`);
  // 2) the injected dw-header config siteName → clean name (keep header in sync)
  html = html.replace(/(window\.DwHeaderConfig=\{siteName:)("[^"]*"|'[^']*')/, `$1${JSON.stringify(name)}`);

  if (html !== before) {
    fs.writeFileSync(idx, html);
    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 "header name → domain name '${name}' (remove odd brand name)" && git rev-parse --short HEAD`, dir);
    changed++;
    console.log(`  ${slug.padEnd(24)} → "${name}"  ${c || ''}`);
  }
}
console.log(`\n${changed} sites' header names normalized to the domain name`);