← back to Dw Sarah Rowland Searchfix
build-titles.mjs
24 lines
import { gql } from './lib.mjs'; import fs from 'node:fs';
const products=JSON.parse(fs.readFileSync(new URL('./products.json',import.meta.url)));
const generic=products.filter(p=>p.title.trim()==='Wallcovering | Architectural Wallcoverings');
const colorMap=Object.fromEntries(fs.readFileSync(new URL('./color-map.tsv',import.meta.url),'utf8')
.trim().split('\n').filter(Boolean).map(l=>l.split('\t')));
const titleCase=(s)=>s.split(' ').map(w=>w?w[0].toUpperCase()+w.slice(1):w).join(' ');
const plan=[];
for(const p of generic){
const d=(await gql(`query($id:ID!){product(id:$id){handle variants(first:1){nodes{sku}}}}`,{id:p.id})).product;
const sku=d.variants.nodes[0]?.sku;
// handle -> pattern+colorway: strip prefix up to 'sarah-rowland-' and trailing '-srd-xxx-NN'
let mid=d.handle.replace(/^.*sarah-rowland-/,'').replace(/-srd-[a-z0-9]+-\d+.*$/,'').replace(/-(sample|architectural-wallcoverings).*$/,'');
const name=titleCase(mid.replace(/-/g,' ').trim());
const fam=colorMap[sku]||'';
const title = fam ? `${name} - ${fam} Commercial Wallcovering | Koroseal`
: `${name} Commercial Wallcovering | Koroseal`;
plan.push({id:p.id, sku, handle:d.handle, title});
}
fs.writeFileSync(new URL('./title-plan.json',import.meta.url), JSON.stringify(plan,null,2));
console.log(`Proposed titles (${plan.length}):\n`);
plan.forEach(x=>console.log(` ${x.sku} ${x.title}`));
const noFam=plan.filter(x=>!/ - /.test(x.title));
if(noFam.length) console.log(`\n⚠ ${noFam.length} without a color family (no DB color_name): ${noFam.map(x=>x.sku).join(', ')}`);