#!/bin/bash # MCPTesta Documentation Startup Script # Handles initialization and environment setup set -euo pipefail # Colors for output RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' BLUE='\033[0;34m' NC='\033[0m' # No Color # Logging function log() { echo -e "${BLUE}[$(date +'%Y-%m-%d %H:%M:%S')]${NC} $1" } error() { echo -e "${RED}[ERROR]${NC} $1" >&2 } success() { echo -e "${GREEN}[SUCCESS]${NC} $1" } warn() { echo -e "${YELLOW}[WARN]${NC} $1" } # Environment setup NODE_ENV=${NODE_ENV:-development} HOST=${HOST:-0.0.0.0} PORT=${PORT:-4321} log "Starting MCPTesta Documentation Server" log "Environment: $NODE_ENV" log "Host: $HOST" log "Port: $PORT" # Ensure we're in the correct directory cd /app # Check if node_modules exists if [ ! -d "node_modules" ]; then warn "node_modules not found, installing dependencies..." npm ci success "Dependencies installed" fi # Check if package.json exists if [ ! -f "package.json" ]; then error "package.json not found in /app" exit 1 fi # Validate Astro configuration if [ ! -f "astro.config.mjs" ]; then error "astro.config.mjs not found" exit 1 fi # Health check function health_check() { local max_attempts=30 local attempt=1 log "Waiting for server to be ready..." while [ $attempt -le $max_attempts ]; do if curl -f -s "http://localhost:$PORT/" > /dev/null 2>&1; then success "Server is ready!" return 0 fi log "Attempt $attempt/$max_attempts - Server not ready yet..." sleep 2 ((attempt++)) done error "Server failed to start within expected time" return 1 } # Start server based on environment if [ "$NODE_ENV" = "development" ]; then log "Starting development server with hot reloading..." # Start server in background for health check npm run dev:verbose -- --host "$HOST" --port "$PORT" & SERVER_PID=$! # Wait for server to be ready if health_check; then success "Development server started successfully" # Bring server to foreground wait $SERVER_PID else error "Failed to start development server" kill $SERVER_PID 2>/dev/null || true exit 1 fi elif [ "$NODE_ENV" = "production" ]; then log "Building production assets..." # Clean previous builds npm run clean # Build for production npm run build:prod if [ ! -d "dist" ]; then error "Production build failed - dist directory not found" exit 1 fi success "Production build completed" log "Starting production server..." # Start preview server npm run preview -- --host "$HOST" --port "$PORT" else error "Unknown NODE_ENV: $NODE_ENV (expected: development or production)" exit 1 fi