← back to Dw Sarah Rowland Searchfix
apply-theme.mjs
19 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 before=fs.readFileSync(new URL('./theme-backups/product-description-meta.BEFORE.liquid',import.meta.url),'utf8');
const OLD=`{% assign brand = product.metafields.custom.brand.value | default: product.metafields.global.Brand.value %}`;
const NEW=`{% assign brand = product.metafields.custom.designer.value | default: product.metafields.custom.brand.value | default: product.metafields.global.Brand.value %}`;
const n=before.split(OLD).length-1;
if(n!==1){ console.error(`ABORT: expected exactly 1 match of line 49, found ${n}`); process.exit(1); }
const after=before.replace(OLD,NEW);
fs.writeFileSync(new URL('./theme-backups/product-description-meta.AFTER.liquid',import.meta.url),after);
// PUT (Shopify validates Liquid on save; 200 = valid)
const put=await fetch(`https://${S}/admin/api/${A}/themes/${ID}/assets.json`,{method:'PUT',
headers:{'X-Shopify-Access-Token':T,'Content-Type':'application/json'},
body:JSON.stringify({asset:{key:KEY,value:after}})});
console.log('PUT status:',put.status, put.status===200?'✓ saved (Liquid valid)':await put.text());