-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathdev-start.sh
More file actions
executable file
·188 lines (162 loc) · 5.08 KB
/
Copy pathdev-start.sh
File metadata and controls
executable file
·188 lines (162 loc) · 5.08 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
#!/bin/bash
# InnoClaw Dev Start Script
cd "$(dirname "$0")"
PORT=3000
if [ -n "${INNOCLAW_DEV_START_TEST_PATH:-}" ]; then
PATH="${INNOCLAW_DEV_START_TEST_PATH}:$PATH"
hash -r
fi
server_responding() {
command -v curl >/dev/null 2>&1 || return 1
command -v node >/dev/null 2>&1 || return 1
local body_file meta status content_type node_status
body_file=$(mktemp) || return 1
meta=$(curl --noproxy "*" -sS -o "$body_file" -w "%{http_code}\n%{content_type}" "http://127.0.0.1:$PORT/api/auth/me" 2>/dev/null) || {
rm -f "$body_file"
return 1
}
status=$(printf '%s\n' "$meta" | sed -n '1p')
content_type=$(printf '%s\n' "$meta" | sed -n '2p')
case "$(printf '%s' "$content_type" | tr '[:upper:]' '[:lower:]')" in
*application/json*) ;;
*)
rm -f "$body_file"
return 1
;;
esac
node - "$status" "$body_file" <<'NODE'
const [status, bodyPath] = process.argv.slice(2);
const { readFileSync } = require("node:fs");
let body;
try {
body = JSON.parse(readFileSync(bodyPath, "utf8"));
} catch {
process.exit(1);
}
if (status === "200" && body && typeof body.authMode === "string" && body.user) {
process.exit(0);
}
if (status === "401" && body?.error === "Unauthorized") {
process.exit(0);
}
process.exit(1);
NODE
node_status=$?
rm -f "$body_file"
return "$node_status"
}
pid_elapsed_seconds() {
ps -p "$1" -o etimes= 2>/dev/null | tr -d ' '
}
pid_workdir() {
readlink "/proc/$1/cwd" 2>/dev/null
}
is_repo_dev_process() {
local pid=$1
local cwd=$(pid_workdir "$pid")
local cmdline=$(ps -p "$pid" -o args= 2>/dev/null)
[ "$cwd" = "$PWD" ] && echo "$cmdline" | grep -qE "(npm run dev|next dev|node.*next)"
}
if [ "${INNOCLAW_DEV_START_TEST_HOOK:-}" = "1" ]; then
case "${1:-}" in
server_responding)
server_responding
exit $?
;;
*)
echo "Unknown dev-start test hook: ${1:-}" >&2
exit 64
;;
esac
fi
# Check if already running
if [ -f .dev.pid ]; then
PID=$(cat .dev.pid)
if ! echo "$PID" | grep -qE '^[0-9]+$'; then
echo "Invalid PID in .dev.pid, removing file"
rm -f .dev.pid
elif ps -p "$PID" > /dev/null 2>&1; then
if ! is_repo_dev_process "$PID"; then
PID_CWD=$(pid_workdir "$PID")
echo ".dev.pid points to a live non-server process (PID: $PID${PID_CWD:+, cwd: $PID_CWD}). Removing stale file."
rm -f .dev.pid
elif server_responding; then
echo "Dev server is already running (PID: $PID)"
exit 0
else
PID_AGE=$(pid_elapsed_seconds "$PID")
if [ -n "$PID_AGE" ] && [ "$PID_AGE" -le 30 ]; then
echo "Dev server is still starting (PID: $PID, age: ${PID_AGE}s)"
exit 0
fi
echo "Dev server PID $PID is not healthy. Restarting it..."
kill "$PID" 2>/dev/null
sleep 2
if ps -p "$PID" > /dev/null 2>&1; then
echo "Force killing stale dev server..."
kill -9 "$PID" 2>/dev/null
sleep 1
fi
rm -f .dev.pid
fi
else
rm -f .dev.pid
fi
fi
# Check if port is occupied
check_port() {
local port=$1
local pids=$(lsof -t -i:$port 2>/dev/null)
if [ -n "$pids" ]; then
echo "Port $port is occupied by process: $pids"
for pid in $pids; do
local process_name=$(ps -p "$pid" -o comm= 2>/dev/null)
local cmdline=$(ps -p "$pid" -o args= 2>/dev/null)
echo "Process $pid: ${process_name:-unknown} ${cmdline:+($cmdline)}"
done
return 0
fi
return 1
}
port_owned_by_pid_file() {
local port=$1
[ -f .dev.pid ] || return 1
local expected_pid=$(cat .dev.pid)
echo "$expected_pid" | grep -qE '^[0-9]+$' || return 1
is_repo_dev_process "$expected_pid" || return 1
local pids=$(lsof -t -i:$port 2>/dev/null)
[ -n "$pids" ] || return 1
for pid in $pids; do
if [ "$pid" = "$expected_pid" ]; then
return 0
fi
done
return 1
}
# Check and resolve port conflict
if check_port $PORT; then
if port_owned_by_pid_file $PORT && server_responding; then
echo "Dev server is already running (PID: $(cat .dev.pid))"
exit 0
fi
echo "Error: Port $PORT is already in use by a process not managed by this repo's .dev.pid."
echo "Stop that process or remove the conflict before running dev-start.sh."
exit 1
fi
# Install dependencies if needed
if [ ! -d "node_modules" ] || [ "package.json" -nt "node_modules" ]; then
echo "Installing dependencies..."
npm install
fi
# Run database migrations
echo "Running database migrations..."
npx drizzle-kit migrate 2>&1 | grep -v "^Reading config\|^No config"
# Create logs directory
mkdir -p logs
# Start dev server in background
echo "Starting dev server..."
nohup npm run dev > logs/dev.log 2>&1 &
echo $! > .dev.pid
echo "Dev server started (PID: $(cat .dev.pid))"
echo "Logs: logs/dev.log"
echo "URL: http://localhost:3000"