-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdev.sh
More file actions
executable file
·353 lines (308 loc) · 10 KB
/
dev.sh
File metadata and controls
executable file
·353 lines (308 loc) · 10 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
#!/usr/bin/env bash
# dev.sh — start/stop backend and frontend for local development
#
# Usage:
# ./dev.sh start # start both backend (:8000) and frontend (:5173)
# ./dev.sh stop # stop both
# ./dev.sh restart # stop then start
# ./dev.sh status # show running processes
# ./dev.sh logs # tail logs from both services
set -euo pipefail
# ── Colour helpers (defined first so _ensure_node22 can use them) ─────────────
green() { printf '\033[0;32m%s\033[0m\n' "$*"; }
yellow() { printf '\033[0;33m%s\033[0m\n' "$*"; }
red() { printf '\033[0;31m%s\033[0m\n' "$*"; }
bold() { printf '\033[1m%s\033[0m\n' "$*"; }
# ── Auto-load nvm and switch to Node 22 if needed ────────────────────────────
_load_nvm() {
# Source nvm if not already loaded
if ! command -v nvm &>/dev/null; then
local nvm_dir="${NVM_DIR:-$HOME/.nvm}"
if [[ -s "$nvm_dir/nvm.sh" ]]; then
# shellcheck source=/dev/null
source "$nvm_dir/nvm.sh"
fi
fi
}
_ensure_node22() {
_load_nvm
local node_major
node_major=$(node -e "process.stdout.write(process.versions.node.split('.')[0])" 2>/dev/null || echo "0")
if (( node_major < 22 )); then
if command -v nvm &>/dev/null; then
yellow " · Node $node_major detected — switching to Node 22 via nvm…"
nvm use 22
fi
fi
}
REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
BACKEND_DIR="$REPO_ROOT/backend"
FRONTEND_DIR="$REPO_ROOT/frontend"
LOG_DIR="$REPO_ROOT/.dev-logs"
BACKEND_PID_FILE="$LOG_DIR/backend.pid"
FRONTEND_PID_FILE="$LOG_DIR/frontend.pid"
MCP_PID_FILE="$LOG_DIR/mcp.pid"
BACKEND_LOG="$LOG_DIR/backend.log"
FRONTEND_LOG="$LOG_DIR/frontend.log"
MCP_LOG="$LOG_DIR/mcp.log"
# Resolve absolute paths to tools now (before any subshells lose PATH context)
UV="$(command -v uv 2>/dev/null || true)"
NPM="$(command -v npm 2>/dev/null || true)"
# ── Helpers ──────────────────────────────────────────────────────────────────
is_running() {
local pid_file="$1"
[[ -f "$pid_file" ]] && kill -0 "$(cat "$pid_file")" 2>/dev/null
}
stop_service() {
local name="$1"
local pid_file="$2"
if is_running "$pid_file"; then
local pid
pid=$(cat "$pid_file")
kill "$pid" 2>/dev/null || true
# Wait up to 5s for graceful exit
local i=0
while kill -0 "$pid" 2>/dev/null && (( i < 10 )); do
sleep 0.5; (( i++ ))
done
kill -9 "$pid" 2>/dev/null || true
rm -f "$pid_file"
green " ✓ $name stopped"
else
yellow " · $name was not running"
rm -f "$pid_file"
fi
}
check_prereqs() {
local ok=true
if [[ -z "$UV" ]]; then
red " ✗ uv not found — install from https://docs.astral.sh/uv/"
ok=false
fi
# Try to switch to Node 22 via nvm before checking
_ensure_node22
# Re-resolve npm after nvm may have changed the active node version
NPM="$(command -v npm 2>/dev/null || true)"
if ! command -v node &>/dev/null; then
red " ✗ node not found — install Node 22 via nvm: nvm install 22 && nvm use 22"
ok=false
else
local node_major
node_major=$(node -e "process.stdout.write(process.versions.node.split('.')[0])")
if (( node_major < 22 )); then
red " ✗ Node $node_major detected — need Node 22+. Install with: nvm install 22"
ok=false
fi
fi
if [[ -z "$NPM" ]]; then
red " ✗ npm not found"
ok=false
fi
[[ "$ok" == true ]]
}
# ── Start ─────────────────────────────────────────────────────────────────────
start_backend() {
if is_running "$BACKEND_PID_FILE"; then
yellow " · backend already running (pid $(cat "$BACKEND_PID_FILE"))"
return
fi
bold " Starting backend…"
# Ensure deps are installed
(cd "$BACKEND_DIR" && "$UV" sync)
# Create data dir
mkdir -p "$REPO_ROOT/data"
# Start uvicorn — nohup + disown survive subshell and terminal detach
(
cd "$BACKEND_DIR"
export DATA_DIR="$REPO_ROOT/data"
export CORS_ORIGINS='["http://localhost:5173"]'
nohup "$UV" run uvicorn app.main:app \
--host 0.0.0.0 \
--port 8000 \
--reload \
>> "$BACKEND_LOG" 2>&1 &
echo $! > "$BACKEND_PID_FILE"
disown $!
)
# Wait for it to be ready (up to 30s)
printf " waiting for backend"
local i=0
while (( i < 60 )); do
if curl -sf --connect-timeout 1 http://localhost:8000/health &>/dev/null; then
echo ""
green " ✓ backend ready at http://localhost:8000"
return
fi
# Bail early if the process already died
if [[ -f "$BACKEND_PID_FILE" ]] && ! kill -0 "$(cat "$BACKEND_PID_FILE")" 2>/dev/null; then
echo ""
red " ✗ backend process exited unexpectedly. Last log lines:"
echo ""
tail -20 "$BACKEND_LOG" | sed 's/^/ /'
echo ""
return 1
fi
printf "."
sleep 0.5; (( i++ ))
done
echo ""
red " ✗ backend did not respond within 30s. Last log lines:"
echo ""
tail -20 "$BACKEND_LOG" | sed 's/^/ /'
echo ""
return 1
}
start_frontend() {
if is_running "$FRONTEND_PID_FILE"; then
yellow " · frontend already running (pid $(cat "$FRONTEND_PID_FILE"))"
return
fi
bold " Starting frontend…"
# Install deps if node_modules is missing
if [[ ! -d "$FRONTEND_DIR/node_modules" ]]; then
(cd "$FRONTEND_DIR" && "$NPM" install --silent)
fi
(
cd "$FRONTEND_DIR"
export VITE_API_BASE_URL="http://localhost:8000"
nohup "$NPM" run dev -- --port 5173 \
>> "$FRONTEND_LOG" 2>&1 &
echo $! > "$FRONTEND_PID_FILE"
disown $!
)
# Wait for Vite to be ready (up to 30s)
printf " waiting for frontend"
local i=0
while (( i < 60 )); do
if curl -sf --connect-timeout 1 http://localhost:5173 &>/dev/null; then
echo ""
green " ✓ frontend ready at http://localhost:5173"
return
fi
printf "."
sleep 0.5; (( i++ ))
done
echo ""
yellow " ⚠ frontend started but not yet responding — check $FRONTEND_LOG"
}
start_mcp() {
if is_running "$MCP_PID_FILE"; then
yellow " · MCP server already running (pid $(cat "$MCP_PID_FILE"))"
return
fi
bold " Starting MCP server…"
(
cd "$BACKEND_DIR"
nohup "$UV" run python mcp_server.py --transport http --port 8001 \
>> "$MCP_LOG" 2>&1 &
echo $! > "$MCP_PID_FILE"
disown $!
)
printf " waiting for MCP server"
local i=0
while (( i < 20 )); do
if nc -z localhost 8001 2>/dev/null; then
echo ""
green " ✓ MCP server ready at http://localhost:8001/mcp"
return
fi
if [[ -f "$MCP_PID_FILE" ]] && ! kill -0 "$(cat "$MCP_PID_FILE")" 2>/dev/null; then
echo ""
red " ✗ MCP server process exited unexpectedly. Last log lines:"
echo ""
tail -20 "$MCP_LOG" | sed 's/^/ /'
echo ""
return 1
fi
printf "."
sleep 0.5; (( i++ ))
done
echo ""
yellow " ⚠ MCP server started but not yet responding — check $MCP_LOG"
}
cmd_mcp() {
bold "code-graph dev — starting MCP server (SSE)"
echo ""
mkdir -p "$LOG_DIR"
start_mcp
echo ""
bold "Log:"
echo " MCP server → $MCP_LOG"
echo ""
echo "Run './dev.sh stop' to shut down."
}
cmd_start() {
bold "code-graph dev — starting services"
echo ""
if ! check_prereqs; then
echo ""
red "Fix the above issues and retry."
exit 1
fi
mkdir -p "$LOG_DIR"
start_backend
start_frontend
start_mcp
echo ""
bold "Logs:"
echo " backend → $BACKEND_LOG"
echo " frontend → $FRONTEND_LOG"
echo " MCP server → $MCP_LOG"
echo ""
echo "Run './dev.sh stop' to shut down."
}
# ── Stop ──────────────────────────────────────────────────────────────────────
cmd_stop() {
bold "code-graph dev — stopping services"
echo ""
stop_service "MCP server" "$MCP_PID_FILE"
stop_service "backend" "$BACKEND_PID_FILE"
stop_service "frontend" "$FRONTEND_PID_FILE"
echo ""
}
# ── Status ────────────────────────────────────────────────────────────────────
cmd_status() {
bold "code-graph dev — status"
echo ""
if is_running "$BACKEND_PID_FILE"; then
green " ✓ backend running (pid $(cat "$BACKEND_PID_FILE")) http://localhost:8000"
else
red " ✗ backend not running"
fi
if is_running "$FRONTEND_PID_FILE"; then
green " ✓ frontend running (pid $(cat "$FRONTEND_PID_FILE")) http://localhost:5173"
else
red " ✗ frontend not running"
fi
if is_running "$MCP_PID_FILE"; then
green " ✓ MCP server running (pid $(cat "$MCP_PID_FILE")) http://localhost:8001/mcp"
else
yellow " · MCP server not running (start with './dev.sh mcp')"
fi
echo ""
}
# ── Logs ──────────────────────────────────────────────────────────────────────
cmd_logs() {
if [[ ! -d "$LOG_DIR" ]]; then
yellow "No logs yet — run './dev.sh start' first."
exit 0
fi
bold "Tailing logs (Ctrl-C to stop)…"
echo ""
local logs=("$BACKEND_LOG" "$FRONTEND_LOG")
[[ -f "$MCP_LOG" ]] && logs+=("$MCP_LOG")
tail -f "${logs[@]}"
}
# ── Dispatch ──────────────────────────────────────────────────────────────────
case "${1:-}" in
start) cmd_start ;;
stop) cmd_stop ;;
restart) cmd_stop; echo ""; cmd_start ;;
status) cmd_status ;;
logs) cmd_logs ;;
mcp) cmd_mcp ;;
*)
echo "Usage: $0 {start|stop|restart|status|logs|mcp}"
exit 1
;;
esac