Repository navigation
Expand file tree
/
Copy pathrun.sh
More file actions
executable file
·288 lines (228 loc) · 6.6 KB
/
Copy pathrun.sh
File metadata and controls
executable file
·288 lines (228 loc) · 6.6 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
#!/bin/bash
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
CLIENT_DIR="$SCRIPT_DIR/client"
E2E_DIR="$SCRIPT_DIR/e2e"
SERVER_DIR="$SCRIPT_DIR/server"
SOLUTION="$SERVER_DIR/Blocks.slnx"
API_PROJECT="$SCRIPT_DIR/server/Api/Api.csproj"
WORKER_PROJECT="$SCRIPT_DIR/server/Worker/Worker.csproj"
WWWROOT_DIR="$SCRIPT_DIR/server/Api/wwwroot"
API_PORT=5000
FRONTEND_PORT=4000
# Ensure SSL vars are explicitly in scope for Vite
export DATA_SSL_CERT="${DATA_SSL_CERT:-}"
export DATA_SSL_KEY="${DATA_SSL_KEY:-}"
API_PID=""
WORKER_PID=""
usage() {
cat <<EOF
Usage: $0 [OPTION]
Options:
-a, --all Build frontend + run API + Worker
-b, --backend Run .NET API
-w, --worker Run .NET Worker
-f, --frontend Run frontend dev server
-k, --kill-port Kill API port ($API_PORT)
-n, --npm Run npm command inside client/
-h, --help Show help
Tests:
-tf, --test-fe Frontend unit tests (client/: build + vitest)
-te, --test-e2e End-to-end tests (e2e/: playwright)
-tb, --test-be Backend unit tests (server/: clean + build + dotnet test)
-ta, --test-all Frontend unit + backend unit + e2e
Env:
SKIP_BUILD=1 Skip the client build step in --test-fe
Examples:
$0 -a
$0 -b
$0 -f
$0 -k
$0 -tf
$0 -tb
$0 -te
EOF
exit 1
}
# ---------- PORT CLEANUP ----------
free_port() {
local PORT=$1
if command -v lsof >/dev/null 2>&1; then
local pids
pids="$(lsof -tiTCP:$PORT -sTCP:LISTEN || true)"
if [ -n "$pids" ]; then
echo "Port $PORT in use by: $pids — killing..."
for pid in $pids; do
kill "$pid" 2>/dev/null || true
done
sleep 1
fi
else
local pids
pids="$(netstat -ano 2>/dev/null | grep ":$PORT" | awk '{print $5}' | sort -u || true)"
if [ -n "$pids" ]; then
echo "Port $PORT in use by: $pids — killing..."
for pid in $pids; do
taskkill //PID "$pid" //F >/dev/null 2>&1 || true
done
fi
fi
}
# ---------- CLEANUP ----------
cleanup() {
echo "Shutting down..."
[ -n "${API_PID:-}" ] && kill "$API_PID" 2>/dev/null || true
[ -n "${WORKER_PID:-}" ] && kill "$WORKER_PID" 2>/dev/null || true
}
trap cleanup EXIT INT TERM
# ---------- FRONTEND ----------
run_frontend() {
echo "Starting frontend..."
if [ ! -d "$CLIENT_DIR/node_modules" ]; then
echo "Installing dependencies..."
(cd "$CLIENT_DIR" && npm clean-install)
fi
free_port $FRONTEND_PORT
cd "$CLIENT_DIR" && npm run dev
}
build_frontend() {
echo "Building frontend..."
pushd "$CLIENT_DIR" > /dev/null
npm install
npm run build
popd > /dev/null
mkdir -p "$WWWROOT_DIR"
if [ -d "$CLIENT_DIR/dist" ]; then
echo "Syncing dist → wwwroot..."
if command -v rsync >/dev/null 2>&1; then
rsync -a --delete "$CLIENT_DIR/dist/" "$WWWROOT_DIR/"
else
rm -rf "$WWWROOT_DIR"/*
cp -r "$CLIENT_DIR/dist/"* "$WWWROOT_DIR/"
fi
fi
}
# ---------- BACKEND ----------
# HTTPS is driven by the machine env vars DATA_SSL_CERT / DATA_SSL_KEY.
# Both set + both files present -> HTTPS on $API_PORT; otherwise -> HTTP (fallback).
configure_backend_tls() {
if [ -n "${DATA_SSL_CERT:-}" ] && [ -n "${DATA_SSL_KEY:-}" ] \
&& [ -f "$DATA_SSL_CERT" ] && [ -f "$DATA_SSL_KEY" ]; then
export Kestrel__Certificates__Default__Path="$DATA_SSL_CERT"
export Kestrel__Certificates__Default__KeyPath="$DATA_SSL_KEY"
export ASPNETCORE_URLS="https://0.0.0.0:$API_PORT"
echo "Backend TLS: HTTPS on $API_PORT"
else
export ASPNETCORE_URLS="http://0.0.0.0:$API_PORT"
echo "Backend TLS: cert env not set/found — HTTP on $API_PORT"
fi
}
run_backend() {
configure_backend_tls
echo "Running .NET API on port $API_PORT..."
# Pass the URL on the command line: it has higher precedence than the
# launchSettings.json applicationUrl, which would otherwise override
# the ASPNETCORE_URLS we exported above.
dotnet run --project "$API_PROJECT" -- --urls "$ASPNETCORE_URLS"
}
run_worker() {
echo "Running .NET Worker..."
dotnet run --project "$WORKER_PROJECT"
}
# ---------- TESTS ----------
ensure_node_modules() {
local dir=$1
if [ ! -d "$dir/node_modules" ]; then
echo "Installing dependencies in $(basename "$dir")..."
(cd "$dir" && npm clean-install)
fi
}
# Vitest does not read dist/, so the build is only a TypeScript gate.
# Skip it with SKIP_BUILD=1 for a faster loop.
test_frontend() {
echo "=== Frontend unit tests ==="
ensure_node_modules "$CLIENT_DIR"
if [ "${SKIP_BUILD:-0}" = "1" ]; then
echo "SKIP_BUILD=1 — skipping client build."
else
(cd "$CLIENT_DIR" && npm run build)
fi
(cd "$CLIENT_DIR" && npm run test)
}
# Playwright starts the app itself (webServer: run.sh -b) and needs e2e/.env.e2e.
test_e2e() {
echo "=== E2E tests ==="
if [ ! -f "$E2E_DIR/.env.e2e" ]; then
echo "Missing $E2E_DIR/.env.e2e — copy .env.e2e.example and set E2E_BASE_URL + credentials."
exit 1
fi
ensure_node_modules "$E2E_DIR"
(cd "$E2E_DIR" && npx playwright install --no-shell chromium)
(cd "$E2E_DIR" && npm run test)
}
test_backend() {
echo "=== Backend unit tests ==="
dotnet clean "$SOLUTION"
dotnet build "$SOLUTION"
dotnet test "$SOLUTION" --no-build
}
test_all() {
test_frontend
test_backend
test_e2e
}
# ---------- MAIN ----------
if [ $# -eq 0 ]; then
usage
fi
case "$1" in
-k|--kill-port)
free_port $API_PORT
echo "Port $API_PORT cleared."
;;
-f|--frontend)
run_frontend
;;
-b|--backend)
free_port $API_PORT
(cd "$SCRIPT_DIR" && run_backend) &
API_PID=$!
wait $API_PID
;;
-w|--worker)
(cd "$SCRIPT_DIR" && run_worker)
;;
-a|--all)
free_port $API_PORT
build_frontend
echo "Starting services..."
(cd "$SCRIPT_DIR" && run_backend) &
API_PID=$!
(cd "$SCRIPT_DIR" && run_worker) &
WORKER_PID=$!
wait $API_PID $WORKER_PID
;;
-tf|--test-fe)
test_frontend
;;
-te|--test-e2e)
test_e2e
;;
-tb|--test-be)
test_backend
;;
-ta|--test-all)
test_all
;;
-n|--npm)
shift
[ $# -eq 0 ] && echo "Usage: $0 -n <args>" && exit 1
(cd "$CLIENT_DIR" && npm "$@")
;;
-h|--help)
usage
;;
*)
usage
;;
esac