Repository navigation
Expand file tree
/
Copy pathdev.sh
More file actions
executable file
·119 lines (104 loc) · 3.89 KB
/
Copy pathdev.sh
File metadata and controls
executable file
·119 lines (104 loc) · 3.89 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
#!/usr/bin/env bash
#
# dev.sh — one command to run Household Finance locally.
#
# Bootstraps the backend virtualenv + deps and the frontend node_modules on
# first run (or when they're missing), then starts both dev servers together
# and shuts them down cleanly on Ctrl+C.
#
# ./dev.sh # setup (if needed) + run backend + frontend
# ./dev.sh setup # only install/refresh dependencies, don't run
#
# Backend: http://localhost:8000 (FastAPI, auto-reload)
# Frontend: http://localhost:5173 (Vite, proxies /api -> :8000)
set -euo pipefail
# Monitor mode: each background job (below) runs in its own process group, so we
# can signal the whole tree — uvicorn's reload worker, npm's Vite child — at once.
set -m
ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
BACKEND="$ROOT/backend"
FRONTEND="$ROOT/frontend"
VENV="$BACKEND/.venv"
# --- pretty logging ----------------------------------------------------------
c_reset=$'\033[0m'; c_blue=$'\033[34m'; c_green=$'\033[32m'; c_yellow=$'\033[33m'
info() { printf '%s==>%s %s\n' "$c_blue" "$c_reset" "$*"; }
ok() { printf '%s==>%s %s\n' "$c_green" "$c_reset" "$*"; }
warn() { printf '%s==>%s %s\n' "$c_yellow" "$c_reset" "$*"; }
# --- prerequisite checks -----------------------------------------------------
need() { command -v "$1" >/dev/null 2>&1 || { echo "Missing required tool: $1" >&2; exit 1; }; }
need python3
need node
need npm
# --- backend setup -----------------------------------------------------------
setup_backend() {
if [ ! -d "$VENV" ]; then
info "Creating backend virtualenv (.venv)…"
python3 -m venv "$VENV"
fi
# Install deps only when the venv is fresh or pyproject changed since last install.
local stamp="$VENV/.deps-stamp"
if [ ! -f "$stamp" ] || [ "$BACKEND/pyproject.toml" -nt "$stamp" ]; then
info "Installing backend dependencies…"
"$VENV/bin/python" -m pip install --quiet --upgrade pip
(cd "$BACKEND" && "$VENV/bin/pip" install --quiet -e ".[dev]")
touch "$stamp"
ok "Backend dependencies ready."
else
ok "Backend dependencies up to date."
fi
}
# --- frontend setup ----------------------------------------------------------
setup_frontend() {
if [ ! -d "$FRONTEND/node_modules" ] || [ "$FRONTEND/package-lock.json" -nt "$FRONTEND/node_modules" ]; then
info "Installing frontend dependencies (npm install)…"
(cd "$FRONTEND" && npm install --silent)
ok "Frontend dependencies ready."
else
ok "Frontend dependencies up to date."
fi
}
setup_backend
setup_frontend
if [ "${1:-}" = "setup" ]; then
ok "Setup complete. Run ./dev.sh to start the app."
exit 0
fi
# --- run both servers --------------------------------------------------------
pids=()
# Recursively terminate a process and all its descendants. uvicorn --reload
# spawns a worker child and npm spawns the Vite process, so killing only the
# top pid would orphan them on ports 8000/5173. Depth-first: children first.
kill_tree() {
local pid=$1
local child
for child in $(pgrep -P "$pid" 2>/dev/null); do
kill_tree "$child"
done
kill -TERM "$pid" 2>/dev/null || true
}
cleaned=0
cleanup() {
[ "$cleaned" = 1 ] && return; cleaned=1
warn "Shutting down…"
for pid in "${pids[@]}"; do
kill_tree "$pid"
done
wait 2>/dev/null || true
}
trap cleanup INT TERM EXIT
info "Starting backend → http://localhost:8000"
(cd "$BACKEND" && exec "$VENV/bin/uvicorn" main:app --reload --port 8000) &
pids+=($!)
info "Starting frontend → http://localhost:5173"
(cd "$FRONTEND" && exec npm run dev) &
pids+=($!)
ok "Both servers running. Open http://localhost:5173 (Ctrl+C to stop)"
# Wait for either process to exit; cleanup trap stops the other.
# (Not `wait -n`: that needs bash 4.3+, but macOS ships bash 3.2 as /bin/sh,
# which is what `sh dev.sh` invokes regardless of this file's shebang.)
while :; do
for pid in "${pids[@]}"; do
kill -0 "$pid" 2>/dev/null || exit 0
done
sleep 1
done