#!/bin/bash # 🔧 Development Environment Manager # Manages development services with clean, organized structure set -e # Configuration SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" PROJECT_ROOT="$(cd "$SCRIPT_DIR/../.." && pwd)" COMPOSE_FILE="$PROJECT_ROOT/docker/dev/docker-compose.yml" ENV_FILE="$PROJECT_ROOT/.env" PROJECT_NAME="portal-dev" # Colors GREEN='\033[0;32m' YELLOW='\033[1;33m' RED='\033[0;31m' NC='\033[0m' log() { echo -e "${GREEN}[DEV] $1${NC}"; } warn() { echo -e "${YELLOW}[DEV] $1${NC}"; } error() { echo -e "${RED}[DEV] ERROR: $1${NC}"; exit 1; } # Change to project root cd "$PROJECT_ROOT" # Start development services start_services() { log "🚀 Starting development services..." if [ ! -f "$ENV_FILE" ]; then warn "Environment file not found at $ENV_FILE" log "Creating from template..." cp .env.example .env warn "Please edit .env with your actual values" fi # Start PostgreSQL and Redis docker-compose -f "$COMPOSE_FILE" -p "$PROJECT_NAME" up -d postgres redis # Wait for database log "âŗ Waiting for database to be ready..." timeout=30 while [ $timeout -gt 0 ]; do if docker-compose -f "$COMPOSE_FILE" -p "$PROJECT_NAME" exec -T postgres pg_isready -U dev -d portal_dev 2>/dev/null; then log "✅ Database is ready!" break fi sleep 2 timeout=$((timeout - 2)) done if [ $timeout -eq 0 ]; then error "Database failed to start within 30 seconds" fi log "✅ Development services are running!" log "🔗 Database: postgresql://dev:dev@localhost:5432/portal_dev" log "🔗 Redis: redis://localhost:6379" } # Start with admin tools start_with_tools() { log "đŸ› ī¸ Starting development services with admin tools..." docker-compose -f "$COMPOSE_FILE" -p "$PROJECT_NAME" --profile tools up -d log "🔗 Database Admin: http://localhost:8080" log "🔗 Redis Commander: http://localhost:8081" } # Stop services stop_services() { log "âšī¸ Stopping development services..." docker-compose -f "$COMPOSE_FILE" -p "$PROJECT_NAME" down log "✅ Services stopped" } # Show status show_status() { log "📊 Development Services Status:" docker-compose -f "$COMPOSE_FILE" -p "$PROJECT_NAME" ps } # Show logs show_logs() { docker-compose -f "$COMPOSE_FILE" -p "$PROJECT_NAME" logs -f "${@:2}" } # Start apps (services + local development) start_apps() { log "🚀 Starting development services and applications..." # Start services if not running if ! docker-compose -f "$COMPOSE_FILE" -p "$PROJECT_NAME" ps | grep -q "Up"; then start_services fi log "Starting development applications..." # Export root env so both apps can read from central .env set -a source "$ENV_FILE" 2>/dev/null || true set +a # Show startup information log "đŸŽ¯ Starting development applications..." log "🔗 BFF API: http://localhost:${BFF_PORT:-4000}/api" log "🔗 Frontend: http://localhost:${NEXT_PORT:-3000}" log "🔗 Database: postgresql://dev:dev@localhost:5432/portal_dev" log "🔗 Redis: redis://localhost:6379" log "📚 API Docs: http://localhost:${BFF_PORT:-4000}/api/docs" log "Starting apps with hot-reload..." # Start Prisma Studio (opens browser) (cd "$PROJECT_ROOT/apps/bff" && pnpm db:studio &) # Start apps (portal + bff) with hot reload in parallel pnpm --parallel --filter @customer-portal/portal --filter @customer-portal/bff run dev } # Reset environment reset_env() { log "🔄 Resetting development environment..." stop_services docker-compose -f "$COMPOSE_FILE" -p "$PROJECT_NAME" down -v docker system prune -f log "✅ Development environment reset" } # Run database migrations migrate_db() { log "đŸ—„ī¸ Running database migrations..." if ! docker-compose -f "$COMPOSE_FILE" -p "$PROJECT_NAME" ps postgres | grep -q "Up"; then error "Database service not running. Run 'pnpm dev:start' first" fi pnpm db:migrate log "✅ Database migrations completed" } # Main function case "${1:-help}" in "start") start_services ;; "stop") stop_services ;; "restart") stop_services && start_services ;; "status") show_status ;; "logs") show_logs "$@" ;; "tools") start_with_tools ;; "apps") start_apps ;; "migrate") migrate_db ;; "reset") reset_env ;; "help"|*) echo "🔧 Development Environment Manager" echo "" echo "Usage: $0 {command}" echo "" echo "Commands:" echo " start - Start development services (PostgreSQL + Redis)" echo " stop - Stop all development services" echo " restart - Restart all services" echo " status - Show service status" echo " logs - Show service logs" echo " tools - Start services with admin tools" echo " apps - Start services + run development apps" echo " migrate - Run database migrations" echo " reset - Reset development environment" echo " help - Show this help" exit 0 ;; esac