← back to Reidwitlin Landing
scripts/fix-colorway-labels.js
40 lines
#!/usr/bin/env node
/**
* One-off, DB-free repair of the CURRENT data/products.json.
*
* The full rebuild (build-rwltd-data.js) reads the authoritative Kamatera
* dw_unified over ssh, which is gated. This patcher applies the exact same
* colorway-label repair (lib/relabel-colorways.js) to the snapshot already on
* disk, so the fix ships without a prod-DB round-trip. build-rwltd-data.js also
* runs the repair, so future rebuilds stay correct.
*
* Writes a timestamped .bak beside products.json first. $0 (local).
*/
const fs = require('fs');
const path = require('path');
const { relabelUnreliableSeries } = require('../lib/relabel-colorways');
const OUT = path.join(__dirname, '..', 'data', 'products.json');
const snap = JSON.parse(fs.readFileSync(OUT, 'utf8'));
const before = snap.products.map(p => ({ handle: p.handle, color: p.color, title: p.title }));
const rl = relabelUnreliableSeries(snap.products);
// Show a sample of what changed so the diff is auditable, not silent.
const changed = snap.products.filter((p, i) => p.color !== before[i].color);
console.log(`relabelled ${rl.productsFixed} products across ${rl.seriesFixed} series: ${rl.series.join(', ')}`);
for (const p of changed.slice(0, 8)) {
const b = before.find(x => x.handle === p.handle);
console.log(` ${p.handle}: "${b.color}" -> "${p.color}" (hex ${p.hex})`);
}
if (changed.length > 8) console.log(` …and ${changed.length - 8} more`);
if (!rl.productsFixed) { console.log('nothing to fix — exiting without writing'); process.exit(0); }
const bak = `${OUT}.${new Date().toISOString().replace(/[:.]/g, '-')}.bak`;
fs.copyFileSync(OUT, bak);
snap.relabelled_at = new Date().toISOString();
fs.writeFileSync(OUT, JSON.stringify(snap));
console.log(`backup -> ${path.basename(bak)}`);
console.log(`products.json rewritten (${snap.products.length} products)`);