← back to Newmor Onboard
scripts/fill-images.mjs
30 lines
import fs from 'node:fs';
import assert from 'node:assert/strict';
import crypto from 'node:crypto';
import {ROOT,request,read,save,product} from './shopify.mjs';
const rows=read('data/live-2026-09-09/image-fill-plan.json');
const plan=read('data/newmor-showroom-plan-2026-09-08.json');
const range=read('data/live-2026-09-09/vendor-range.json');
assert.equal(rows.length,6);assert.equal(new Set(rows.map(r=>r.id)).size,6);
const apply=process.argv.includes('--approved');
const path='data/live-2026-09-09/image-fill-journal.json';
const records=fs.existsSync(`${ROOT}/${path}`)?read(path):[];
for(const r of rows){
assert(plan.candidates.some(c=>c.id===r.id&&c.manufacturer_code===r.code&&c.pattern==='Arbour'));
assert(range.candidates.some(c=>c.id===r.id&&c.found));assert.equal(new URL(r.url).hostname,'newmor.com');
const p=await product(r.id);assert.equal(p.status,'DRAFT');assert.equal(p.vendor,'Newmor Wallcoverings');
const old=records.find(x=>x.id===r.id);
if(p.images.nodes.length){assert(old&&p.images.nodes.some(i=>i.id===old.image_gid),'Unexpected existing image; no overwrite');console.log(`Already filled ${r.code}`);continue;}
const response=await fetch(r.url,{signal:AbortSignal.timeout(30000)});assert(response.ok);assert(response.headers.get('content-type').startsWith('image/'));
const bytes=Buffer.from(await response.arrayBuffer());assert(bytes.length>1000);
const sha256=crypto.createHash('sha256').update(bytes).digest('hex');
fs.mkdirSync(`${ROOT}/data/live-2026-09-09/swatch-sources`,{recursive:true});fs.writeFileSync(`${ROOT}/data/live-2026-09-09/swatch-sources/${r.code}.jpg`,bytes);
if(!apply){console.log(`DRY RUN ${r.code} exact vendor swatch,${bytes.length}bytes`);continue;}
const record={...r,source_sha256:sha256,before:p,started_at:new Date().toISOString()};records.push(record);save(path,records);
const result=await request(`products/${r.id.split('/').at(-1)}/images.json`,'POST',{image:{src:r.url,alt:r.alt}});
assert(result.image?.id);record.image_id=result.image.id;record.image_gid=result.image.admin_graphql_api_id;save(path,records);
const after=await product(r.id);assert.equal(after.status,'DRAFT');assert.equal(after.images.nodes.length,1);assert.equal(after.images.nodes[0].altText,r.alt);
record.image_gid=after.images.nodes[0].id;record.after=after;record.verified_at=new Date().toISOString();save(path,records);
console.log(`Verified image ${r.code}`);
}