← back to Majilite Onboard

scripts/publish_all_channels.js

32 lines

const fs=require('fs'),path=require('path');
const STORE='designer-laboratory-sandbox.myshopify.com';
const TOKEN=(fs.readFileSync(path.join(process.env.HOME,'Projects/secrets-manager/.env'),'utf8').match(/^SHOPIFY_ADMIN_TOKEN=(.+)$/m)||[])[1].replace(/["']/g,'').trim();
const GQL=`https://${STORE}/admin/api/2024-10/graphql.json`;
const sleep=ms=>new Promise(r=>setTimeout(r,ms));
async function gql(q,v){for(let a=0;a<6;a++){const r=await fetch(GQL,{method:'POST',headers:{'X-Shopify-Access-Token':TOKEN,'Content-Type':'application/json'},body:JSON.stringify({query:q,variables:v})});const j=await r.json();if(j.errors){if(JSON.stringify(j.errors).includes('THROTTLED')){await sleep(1500*(a+1));continue;}throw new Error(JSON.stringify(j.errors).slice(0,200));}return j.data;}throw new Error('throttled');}
(async()=>{
  const pd=await gql(`{ publications(first:40){ nodes{ id name } } }`);
  const pubs=pd.publications.nodes;
  console.log('ALL channels ('+pubs.length+'):', pubs.map(p=>p.name).join(', '));
  const pubInput=pubs.map(p=>({publicationId:p.id}));
  // collect Majilite products + the collection
  let items=[]; let c=null;
  do{const d=await gql(`query($c:String){ products(first:100, after:$c, query:"vendor:Majilite"){ pageInfo{hasNextPage endCursor} nodes{ id handle } } }`,{c});
     items.push(...d.products.nodes); c=d.products.pageInfo.hasNextPage?d.products.pageInfo.endCursor:null;}while(c);
  items.push({id:'gid://shopify/Collection/303540666419', handle:'(collection)'});
  console.log(`publishing ${items.length} items to ${pubs.length} channels each…`);
  const chanFail={}; let ok=0,err=0;
  for(const it of items){
    try{
      const d=await gql(`mutation($id:ID!,$pubs:[PublicationInput!]!){ publishablePublish(id:$id, input:$pubs){ userErrors{field message} } }`,{id:it.id,pubs:pubInput});
      const ue=d.publishablePublish.userErrors;
      if(ue&&ue.length){ err++; ue.forEach(e=>{const k=(e.message||'').slice(0,60); chanFail[k]=(chanFail[k]||0)+1;}); if(err<=6)console.log('  ⚠',it.handle,JSON.stringify(ue).slice(0,120)); }
      else ok++;
    }catch(e){err++; if(err<=6)console.log('  ✗',it.handle,e.message.slice(0,80));}
    if(ok%40===0&&ok)process.stdout.write(`  …${ok} published to all channels\n`);
    await sleep(180);
  }
  console.log(`\nDONE. items_ok=${ok} items_with_errors=${err}`);
  if(Object.keys(chanFail).length) console.log('channel-level rejections (by msg):', JSON.stringify(chanFail,null,1));
})();