172 lines
5.2 KiB
Bash
Raw Normal View History

2025-08-21 15:24:40 +09:00
#!/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
2025-08-21 15:24:40 +09:00
# 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
2025-08-21 15:24:40 +09:00
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
2025-08-21 15:24:40 +09:00
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
2025-08-21 15:24:40 +09:00
log "✅ Services stopped"
}
# Show status
show_status() {
log "📊 Development Services Status:"
docker compose -f "$COMPOSE_FILE" -p "$PROJECT_NAME" ps
2025-08-21 15:24:40 +09:00
}
# Show logs
show_logs() {
docker compose -f "$COMPOSE_FILE" -p "$PROJECT_NAME" logs -f "${@:2}"
2025-08-21 15:24:40 +09:00
}
# 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
2025-08-21 15:24:40 +09:00
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
2025-08-22 17:02:49 +09:00
# 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
2025-08-21 15:24:40 +09:00
}
# Reset environment
reset_env() {
log "🔄 Resetting development environment..."
stop_services
docker compose -f "$COMPOSE_FILE" -p "$PROJECT_NAME" down -v
2025-08-21 15:24:40 +09:00
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
2025-08-21 15:24:40 +09:00
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