← back to Dw Sarah Rowland Searchfix

update-menu.mjs

19 lines

// Add "Koroseal Designers" to the Resources menu (re-sends existing items intact).
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_CONTENT_TOKEN')||tok('SHOPIFY_ADMIN_TOKEN');
const S='designer-laboratory-sandbox.myshopify.com',A='2024-10';
async function gql(q,v){const r=await fetch(`https://${S}/admin/api/${A}/graphql.json`,{method:'POST',headers:{'X-Shopify-Access-Token':T,'Content-Type':'application/json'},body:JSON.stringify({query:q,variables:v})});return r.json();}
const before=JSON.parse(fs.readFileSync(new URL('./resources-menu.BEFORE.json',import.meta.url)));
const items=before.items.map(i=>({title:i.title, type:i.type, resourceId:i.resourceId, tags:i.tags||[]}));
items.push({title:'Koroseal Designers', type:'PAGE', resourceId:'gid://shopify/Page/220364832819', tags:[]});
const M=`mutation($id:ID!,$title:String!,$handle:String!,$items:[MenuItemUpdateInput!]!){
  menuUpdate(id:$id,title:$title,handle:$handle,items:$items){ menu{ id items{ title url } } userErrors{ field message } } }`;
const d=await gql(M,{id:'gid://shopify/Menu/65661239361', title:'Resources', handle:'resources', items});
if(d.errors){console.log('GQL ERR',JSON.stringify(d.errors).slice(0,400));process.exit(1);}
const ue=d.data.menuUpdate.userErrors;
if(ue.length){console.log('userErrors',JSON.stringify(ue));process.exit(1);}
console.log('✓ Resources menu updated. Items now:');
d.data.menuUpdate.menu.items.forEach(i=>console.log(`   - ${i.title}  →  ${i.url}`));