← back to Dw Theme Toggle

push.sh

63 lines

#!/usr/bin/env bash
# Push the theme-toggle snippet to a Shopify theme on designer-laboratory-sandbox.
# Usage:
#   ./push.sh                # auto-detects MAIN theme on the sandbox
#   ./push.sh <theme_id>     # target a specific theme (preview, dev, etc.)
#
# Requires: ~/Projects/secrets-manager/.env populated with SHOPIFY_ADMIN_TOKEN
# Reads:    ./snippets/theme-toggle.liquid
# Writes:   snippets/theme-toggle.liquid asset on the target theme
#
# The snippet is self-contained (anti-flash + button + CSS + JS in one file).
# After push, edit theme.liquid and add ONE line just inside <head>:
#     {% render 'theme-toggle' %}

set -euo pipefail
cd "$(dirname "$0")"

# Load token from canonical secrets-manager .env
set -a; source ~/Projects/secrets-manager/.env 2>/dev/null || true; set +a
TOKEN="${SHOPIFY_ADMIN_TOKEN:-${SHOPIFY_ACCESS_TOKEN:-}}"
DOMAIN="designer-laboratory-sandbox.myshopify.com"

if [ -z "$TOKEN" ]; then
  echo "ERROR: SHOPIFY_ADMIN_TOKEN not in ~/Projects/secrets-manager/.env" >&2
  exit 1
fi

API="https://$DOMAIN/admin/api/2024-10"

# Decide target theme
if [ "${1:-}" != "" ]; then
  THEME_ID="$1"
else
  echo "Looking up MAIN theme on $DOMAIN..."
  THEME_ID=$(curl -sf -H "X-Shopify-Access-Token: $TOKEN" "$API/themes.json" \
    | python3 -c "import sys,json; print(next(t['id'] for t in json.load(sys.stdin)['themes'] if t['role']=='main'))")
fi
echo "Target theme ID: $THEME_ID"

# Read snippet body and PUT it as snippets/theme-toggle.liquid
BODY=$(python3 -c "
import json
content = open('snippets/theme-toggle.liquid').read()
print(json.dumps({'asset': {'key': 'snippets/theme-toggle.liquid', 'value': content}}))
")

echo "Uploading snippets/theme-toggle.liquid ..."
RESP=$(curl -sf -X PUT \
  -H "X-Shopify-Access-Token: $TOKEN" \
  -H "Content-Type: application/json" \
  -d "$BODY" \
  "$API/themes/$THEME_ID/assets.json")

KEY=$(echo "$RESP" | python3 -c "import sys,json; print(json.load(sys.stdin)['asset']['key'])")
echo "Uploaded asset: $KEY"
echo
echo "Final step (one-time, in Shopify Admin → Online Store → Themes → ⋯ → Edit code):"
echo "  Open layout/theme.liquid and add this line just inside <head>:"
echo "      {% render 'theme-toggle' %}"
echo
echo "Preview URL:"
echo "  https://$DOMAIN?preview_theme_id=$THEME_ID"