#!/bin/bash # Customer Portal Development Environment Startup Script # Usage: ./scripts/dev.sh [command] # Commands: start, stop, restart, logs, status set -e # Colors for output RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' BLUE='\033[0;34m' CYAN='\033[0;36m' NC='\033[0m' # No Color # Function to print colored output log() { echo -e "${CYAN}[$(date +'%H:%M:%S')] $1${NC}" } success() { echo -e "${GREEN}✅ $1${NC}" } error() { echo -e "${RED}❌ $1${NC}" } warning() { echo -e "${YELLOW}⚠️ $1${NC}" } # Check if Docker is running check_docker() { if ! docker info >/dev/null 2>&1; then error "Docker is not running. Please start Docker and try again." exit 1 fi success "Docker is running" } # Check if a Docker container is running check_container_running() { local container_name=$1 if docker ps --filter "name=${container_name}" --filter "status=running" --format "{{.Names}}" | grep -q "^${container_name}$"; then return 0 else return 1 fi } # Check if a port is in use check_port_in_use() { local port=$1 if lsof -i:${port} >/dev/null 2>&1; then return 0 else return 1 fi } # Wait for a service to be ready wait_for_service() { local service_name=$1 local check_command=$2 local max_attempts=${3:-30} log "Waiting for ${service_name} to be ready..." for ((i=1; i<=max_attempts; i++)); do if eval "$check_command" >/dev/null 2>&1; then success "${service_name} is ready!" return 0 fi echo -ne "⏳ Waiting for ${service_name}... (${i}/${max_attempts})\r" sleep 1 done echo "" error "${service_name} failed to start after ${max_attempts} attempts" return 1 } # Start Prisma Studio start_prisma_studio() { log "Starting Prisma Studio..." # Check if Prisma Studio is already running if check_port_in_use 5555; then success "Prisma Studio is already running on port 5555" return fi # Start Prisma Studio in background log "Launching Prisma Studio on http://localhost:5555" pnpm --filter @customer-portal/bff run db:studio >/dev/null 2>&1 & # Wait a moment for it to start sleep 3 success "Prisma Studio started on http://localhost:5555" } # Start all services start_all() { echo -e "${BLUE}🚀 Starting Customer Portal Development Environment${NC}" echo "==============================================" check_docker # Check if services are already running local pg_running=false local redis_running=false if check_container_running "portal-postgres"; then pg_running=true fi if check_container_running "portal-redis"; then redis_running=true fi if [ "$pg_running" = true ] && [ "$redis_running" = true ]; then success "PostgreSQL and Redis are already running" else log "Starting PostgreSQL and Redis containers..." docker-compose -f tools/deployment/docker-compose.yml up -d success "Docker services started" fi # Wait for PostgreSQL wait_for_service "PostgreSQL" "docker exec portal-postgres pg_isready -U app -d portal" # Wait for Redis wait_for_service "Redis" "docker exec portal-redis redis-cli ping" # Start Prisma Studio start_prisma_studio log "Starting frontend and backend development servers..." success "All services are ready! Starting development servers..." echo "" echo -e "${GREEN}🎉 All services are running:${NC}" echo -e " 🖥️ Frontend: ${CYAN}http://localhost:3000${NC}" echo -e " 🔌 Backend: ${CYAN}http://localhost:4000${NC}" echo -e " 🗄️ Database: ${CYAN}http://localhost:5555${NC}" echo "" echo -e "${YELLOW}Press Ctrl+C to stop all services${NC}" # Start development servers pnpm --parallel --recursive run dev } # Stop all services stop_all() { log "Stopping all services..." docker-compose -f tools/deployment/docker-compose.yml down success "All services stopped" } # Restart all services restart_all() { log "Restarting all services..." stop_all sleep 2 start_all } # Show logs show_logs() { log "Showing Docker service logs..." docker-compose -f tools/deployment/docker-compose.yml logs -f } # Show status show_status() { log "Service status:" docker-compose -f tools/deployment/docker-compose.yml ps } # Main command handling case "${1:-start}" in start) start_all ;; stop) stop_all ;; restart) restart_all ;; logs) show_logs ;; status) show_status ;; *) echo "Usage: $0 {start|stop|restart|logs|status}" echo "" echo "Commands:" echo " start - Start all services (default)" echo " stop - Stop all services" echo " restart - Restart all services" echo " logs - Show Docker service logs" echo " status - Show service status" exit 1 ;; esac