← back to Novasuede Min Fix

enumerate.mjs

26 lines

import fs from 'fs';
const env = fs.readFileSync(process.env.HOME+'/Projects/secrets-manager/.env','utf8');
const get = k => (env.match(new RegExp('^'+k+'=(.*)$','m'))||[])[1];
const TOKEN = get('SHOPIFY_ADMIN_TOKEN');
const DOMAIN = 'designer-laboratory-sandbox.myshopify.com';
const API = `https://${DOMAIN}/admin/api/2024-10/graphql.json`;
async function gql(query, variables={}){
  const r = await fetch(API,{method:'POST',headers:{'X-Shopify-Access-Token':TOKEN,'Content-Type':'application/json'},body:JSON.stringify({query,variables})});
  const j = await r.json();
  if(j.errors){console.error(JSON.stringify(j.errors));}
  return j.data;
}
// Query products by vendor Novasuede, active status
let cursor=null, all=[];
do {
  const d = await gql(`query($c:String){products(first:50,after:$c,query:"vendor:Novasuede status:active"){pageInfo{hasNextPage endCursor} nodes{id title status handle vendor
    metafields(first:20){nodes{namespace key value type}}
    variants(first:20){nodes{id title sku inventoryPolicy inventoryItem{tracked}
      metafields(first:10){nodes{namespace key value type}}}}
  }}}`,{c:cursor});
  const p=d.products; all.push(...p.nodes);
  cursor = p.pageInfo.hasNextPage ? p.pageInfo.endCursor : null;
} while(cursor);
fs.writeFileSync('live-novasuede.json', JSON.stringify(all,null,2));
console.log('TOTAL active Novasuede products:', all.length);