← back to Consulting Designerwallcoverings Com

docs/aeo-drafts/theme-snippets/APPLY-page-jsonld.sh

72 lines

#!/usr/bin/env bash
# TK-20 GATED — install the handle-keyed page JSON-LD snippet on the LIVE theme.
# Emits WebPage + FAQPage JSON-LD on /pages/wall-coverings + /pages/designer-wallpaper.
# DO NOT RUN until Steve GO. Requires SHOPIFY_THEME_TOKEN (theme asset write scope).
#
# Preconditions:
#   - LIVE theme id 144396058675 (main). Re-confirm before running.
#   - This script lives in docs/aeo-drafts/theme-snippets/ next to the two source files.
set -euo pipefail
TOKEN="${SHOPIFY_THEME_TOKEN:?export SHOPIFY_THEME_TOKEN first}"
STORE="designer-laboratory-sandbox.myshopify.com"
THEME="144396058675"
API="https://$STORE/admin/api/2024-10/themes/$THEME/assets.json"
DIR="$(cd "$(dirname "$0")" && pwd)"

echo "== [0/3] backup current sections/page.liquid =="
curl -s "$API?asset%5Bkey%5D=sections/page.liquid" -H "X-Shopify-Access-Token: $TOKEN" \
  | node -e 'let d="";process.stdin.on("data",c=>d+=c).on("end",()=>{require("fs").writeFileSync(process.argv[1],JSON.parse(d).asset.value)})' \
    "$DIR/sections-page.liquid.LIVE-BACKUP-$(date +%Y%m%dT%H%M%S)"
echo "   backup saved to $DIR/"

echo "== [1/3] PUT snippets/dw-aeo-jsonld.liquid =="
node -e '
  const fs=require("fs");
  const val=fs.readFileSync(process.argv[1],"utf8");
  process.stdout.write(JSON.stringify({asset:{key:"snippets/dw-aeo-jsonld.liquid",value:val}}));
' "$DIR/dw-aeo-jsonld.liquid" > /tmp/aeo-snippet-put.json
curl -s -X PUT "$API" -H "X-Shopify-Access-Token: $TOKEN" -H "Content-Type: application/json" \
  --data-binary @/tmp/aeo-snippet-put.json | tee /tmp/aeo-snippet-put-resp.json | head -c 300; echo

echo "== [2/3] append render hook to the LIVE sections/page.liquid (drift-safe) =="
# vp-dw-commerce REVISE (2026-07-26): do NOT PUT the stale .PROPOSED snapshot — it could
# clobber live drift. Instead FETCH the live file, verify it still matches the .PROPOSED
# body (abort on drift so a human can re-inspect), then APPEND only the single render line
# and PUT that. Idempotent: no-op if the render line is already present.
LIVE_JSON="$(curl -s "$API?asset%5Bkey%5D=sections/page.liquid" -H "X-Shopify-Access-Token: $TOKEN")"
echo "$LIVE_JSON" | node -e '
  const fs=require("fs");
  let d=""; process.stdin.on("data",c=>d+=c).on("end",()=>{
    const live = JSON.parse(d).asset.value;
    const HOOK = "{% render \x27dw-aeo-jsonld\x27 %}";
    if (live.includes(HOOK)) { console.error("   render hook already present — nothing to do."); process.exit(3); }
    // expected live == .PROPOSED minus the leading TK-20 comment block and minus the hook line
    let proposed = fs.readFileSync(process.argv[1],"utf8")
      .replace(/^{%- comment -%}[\s\S]*?{%- endcomment -%}\n/, "");
    const expectedBody = proposed.replace(/\n*{% render \x27dw-aeo-jsonld\x27 %}\n*$/, "").trim();
    if (live.trim() !== expectedBody) {
      console.error("   !! DRIFT: live sections/page.liquid differs from the captured .PROPOSED body.");
      console.error("   !! ABORTING step [2]. Diff the LIVE-BACKUP against .PROPOSED, then append the");
      console.error("   !! single line  {% render \x27dw-aeo-jsonld\x27 %}  to the live file by hand and PUT.");
      process.exit(4);
    }
    const shipped = live.replace(/\s*$/, "") + "\n\n" + HOOK + "\n";
    process.stdout.write(JSON.stringify({asset:{key:"sections/page.liquid",value:shipped}}));
  });
' "$DIR/sections-page.liquid.PROPOSED" > /tmp/aeo-page-put.json
_step2=$?
if [ "$_step2" = "3" ]; then
  echo "   [2/3] skipped (hook already installed)."
elif [ "$_step2" != "0" ]; then
  echo "   [2/3] ABORTED (drift or fetch error, exit $_step2) — snippet from [1] is live; page.liquid untouched."
  echo "   Resolve drift, then hand-PUT. Everything else is safe to leave as-is."
  exit "$_step2"
else
  curl -s -X PUT "$API" -H "X-Shopify-Access-Token: $TOKEN" -H "Content-Type: application/json" \
    --data-binary @/tmp/aeo-page-put.json | tee /tmp/aeo-page-put-resp.json | head -c 300; echo
fi

echo "== [3/3] verify (re-render with Playwright) =="
echo "   node ../../scripts/aeo-render-verify.mjs   # expect hasJsonLdFaq:true on the page"
echo "DONE — rollback: PUT the sections-page.liquid.LIVE-BACKUP-* file back, and DELETE snippets/dw-aeo-jsonld.liquid."