← back to Dw Sarah Rowland Searchfix

fetch-snippet.mjs

15 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 T=tok('SHOPIFY_THEME_TOKEN')||tok('SHOPIFY_CONTENT_TOKEN')||tok('SHOPIFY_ADMIN_TOKEN');
const S='designer-laboratory-sandbox.myshopify.com',A='2024-10',ID='144396058675';
const KEY='snippets/product-description-meta.liquid';
const r=await fetch(`https://${S}/admin/api/${A}/themes/${ID}/assets.json?asset[key]=${encodeURIComponent(KEY)}`,{headers:{'X-Shopify-Access-Token':T}});
const body=(await r.json())?.asset?.value||'';
fs.writeFileSync(new URL('./theme-backups/product-description-meta.BEFORE.liquid',import.meta.url),body);
console.log('Backed up',body.length,'chars.');
// audit ALL uses of the `brand` variable so we don't break another consumer
const uses=body.split('\n').map((l,i)=>({i:i+1,l})).filter(x=>/\bbrand\b/.test(x.l));
console.log('\nAll lines referencing `brand`:');
uses.forEach(x=>console.log(String(x.i).padStart(4), x.l.trim().slice(0,130)));