#!/bin/sh # Publish custom pieces to Verdaccio and register metadata in database # This script runs on container startup set -e VERDACCIO_URL="${VERDACCIO_URL:-http://verdaccio:4873}" PIECES_DIR="/usr/src/app/dist/packages/pieces/community" CUSTOM_PIECES="smoothschedule python-code ruby-code interfaces" # Wait for Verdaccio to be ready wait_for_verdaccio() { echo "Waiting for Verdaccio to be ready..." max_attempts=30 attempt=0 while [ $attempt -lt $max_attempts ]; do if curl -sf "$VERDACCIO_URL/-/ping" > /dev/null 2>&1; then echo "Verdaccio is ready!" return 0 fi attempt=$((attempt + 1)) echo "Attempt $attempt/$max_attempts - Verdaccio not ready yet..." sleep 2 done echo "Warning: Verdaccio not available after $max_attempts attempts" return 1 } # Configure npm/bun to use Verdaccio with authentication configure_registry() { echo "Configuring npm registry to use Verdaccio..." # Register user with Verdaccio first echo "Registering npm user with Verdaccio..." RESPONSE=$(curl -sf -X PUT "$VERDACCIO_URL/-/user/org.couchdb.user:publisher" \ -H "Content-Type: application/json" \ -d '{"name":"publisher","password":"publisher","email":"publisher@smoothschedule.com"}' 2>&1) || true echo "Registration response: $RESPONSE" # Extract token from response if available TOKEN=$(echo "$RESPONSE" | node -pe "JSON.parse(require('fs').readFileSync('/dev/stdin').toString()).token" 2>/dev/null || echo "") if [ -n "$TOKEN" ] && [ "$TOKEN" != "undefined" ]; then echo "Using token from registration" cat > ~/.npmrc << EOF registry=$VERDACCIO_URL //verdaccio:4873/:_authToken=$TOKEN EOF else echo "Using basic auth" # Use legacy _auth format (base64 of username:password) AUTH=$(echo -n "publisher:publisher" | base64) cat > ~/.npmrc << EOF registry=$VERDACCIO_URL //verdaccio:4873/:_auth=$AUTH always-auth=true EOF fi # Create bunfig.toml for bun mkdir -p ~/.bun cat > ~/.bun/bunfig.toml << EOF [install] registry = "$VERDACCIO_URL" EOF echo "Registry configured: $VERDACCIO_URL" } # Publish a piece to Verdaccio publish_piece() { piece_name=$1 piece_dir="$PIECES_DIR/$piece_name" if [ ! -d "$piece_dir" ]; then echo "Warning: Piece directory not found: $piece_dir" return 1 fi cd "$piece_dir" # Get package name and version pkg_name=$(node -p "require('./package.json').name") pkg_version=$(node -p "require('./package.json').version") echo "Publishing $pkg_name@$pkg_version to Verdaccio..." # Check if already published if npm view "$pkg_name@$pkg_version" --registry "$VERDACCIO_URL" > /dev/null 2>&1; then echo " $pkg_name@$pkg_version already published, skipping..." return 0 fi # Publish to Verdaccio (--force to allow republishing) if npm publish --registry "$VERDACCIO_URL" 2>&1; then echo " Successfully published $pkg_name@$pkg_version" else echo " Warning: Could not publish $pkg_name (may already exist)" fi cd /usr/src/app } # Insert piece metadata into database insert_metadata() { if [ -z "$AP_POSTGRES_HOST" ] || [ -z "$AP_POSTGRES_DATABASE" ]; then echo "Warning: Database configuration not available, skipping metadata insertion" return 1 fi echo "Inserting custom piece metadata into database..." echo " Host: $AP_POSTGRES_HOST" echo " Database: $AP_POSTGRES_DATABASE" echo " User: $AP_POSTGRES_USERNAME" # Wait for PostgreSQL to be ready max_attempts=30 attempt=0 while [ $attempt -lt $max_attempts ]; do if PGPASSWORD="$AP_POSTGRES_PASSWORD" psql -h "$AP_POSTGRES_HOST" -p "${AP_POSTGRES_PORT:-5432}" -U "$AP_POSTGRES_USERNAME" -d "$AP_POSTGRES_DATABASE" -c "SELECT 1" > /dev/null 2>&1; then break fi attempt=$((attempt + 1)) echo "Waiting for PostgreSQL... ($attempt/$max_attempts)" sleep 2 done if [ $attempt -eq $max_attempts ]; then echo "Warning: PostgreSQL not available, skipping metadata insertion" return 1 fi # Run the SQL file PGPASSWORD="$AP_POSTGRES_PASSWORD" psql -h "$AP_POSTGRES_HOST" -p "${AP_POSTGRES_PORT:-5432}" -U "$AP_POSTGRES_USERNAME" -d "$AP_POSTGRES_DATABASE" -f /usr/src/app/custom-pieces-metadata.sql echo "Piece metadata inserted successfully!" } # Main execution main() { echo "============================================" echo "Custom Pieces Registration" echo "============================================" # Check if Verdaccio is configured and available if [ -n "$VERDACCIO_URL" ] && [ "$VERDACCIO_URL" != "none" ]; then if wait_for_verdaccio; then configure_registry # Publish each custom piece for piece in $CUSTOM_PIECES; do publish_piece "$piece" || true done else echo "Skipping Verdaccio publishing - pieces are pre-built in image" fi else echo "Verdaccio not configured - using pre-built pieces from image" fi # Insert metadata into database insert_metadata || true echo "============================================" echo "Custom Pieces Registration Complete" echo "============================================" } main "$@"