-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdev
More file actions
executable file
·133 lines (109 loc) · 3.43 KB
/
Copy pathdev
File metadata and controls
executable file
·133 lines (109 loc) · 3.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#!/bin/bash
# ==========================================
# Configuration
# ==========================================
BACKEND_DIR="/Users/karpinski94/projects/scripts-writer/backend"
FRONTEND_DIR="/Users/karpinski94/projects/scripts-writer/frontend"
BACKEND_CMD="uv run uvicorn app.main:app --reload --use-colors"
FRONTEND_CMD="npm run dev"
BACKEND_PORT=8000
FRONTEND_PORT=3000
LOG_DIR="/tmp/scripts-writer-logs"
BACKEND_LOG="$LOG_DIR/backend.log"
FRONTEND_LOG="$LOG_DIR/frontend.log"
mkdir -p "$LOG_DIR"
touch "$BACKEND_LOG"
touch "$FRONTEND_LOG"
# Force instant logging and colors
export PYTHONUNBUFFERED=1
export FORCE_COLOR=1
export UV_FORCE_COLOR=1
# ==========================================
# Helper Functions
# ==========================================
is_running() {
lsof -Pi :$1 -sTCP:LISTEN -t >/dev/null
}
kill_port() {
local PORT=$1
local PID=$(lsof -Pi :$PORT -sTCP:LISTEN -t)
if [ ! -z "$PID" ]; then
kill -9 $PID >/dev/null 2>&1
fi
}
# --- BACKGROUND MODE (sw start) ---
start_background() {
echo "🚀 Starting servers in the background..."
if ! is_running $BACKEND_PORT; then
cd "$BACKEND_DIR" && nohup $BACKEND_CMD > "$BACKEND_LOG" 2>&1 &
fi
if ! is_running $FRONTEND_PORT; then
cd "$FRONTEND_DIR" && nohup $FRONTEND_CMD > "$FRONTEND_LOG" 2>&1 &
fi
echo "✅ Servers started! They are running invisibly."
echo "ℹ️ Use 'sw logs' to view them, or 'sw stop' to shut them down."
}
# --- FOREGROUND MODE (sw start --logs) ---
start_foreground() {
echo "🚀 Starting servers in FOREGROUND..."
stop_servers >/dev/null
echo "=========================================="
echo "🟢 Backend & Frontend logs live below."
echo "🛑 Press Ctrl+C to stop both servers."
echo "=========================================="
# Start Backend in current terminal
cd "$BACKEND_DIR" && $BACKEND_CMD &
BACKEND_PID=$!
# Start Frontend in current terminal
cd "$FRONTEND_DIR" && $FRONTEND_CMD &
FRONTEND_PID=$!
# Catch Ctrl+C and shut them both down cleanly
trap "echo -e '\n🛑 Ctrl+C detected. Stopping servers...'; kill $BACKEND_PID $FRONTEND_PID 2>/dev/null; exit" INT TERM
wait $BACKEND_PID $FRONTEND_PID
}
stop_servers() {
echo "🛑 Stopping servers..."
kill_port $BACKEND_PORT
kill_port $FRONTEND_PORT
echo "✅ All servers stopped."
}
show_logs() {
echo "=========================================="
echo "📜 Tailing logs... (Press Ctrl+C to exit logs)"
echo "=========================================="
tail -f "$BACKEND_LOG" "$FRONTEND_LOG"
}
# ==========================================
# CLI Routing
# ==========================================
case "$1" in
start)
if [ "$2" = "--logs" ]; then
start_foreground
else
start_background
fi
;;
stop)
stop_servers
;;
restart)
stop_servers
sleep 1
if [ "$2" = "--logs" ]; then
start_foreground
else
start_background
fi
;;
logs)
show_logs
;;
status)
if is_running $BACKEND_PORT; then echo "🟢 Backend: RUNNING"; else echo "🔴 Backend: STOPPED"; fi
if is_running $FRONTEND_PORT; then echo "🟢 Frontend: RUNNING"; else echo "🔴 Frontend: STOPPED"; fi
;;
*)
echo "Usage: sw {start|stop|restart|logs|status} [--logs]"
exit 1
esac