← back to Dw Sarah Rowland Searchfix

check-wiring.mjs

27 lines

import fs from 'node:fs'; import os from 'node:os';
const env = fs.readFileSync(`${os.homedir()}/Projects/secrets-manager/.env`,'utf8');
const tok = (k)=> (env.match(new RegExp('^'+k+'=(.+)$','m'))||[])[1]?.trim();
const THEME_TOK = tok('SHOPIFY_THEME_TOKEN') || tok('SHOPIFY_CONTENT_TOKEN') || tok('SHOPIFY_ADMIN_TOKEN');
const STORE='designer-laboratory-sandbox.myshopify.com', API='2024-10';
// resolve live (main) theme id
const tr = await fetch(`https://${STORE}/admin/api/${API}/themes.json`,{headers:{'X-Shopify-Access-Token':THEME_TOK}});
const themes=(await tr.json()).themes||[];
const live=themes.find(t=>t.role==='main');
console.log('LIVE theme:', live?`${live.id} "${live.name}"`:'(none / token lacks themes scope)');
if(!live){ console.log(JSON.stringify(themes.slice(0,3))); process.exit(0); }
// pull the asset list, grep filenames, then fetch the likely PDP files and grep for 'designer'
const ar = await fetch(`https://${STORE}/admin/api/${API}/themes/${live.id}/assets.json`,{headers:{'X-Shopify-Access-Token':THEME_TOK}});
const assets=(await ar.json()).assets||[];
const candidates = assets.map(a=>a.key).filter(k=>/product|meta|spec|filter|search/i.test(k) && k.endsWith('.liquid'));
console.log(`\nScanning ${candidates.length} candidate liquid files for "designer"...`);
let hits=[];
for(const key of candidates){
  const r=await fetch(`https://${STORE}/admin/api/${API}/themes/${live.id}/assets.json?asset[key]=${encodeURIComponent(key)}`,{headers:{'X-Shopify-Access-Token':THEME_TOK}});
  const body=(await r.json())?.asset?.value||'';
  const m=body.match(/[^\n]*designer[^\n]*/gi);
  if(m) hits.push({key, lines:[...new Set(m)].slice(0,4)});
  await new Promise(z=>setTimeout(z,80));
}
if(!hits.length) console.log('  → NO theme file references "designer" anywhere.');
else hits.forEach(h=>{ console.log(`\n  ${h.key}:`); h.lines.forEach(l=>console.log('     '+l.trim().slice(0,110))); });