-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdev.sh
More file actions
executable file
·56 lines (47 loc) · 1.61 KB
/
Copy pathdev.sh
File metadata and controls
executable file
·56 lines (47 loc) · 1.61 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
#!/usr/bin/env bash
# Local dev stack in one command:
# - Go API on localhost:8080
# - Vite dev server on localhost:5173 (proxies /api to the Go server)
# One Ctrl-C stops everything; if either server dies, the other is stopped.
# If a port is already taken (usually a stale server), offers to kill it.
set -euo pipefail
cd "$(dirname "$0")"
# free_port checks whether something is listening on the port and, when run
# interactively, offers to kill it. Otherwise it refuses to continue: never
# kill processes silently.
free_port() {
local port="$1" name="$2"
local listener_pid
listener_pid=$(ss -tlnp "sport = :$port" 2>/dev/null | grep -oP 'pid=\K[0-9]+' | head -1 || true)
[ -z "$listener_pid" ] && return 0
local listener_cmd
listener_cmd=$(ps -p "$listener_pid" -o comm= 2>/dev/null || echo unknown)
echo "dev.sh: port $port ($name) is already in use by PID $listener_pid ($listener_cmd)."
local answer="n"
if [ -t 0 ]; then
read -r -p "Kill it and continue? [Y/n] " answer
answer=${answer:-y}
fi
case "$answer" in
[Yy]*)
kill "$listener_pid"
sleep 0.5
;;
*)
echo "dev.sh: aborting; free port $port first."
exit 1
;;
esac
}
free_port 8080 "Go API"
free_port 5173 "Vite"
if [ ! -d web/node_modules ]; then
echo "dev.sh: first run, installing frontend dependencies..."
(cd web && npm install)
fi
# On exit (Ctrl-C included), stop whichever background job is still running.
trap 'kill $(jobs -p) 2>/dev/null' EXIT
go run ./cmd/spreadlab &
(cd web && npm run dev) &
# wait -n returns when the FIRST job exits; the trap then cleans up the rest.
wait -n