-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstart-dev.sh
More file actions
executable file
·100 lines (88 loc) · 3.07 KB
/
Copy pathstart-dev.sh
File metadata and controls
executable file
·100 lines (88 loc) · 3.07 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
#!/usr/bin/env bash
set -e
# Hot-reload dev server — runs TypeScript directly via ts-node
# Tracks its own PID to safely kill only its own previous instance
PID_FILE="/tmp/remote-coder-dev.pid"
# Kill our previous dev instance (by PID file)
if [ -f "$PID_FILE" ]; then
OLD_PID=$(cat "$PID_FILE")
if kill -0 "$OLD_PID" 2>/dev/null; then
echo "[dev] Stopping previous instance (PID $OLD_PID) and children..."
# Kill the process group (npx + ts-node child) so nothing lingers
kill -- -"$OLD_PID" 2>/dev/null || kill "$OLD_PID" 2>/dev/null || true
sleep 1
fi
rm -f "$PID_FILE"
fi
cleanup() {
echo ""
echo "[dev] Shutting down..."
kill "$SERVER_PID" 2>/dev/null || true
rm -f "$PID_FILE"
exit 0
}
trap cleanup INT TERM
echo "[dev] Starting dev server with hot reload..."
echo "[dev] Press Ctrl+C to stop."
# Show Cloudflare tunnel URL if one is running
CF_URL=$(cat /tmp/cloudflared*.log 2>/dev/null | grep -o 'https://[^ ]*trycloudflare.com' | tail -1)
if [ -n "$CF_URL" ]; then
echo "[dev] Cloudflare tunnel: $CF_URL"
fi
while true; do
npx ts-node src/server.ts &
SERVER_PID=$!
echo "$SERVER_PID" > "$PID_FILE"
echo "[dev] Server started (PID $SERVER_PID)"
# Wait for ts-node compilation to finish before watching for changes
sleep 3
# Check server is still alive after startup
if ! kill -0 $SERVER_PID 2>/dev/null; then
echo "[dev] Server failed to start. Not restarting (fix the error first)."
rm -f "$PID_FILE"
exit 1
fi
# Watch for file changes (only user-initiated edits, not ts-node reads)
FILE_CHANGED=false
if command -v inotifywait &>/dev/null; then
# inotifywait (no -m) exits after the first matching event
inotifywait -r -e modify,create,delete --include '\.(ts|json|md|html)$' src/ &
WATCH_PID=$!
# Wait for either: file change (inotifywait exits) or server crash
wait -n $SERVER_PID $WATCH_PID 2>/dev/null
# Determine what happened
if ! kill -0 $WATCH_PID 2>/dev/null; then
FILE_CHANGED=true
fi
kill $SERVER_PID 2>/dev/null || true
kill $WATCH_PID 2>/dev/null || true
wait $SERVER_PID 2>/dev/null || true
wait $WATCH_PID 2>/dev/null || true
else
# Fallback: poll for changes every 2s
HASH=$(find src/ -name '*.ts' -o -name '*.json' -o -name '*.md' -o -name '*.html' | sort | xargs stat -c '%Y %n' 2>/dev/null | md5sum)
while true; do
sleep 2
NEW_HASH=$(find src/ -name '*.ts' -o -name '*.json' -o -name '*.md' -o -name '*.html' | sort | xargs stat -c '%Y %n' 2>/dev/null | md5sum)
if [ "$HASH" != "$NEW_HASH" ]; then
echo "[dev] File change detected, restarting..."
FILE_CHANGED=true
break
fi
if ! kill -0 $SERVER_PID 2>/dev/null; then
echo "[dev] Server exited unexpectedly."
break
fi
HASH="$NEW_HASH"
done
kill $SERVER_PID 2>/dev/null || true
wait $SERVER_PID 2>/dev/null || true
fi
if [ "$FILE_CHANGED" = false ]; then
echo "[dev] Server crashed (no file change detected). Not restarting."
rm -f "$PID_FILE"
exit 1
fi
echo "[dev] Restarting in 1s..."
sleep 1
done