Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save bgrgicak/6da9be5ab59d6ff3e7d784f994ebaa9f to your computer and use it in GitHub Desktop.
Save bgrgicak/6da9be5ab59d6ff3e7d784f994ebaa9f to your computer and use it in GitHub Desktop.
#!/bin/bash
SKIP_BUILD=false
BUILD_PACKAGE=""
# Parse command line arguments
while [[ $# -gt 0 ]]; do
case "$1" in
--port=*)
PORT="${1#*=}"
shift
;;
--skip-build)
SKIP_BUILD=true
shift
;;
esac
done
if [ -z "$PORT" ]; then
PORT=4000
fi
# Run build if not skipped
if [ "$SKIP_BUILD" = false ]; then
# Build all packages
npm run build
fi
# get version from lerna.json
VERSION=$(jq -r '.version' lerna.json)
HOST=http://127.0.0.1:$PORT
PACKAGE_REPOSITORY_PATH=./dist/packages
# generate a random version number (8 digits)
RANDOM_VERSION=$(openssl rand -hex 4)
LOCAL_PACKAGE_REPOSITORY_PATH=./dist/local-package-repository
LOCAL_PACKAGE_REPOSITORY_HOST="$HOST/wp-content/$RANDOM_VERSION"
# if the local package repository path does not exist, create it
if [ ! -d "$LOCAL_PACKAGE_REPOSITORY_PATH" ]; then
mkdir -p $LOCAL_PACKAGE_REPOSITORY_PATH
fi
for package in $PACKAGE_REPOSITORY_PATH/**/*; do
PACKAGE_JSON_FILE="$package/package.json"
PARENT_DIR=$(basename $(dirname $package))
if [ "$PARENT_DIR" == "php-wasm" ]; then
PACKAGE_NAME="@php-wasm/$(basename $package)"
else
PACKAGE_NAME="@wp-playground/$(basename $package)"
fi
# Skip if a specific package was specified and this isn't it
if [ ! -z "$BUILD_PACKAGE" ] && [ "$BUILD_PACKAGE" != "$(basename $package)" ] && [ "$BUILD_PACKAGE" != "$PACKAGE_NAME" ]; then
continue
fi
if [ ! -f "$PACKAGE_JSON_FILE" ]; then
continue
fi
# Create a temporary file with the modified JSON
jq '
.dependencies = (.dependencies // {}) |
.devDependencies = (.devDependencies // {}) |
.dependencies |= with_entries(
if .value == "'$VERSION'" then
.value = "'$LOCAL_PACKAGE_REPOSITORY_HOST'/" + .key + ".tar.gz"
else
.
end
) |
.devDependencies |= with_entries(
if .value == "'$VERSION'" then
.value = "'$LOCAL_PACKAGE_REPOSITORY_HOST'/" + .key + ".tar.gz"
else
.
end
)
' "$PACKAGE_JSON_FILE" > "$PACKAGE_JSON_FILE.tmp"
# Check if the file has changed
if ! cmp -s "$PACKAGE_JSON_FILE" "$PACKAGE_JSON_FILE.tmp"; then
# Files are different, apply the changes
mv "$PACKAGE_JSON_FILE.tmp" "$PACKAGE_JSON_FILE"
TAR_FILE="$LOCAL_PACKAGE_REPOSITORY_PATH/$PACKAGE_NAME.tar.gz"
# Ensure the parent directory exists
mkdir -p "$(dirname "$TAR_FILE")"
# Compress the package and store it in the local package repository
tar -czf "$TAR_FILE" -C "$package" .
else
# Files are identical, no need to update or tar
rm "$PACKAGE_JSON_FILE.tmp"
fi
done
echo "Starting local package repository server on $LOCAL_PACKAGE_REPOSITORY_HOST"
# copy random version number to the clipboard without a newline
printf "%s" "$RANDOM_VERSION" | pbcopy
echo "Random version number copied to clipboard: $RANDOM_VERSION"
# Run the local package repository server
bun ./packages/playground/cli/src/cli.ts server \
--port $PORT \
--mount=$LOCAL_PACKAGE_REPOSITORY_PATH:/wordpress/wp-content/$RANDOM_VERSION \
--quiet
@bgrgicak
Copy link
Author

Run this script from the root of the WordPress Playground repository

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment