Files
chrnx-dev-wp/scripts/package-theme.sh
T
chrnxandClaude Opus 5 842c8a9b37 build: package the theme as an installable zip
An allowlist so a new dev file is excluded by default, a version check
against style.css, and a refusal to ship tests, scripts or vendor.

The first package was 34MB because every img fallback pointed at the
design master: a browser without AVIF or WebP downloaded 3.4MB per
image. That was a live cost, not just a packaging one. Fallbacks point
at the generated WebP now and masters stay out of the zip, which is 3MB.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-13 19:41:48 -05:00

88 lines
3.5 KiB
Bash
Executable File

#!/bin/sh
set -eu
# Build a distributable theme ZIP containing only what WordPress runs.
# Everything ships from an explicit allowlist, so a new dev file is excluded
# by default instead of leaking into the package.
chrnx_package_script_dir=$(CDPATH='' cd -- "$(dirname -- "$0")" && pwd)
chrnx_package_root=$(dirname -- "$chrnx_package_script_dir")
chrnx_package_slug=chrnx
chrnx_package_out=${1:-"$chrnx_package_root/dist"}
case "$chrnx_package_out" in
/*) ;;
*)
echo "Package output must be an explicit absolute path." >&2
exit 2
;;
esac
if [ "$chrnx_package_out" = "/" ] || [ "$chrnx_package_out" = "$chrnx_package_root" ]; then
echo "Refusing broad package output path: $chrnx_package_out" >&2
exit 2
fi
if [ -L "$chrnx_package_out" ]; then
echo "Refusing symbolic-link package output: $chrnx_package_out" >&2
exit 2
fi
if ! command -v zip >/dev/null 2>&1; then
echo "Required tool is missing: zip" >&2
exit 1
fi
chrnx_package_version=$(sed -n 's/^Version:[[:space:]]*\(.*\)$/\1/p' "$chrnx_package_root/style.css" | head -1)
if [ -z "$chrnx_package_version" ]; then
echo "style.css must declare Version metadata." >&2
exit 1
fi
chrnx_package_php_version=$(sed -n "s/^define( 'CHRNX_VERSION', '\(.*\)' );$/\1/p" "$chrnx_package_root/functions.php" | head -1)
if [ "$chrnx_package_version" != "$chrnx_package_php_version" ]; then
echo "style.css ($chrnx_package_version) and CHRNX_VERSION ($chrnx_package_php_version) disagree." >&2
exit 1
fi
mkdir -p -- "$chrnx_package_out"
chrnx_package_stage=$(mktemp -d "${TMPDIR:-/tmp}/chrnx-package.XXXXXX")
trap 'rm -rf -- "$chrnx_package_stage"' EXIT HUP INT TERM
chrnx_package_dir="$chrnx_package_stage/$chrnx_package_slug"
mkdir -p -- "$chrnx_package_dir"
# Runtime files only. Tests, tooling, docs and sources stay out.
for chrnx_package_entry in style.css functions.php theme.json readme.txt; do
if [ -f "$chrnx_package_root/$chrnx_package_entry" ]; then
cp -- "$chrnx_package_root/$chrnx_package_entry" "$chrnx_package_dir/"
fi
done
for chrnx_package_entry in inc blocks patterns parts templates assets; do
if [ ! -d "$chrnx_package_root/$chrnx_package_entry" ]; then
echo "Required theme directory is missing: $chrnx_package_entry" >&2
exit 1
fi
cp -R -- "$chrnx_package_root/$chrnx_package_entry" "$chrnx_package_dir/"
done
# Editor sources and design masters are not runtime assets. Every image is
# served from assets/images/generated, so the multi-megabyte sources stay out.
rm -rf -- "$chrnx_package_dir/assets/js/editor-"*
find "$chrnx_package_dir/assets/images" -maxdepth 1 -type f \( -name '*.png' -o -name '*.jpeg' -o -name '*.jpg' \) -delete
find "$chrnx_package_dir" -name '.DS_Store' -delete
find "$chrnx_package_dir" -name '*.asset.php' -delete
if [ ! -f "$chrnx_package_dir/style.css" ] || [ ! -f "$chrnx_package_dir/functions.php" ]; then
echo "Package is missing required theme files." >&2
exit 1
fi
chrnx_package_zip="$chrnx_package_out/$chrnx_package_slug-$chrnx_package_version.zip"
rm -f -- "$chrnx_package_zip"
( cd "$chrnx_package_stage" && zip -q -r -X "$chrnx_package_zip" "$chrnx_package_slug" )
for chrnx_package_forbidden in tests scripts node_modules vendor docs .git .worktrees composer.json package.json; do
if unzip -Z1 "$chrnx_package_zip" | grep -q "^$chrnx_package_slug/$chrnx_package_forbidden"; then
echo "Package unexpectedly contains: $chrnx_package_forbidden" >&2
exit 1
fi
done
echo "CHRNX_PACKAGE $chrnx_package_zip $(unzip -Z1 "$chrnx_package_zip" | wc -l | tr -d ' ') files $(du -h "$chrnx_package_zip" | cut -f1)"