← back to Dw Sarah Rowland Searchfix

publish.mjs

17 lines

// Publish the Meet-the-Koroseal-Designers page to the LIVE store.
// Reads the already-built koroseal-designers-page.html (Steve-approved) and POSTs it.
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();
// pages create needs write_content — use the content token, not the products token
const T=tok('SHOPIFY_CONTENT_TOKEN')||tok('SHOPIFY_ADMIN_TOKEN');
const S='designer-laboratory-sandbox.myshopify.com',A='2024-10';
const body=fs.readFileSync(new URL('./koroseal-designers-page.html',import.meta.url),'utf8');
const res=await fetch(`https://${S}/admin/api/${A}/pages.json`,{method:'POST',
  headers:{'X-Shopify-Access-Token':T,'Content-Type':'application/json'},
  body:JSON.stringify({page:{title:'Meet the Koroseal Designers', handle:'koroseal-designers', body_html:body, published:true}})});
const j=await res.json();
if(res.status>=300){ console.log('CREATE FAILED', res.status, JSON.stringify(j).slice(0,400)); process.exit(1); }
console.log(`page id=${j.page.id} handle=${j.page.handle} published=${!!j.page.published_at}`);
console.log(`URL: https://www.designerwallcoverings.com/pages/${j.page.handle}`);