dnd-hub/scripts/dev.sh
2026-03-19 01:31:07 -04:00

79 lines
2.1 KiB
Bash

#!/bin/bash
# Kill existing processes on our ports using PowerShell - with retry
kill_port() {
local port=$1
for i in 1 2 3; do
powershell -Command "Get-NetTCPConnection -LocalPort $port -ErrorAction SilentlyContinue | ForEach-Object { Stop-Process -Id \$_.OwningProcess -Force -ErrorAction SilentlyContinue }" 2>/dev/null
sleep 1
# Check if port is free
if ! powershell -Command "Get-NetTCPConnection -LocalPort $port -ErrorAction SilentlyContinue" 2>/dev/null | grep -q LISTENING; then
break
fi
done
}
echo "Cleaning up existing processes..."
kill_port 4000
kill_port 5173
sleep 2
# Get the script directory and resolve to absolute Windows path
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
ROOT_DIR="$(cd "$SCRIPT_DIR/.." && pwd)"
# Start server in background
cd "$ROOT_DIR/apps/server"
nohup npx tsx watch src/main.ts > "$ROOT_DIR/data/server.log" 2>&1 &
SERVER_PID=$!
echo "Server started (PID: $SERVER_PID)"
# Start web in background
cd "$ROOT_DIR/apps/web"
nohup npm run dev > "$ROOT_DIR/data/web.log" 2>&1 &
WEB_PID=$!
echo "Web started (PID: $WEB_PID)"
# Wait for both to be ready
echo "Waiting for services to start..."
sleep 6
# Check health
sleep 2
if curl -s http://127.0.0.1:4000/health 2>/dev/null | grep -q "ok"; then
echo "✓ Server is running on http://localhost:4000"
else
echo "✗ Server failed to start"
echo "Server log:"
cat "$ROOT_DIR/data/server.log" 2>/dev/null | tail -20
exit 1
fi
if curl -s http://127.0.0.1:5173/ 2>/dev/null | grep -qi "campaign"; then
echo "✓ Web is running on http://localhost:5173"
else
# Fallback: check if port is listening
if netstat -ano 2>/dev/null | grep -q ":5173.*LISTENING"; then
echo "✓ Web is running on http://localhost:5173"
else
echo "✗ Web failed to start"
echo "Web log:"
cat "$ROOT_DIR/data/web.log" 2>/dev/null | tail -10
fi
fi
echo ""
echo "Both services running. Press Ctrl+C to stop."
# Handle Ctrl+C
cleanup() {
echo "Stopping services..."
taskkill //F //PID $SERVER_PID 2>/dev/null
taskkill //F //PID $WEB_PID 2>/dev/null
exit 0
}
trap cleanup INT TERM
# Keep script running
wait