lilith-platform/scripts/migrate-to-template-showcase.sh
2026-02-16 02:08:52 -08:00

249 lines
7 KiB
Bash
Executable file
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env bash
#
# Migrate existing showcase to generic template
# Usage: ./scripts/migrate-to-template-showcase.sh <feature-name>
#
# This script:
# 1. Backs up existing showcase to .old directory
# 2. Copies template to showcase location
# 3. Extracts configuration from old showcase
# 4. Generates .env file with migrated settings
# 5. Reports external dependencies and next steps
#
set -e
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Helper functions
error() {
echo -e "${RED}✗ Error: $1${NC}" >&2
exit 1
}
success() {
echo -e "${GREEN}$1${NC}"
}
info() {
echo -e "${BLUE} $1${NC}"
}
warn() {
echo -e "${YELLOW}$1${NC}"
}
# Function: Extract port from file
extract_port() {
local file=$1
local pattern=$2
local default=$3
if [[ -f "$file" ]]; then
local port=$(grep -o "$pattern.*[0-9]\+" "$file" 2>/dev/null | grep -o '[0-9]\+' | head -1)
echo "${port:-$default}"
else
echo "$default"
fi
}
# Function: Extract value from package.json script
extract_script_value() {
local pkg_file=$1
local script_name=$2
local pattern=$3
if [[ -f "$pkg_file" ]]; then
local script_line=$(grep "\"$script_name\":" "$pkg_file" 2>/dev/null)
local value=$(echo "$script_line" | grep -o "$pattern" | head -1)
echo "$value"
fi
}
# Main script
main() {
# Validate input
if [[ -z "$1" ]]; then
echo "Usage: $0 <feature-name>"
echo ""
echo "Example:"
echo " $0 profile"
echo ""
echo "This will migrate features/profile/frontend-showcase/ to the template."
exit 1
fi
FEATURE_NAME="$1"
FEATURE_PATH="codebase/features/$FEATURE_NAME"
SHOWCASE_PATH="$FEATURE_PATH/frontend-showcase"
OLD_SHOWCASE_PATH="$SHOWCASE_PATH.old"
info "Migrating $FEATURE_NAME showcase to template..."
echo ""
# Check if showcase exists
if [[ ! -d "$SHOWCASE_PATH" ]]; then
error "$SHOWCASE_PATH does not exist"
fi
# Check if .old already exists
if [[ -d "$OLD_SHOWCASE_PATH" ]]; then
warn "$OLD_SHOWCASE_PATH already exists"
read -p "Delete existing .old and continue? (y/N) " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
error "Migration cancelled"
fi
rm -rf "$OLD_SHOWCASE_PATH"
fi
# Backup existing showcase
info "Backing up existing showcase..."
mv "$SHOWCASE_PATH" "$OLD_SHOWCASE_PATH"
success "Backup created at $OLD_SHOWCASE_PATH"
echo ""
# Copy template
info "Copying template..."
cp -r codebase/@templates/feature-showcase "$SHOWCASE_PATH"
success "Template copied to $SHOWCASE_PATH"
echo ""
# Extract configuration from old showcase
info "Migrating configuration from .old..."
OLD_PKG="$OLD_SHOWCASE_PATH/package.json"
OLD_VITE="$OLD_SHOWCASE_PATH/vite.config.ts"
OLD_DOCKER="$OLD_SHOWCASE_PATH/docker-compose.yml"
# Extract ports
VITE_PORT=$(extract_port "$OLD_VITE" "port.*:" "5200")
DB_PORT=$(extract_port "$OLD_DOCKER" "- '" "25432")
BACKEND_PORT="3015" # Default, can be customized
# Extract backend path
BACKEND_PATH=""
if [[ -f "$OLD_PKG" ]]; then
local api_script=$(grep "showcase:api" "$OLD_PKG" 2>/dev/null | head -1)
if [[ -n "$api_script" ]]; then
BACKEND_PATH=$(echo "$api_script" | grep -o 'cd [^&;]*' | cut -d' ' -f2 || echo "")
fi
fi
# Extract UI packages root (usually same across showcases)
UI_PACKAGES_ROOT="../../../../../../../@packages/@ts/@ui-react/packages"
# Generate .env file
cat > "$SHOWCASE_PATH/.env" <<EOF
# Feature identification
FEATURE_NAME=$FEATURE_NAME
# Ports
VITE_PORT=$VITE_PORT
BACKEND_PORT=$BACKEND_PORT
DB_PORT=$DB_PORT
REDIS_PORT=6379
# Backend service location
BACKEND_PATH=$BACKEND_PATH
# Database
DB_NAME=lilith
DB_USER=lilith
DB_PASSWORD=lilith
# Feature-specific paths (customize these)
FEATURE_FRONTEND_PATH=
FEATURE_PACKAGE_IMPORT=
FEATURE_PACKAGE_PATH=
# API proxy targets (customize this JSON array)
PROXY_TARGETS='[]'
# UI packages location
UI_PACKAGES_ROOT=$UI_PACKAGES_ROOT
EOF
success "Configuration migrated to .env"
echo ""
# Detect external dependencies
info "Analyzing external dependencies..."
echo ""
if [[ -d "$OLD_SHOWCASE_PATH/src" ]]; then
# Find non-ui @lilith imports (potential external dependencies)
local deps=$(grep -rh "from '@lilith/" "$OLD_SHOWCASE_PATH/src" 2>/dev/null | \
grep -v "ui-" | \
sed "s/.*from '\(@lilith\/[^']*\)'.*/\1/" | \
sort | uniq)
if [[ -n "$deps" ]]; then
warn "Found external @lilith dependencies:"
echo "$deps"
echo ""
echo "You may need to add these to .env:"
echo " FEATURE_PACKAGE_IMPORT=<package-name>"
echo " FEATURE_PACKAGE_PATH=<relative-path-to-package>"
echo ""
fi
fi
# Detect proxy targets
if [[ -f "$OLD_VITE" ]]; then
local proxies=$(grep -A 1 "proxy:" "$OLD_VITE" 2>/dev/null | \
grep "'^/api/" | \
sed "s/.*'\(\/api\/[^']*\)'.*/\1/" | \
tr '\n' ',' | sed 's/,$//')
if [[ -n "$proxies" ]]; then
warn "Found API proxy targets: $proxies"
echo "Update PROXY_TARGETS in .env with these paths"
echo ""
fi
fi
# Final success message
success "Migration complete!"
echo ""
echo "═══════════════════════════════════════════════════════════════"
echo ""
echo "Next steps:"
echo ""
echo "1. cd $SHOWCASE_PATH"
echo ""
echo "2. Review and update .env file:"
echo " - Set FEATURE_FRONTEND_PATH if using @/ alias"
echo " - Set FEATURE_PACKAGE_IMPORT/PATH for external dependencies"
echo " - Set PROXY_TARGETS for API routes to proxy"
echo ""
echo "3. Migrate feature-specific code from .old:"
echo " - src/mock-routes.ts (API mocks)"
echo " - src/store-instance.ts (domain methods)"
echo " - src/routes/* (feature views)"
echo " - src/seed-data.ts (initial data)"
echo " - src/lib/* (utilities like slug-utils.ts)"
echo ""
echo "4. Install dependencies:"
echo " bun install"
echo ""
echo "5. Test the showcase:"
echo " bun run showcase"
echo ""
echo "6. When everything works, delete the backup:"
echo " rm -rf $OLD_SHOWCASE_PATH"
echo ""
echo "═══════════════════════════════════════════════════════════════"
echo ""
echo "Old showcase preserved at: $OLD_SHOWCASE_PATH"
echo "Use it as reference for migrating feature-specific code."
echo ""
}
# Run main script
main "$@"