Files
spiceflow/script/dev

153 lines
4.6 KiB
Bash
Executable File

#!/bin/bash
# Development script - runs backend REPL with auto-reload and frontend
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
ROOT_DIR="$(dirname "$SCRIPT_DIR")"
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
BACKEND_PID=""
FRONTEND_PID=""
WATCHER_PID=""
NREPL_PORT=7888
cleanup() {
echo -e "\n${YELLOW}Shutting down...${NC}"
# Kill file watcher
if [ -n "$WATCHER_PID" ]; then
pkill -P $WATCHER_PID 2>/dev/null || true
kill $WATCHER_PID 2>/dev/null || true
fi
# Kill backend and all its children
if [ -n "$BACKEND_PID" ]; then
pkill -P $BACKEND_PID 2>/dev/null || true
kill $BACKEND_PID 2>/dev/null || true
fi
# Kill frontend and all its children
if [ -n "$FRONTEND_PID" ]; then
pkill -P $FRONTEND_PID 2>/dev/null || true
kill $FRONTEND_PID 2>/dev/null || true
fi
# Also kill any orphaned processes on our ports
fuser -k 3000/tcp 2>/dev/null || true
fuser -k 5173/tcp 2>/dev/null || true
fuser -k $NREPL_PORT/tcp 2>/dev/null || true
wait 2>/dev/null || true
echo -e "${GREEN}Stopped${NC}"
# Restore terminal
stty sane 2>/dev/null || true
exit 0
}
trap cleanup SIGINT SIGTERM SIGHUP EXIT
# Check for clj-nrepl-eval (required for auto-reload)
HAS_NREPL_EVAL=true
if ! command -v clj-nrepl-eval &> /dev/null; then
echo -e "${YELLOW}Warning: clj-nrepl-eval not found. Auto-reload will be disabled.${NC}"
HAS_NREPL_EVAL=false
fi
# Check for inotifywait (preferred) or fall back to polling
HAS_INOTIFY=false
if command -v inotifywait &> /dev/null; then
HAS_INOTIFY=true
fi
echo -e "${BLUE}=== Starting Spiceflow Development Environment ===${NC}\n"
# Start backend REPL
echo -e "${GREEN}Starting backend REPL...${NC}"
cd "$ROOT_DIR/server"
# Start nREPL server and run (start) - no hawk watcher, we use inotifywait instead
clj -M:dev -e "
(require 'nrepl.server)
(def server (nrepl.server/start-server :port $NREPL_PORT))
(println \"nREPL server started on port $NREPL_PORT\")
(require 'user)
(user/start)
@(promise)
" &
BACKEND_PID=$!
# Wait for backend to be ready
echo -e "${YELLOW}Waiting for backend...${NC}"
until curl -s http://localhost:3000/api/health > /dev/null 2>&1; do
sleep 1
done
echo -e "${GREEN}Backend ready on http://localhost:3000${NC}"
echo -e "${GREEN}nREPL available on port $NREPL_PORT${NC}"
# Start file watcher for auto-reload
if [ "$HAS_NREPL_EVAL" = true ]; then
echo -e "${GREEN}Starting file watcher for auto-reload...${NC}"
if [ "$HAS_INOTIFY" = true ]; then
# Use inotifywait (efficient, event-based)
(
cd "$ROOT_DIR/server"
while inotifywait -r -e modify,create,delete,move --include '.*\.clj$' src/ 2>/dev/null; do
echo -e "${YELLOW}File change detected, reloading...${NC}"
clj-nrepl-eval -p $NREPL_PORT "(user/reset)" > /dev/null 2>&1 &
done
) &
WATCHER_PID=$!
else
# Fallback: polling with find (works everywhere)
(
cd "$ROOT_DIR/server"
LAST_HASH=""
while true; do
CURRENT_HASH=$(find src -name '*.clj' -exec stat -c '%Y %n' {} \; 2>/dev/null | md5sum)
if [ -n "$LAST_HASH" ] && [ "$CURRENT_HASH" != "$LAST_HASH" ]; then
echo -e "${YELLOW}File change detected, reloading...${NC}"
clj-nrepl-eval -p $NREPL_PORT "(user/reset)" > /dev/null 2>&1 &
fi
LAST_HASH="$CURRENT_HASH"
sleep 2
done
) &
WATCHER_PID=$!
echo -e "${YELLOW}Using polling for file watching (install inotify-tools for better performance)${NC}"
fi
echo -e "${GREEN}Auto-reload enabled - editing .clj files will trigger reload${NC}"
fi
# Start frontend
echo -e "${GREEN}Starting frontend server...${NC}"
cd "$ROOT_DIR/client"
npm run dev &
FRONTEND_PID=$!
# Wait for frontend to be ready
echo -e "${YELLOW}Waiting for frontend...${NC}"
until curl -sk https://localhost:5173 > /dev/null 2>&1; do
sleep 1
done
echo -e "${GREEN}Frontend ready on https://0.0.0.0:5173${NC}"
# Get local IP for phone access
LOCAL_IP=$(hostname -I | awk '{print $1}')
echo -e "\n${BLUE}=== Spiceflow Development Environment Ready ===${NC}"
echo -e "${GREEN}Backend:${NC} http://localhost:3000"
echo -e "${GREEN}nREPL:${NC} localhost:$NREPL_PORT"
echo -e "${GREEN}Frontend:${NC} https://localhost:5173"
echo -e "${GREEN}Phone:${NC} https://${LOCAL_IP}:5173"
echo -e "\n${YELLOW}Edit any .clj file to trigger auto-reload.${NC}"
echo -e "Press Ctrl+C to stop\n"
# Wait for processes
wait