Skip to content

Commit 3160a18

Browse files
fix: improve environment setup scripts
1 parent d72cb9d commit 3160a18

2 files changed

Lines changed: 87 additions & 12 deletions

File tree

src/start.cmd

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ if exist "%API_ENV_FILE%" (
107107
echo 2. Manually create %API_ENV_FILE% with required environment variables
108108
echo 3. Copy an existing .env file to %API_ENV_FILE%
109109
echo.
110-
echo For more information, see: documents/LocalDebuggingSetup.md
110+
echo For more information, see: documents/LocalDevelopmentSetup.md
111111
exit /b 1
112112
)
113113

@@ -131,7 +131,7 @@ set APP_ENV_FILE=%ROOT_DIR%\src\App\.env
131131
(
132132
echo REACT_APP_API_BASE_URL=http://127.0.0.1:8000
133133
) > "%APP_ENV_FILE%"
134-
echo Updated src/App/.env with APP_API_BASE_URL
134+
echo Updated src/App/.env with REACT_APP_API_BASE_URL
135135

136136
REM Add or update APP_ENV="dev" in API .env file
137137
echo Checking for existing APP_ENV in src/api/.env...
@@ -284,6 +284,18 @@ if errorlevel 1 (
284284
)
285285
cd %ROOT_DIR%
286286

287+
REM Close any processes using ports 8000 and 3000
288+
echo Checking for processes using ports 8000 and 3000...
289+
REM Kill any existing processes on ports 8000 and 3000 before starting
290+
for %%P in (8000 3000) do (
291+
for /f "tokens=5" %%A in ('netstat -ano ^| findstr "LISTENING" ^| findstr ":%%P "') do (
292+
if "%%A" neq "0" (
293+
echo Port %%P is already in use by PID %%A. Stopping it...
294+
taskkill /F /PID %%A /T >nul 2>&1
295+
)
296+
)
297+
)
298+
287299
REM Start backend and frontend
288300
echo Starting backend server...
289301
cd %ROOT_DIR%
@@ -297,10 +309,27 @@ timeout /t 30 /nobreak >nul
297309

298310
echo Starting frontend server...
299311
cd %ROOT_DIR%\src\App
300-
call npm start
301312

302-
echo Both servers have been started.
303-
echo Backend running at http://127.0.0.1:8000
304-
echo Frontend running at http://localhost:3000
313+
REM Show server information before starting
314+
echo.
315+
echo ========================================
316+
echo Both servers are now running:
317+
echo Backend: http://127.0.0.1:8000
318+
echo Frontend: http://localhost:3000
319+
echo ========================================
320+
echo Press Ctrl+C to stop all servers
321+
echo.
322+
323+
REM Start npm with PowerShell wrapper for automatic cleanup on Ctrl+C (single line for reliability)
324+
powershell -NoProfile -ExecutionPolicy Bypass -Command "try { npm start } finally { Write-Host ''; Write-Host 'Stopping all processes...'; Start-Sleep -Milliseconds 500; @(8000, 3000) | ForEach-Object { $port = $_; Get-NetTCPConnection -LocalPort $port -State Listen -ErrorAction SilentlyContinue | ForEach-Object { $pid_ = $_.OwningProcess; if ($pid_ -and $pid_ -ne 0) { taskkill /F /PID $pid_ /T 2>$null } } }; Write-Host 'Cleanup complete.'; Write-Host ''; Write-Host 'All servers stopped.' -ForegroundColor Yellow }"
325+
326+
REM Fallback cleanup in case PowerShell finally block was interrupted
327+
for %%P in (8000 3000) do (
328+
for /f "tokens=5" %%A in ('netstat -ano ^| findstr "LISTENING" ^| findstr ":%%P "') do (
329+
if "%%A" neq "0" (
330+
taskkill /F /PID %%A /T >nul 2>&1
331+
)
332+
)
333+
)
305334

306335
endlocal

src/start.sh

Lines changed: 52 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ check_local_env() {
4545
echo " 2. Manually create $API_ENV_FILE with required environment variables"
4646
echo " 3. Copy an existing .env file to $API_ENV_FILE"
4747
echo ""
48-
echo "For more information, see: documents/LocalDebuggingSetup.md"
48+
echo "For more information, see: documents/LocalDevelopmentSetup.md"
4949
exit 1
5050
fi
5151
}
@@ -97,7 +97,12 @@ setup_environment() {
9797
echo "Checking for existing APP_ENV in src/api/.env..."
9898
if grep -q "^APP_ENV=" "$API_ENV_FILE" 2>/dev/null; then
9999
echo "APP_ENV already exists, updating to \"dev\"..."
100-
sed -i 's/^APP_ENV=.*/APP_ENV="dev"/' "$API_ENV_FILE"
100+
# macOS/BSD sed requires -i with extension, use portable approach
101+
if [[ "$OSTYPE" == "darwin"* ]]; then
102+
sed -i '' 's/^APP_ENV=.*/APP_ENV="dev"/' "$API_ENV_FILE"
103+
else
104+
sed -i 's/^APP_ENV=.*/APP_ENV="dev"/' "$API_ENV_FILE"
105+
fi
101106
else
102107
echo "APP_ENV not found, adding APP_ENV=\"dev\"..."
103108
echo 'APP_ENV="dev"' >> "$API_ENV_FILE"
@@ -286,6 +291,15 @@ setup_environment() {
286291
npm install --force || { echo "Failed to restore frontend npm packages"; exit 1; }
287292
cd "$ROOT_DIR"
288293

294+
# Kill any existing processes on ports 8000 and 3000 before starting
295+
for port in 8000 3000; do
296+
pid=$(lsof -ti :"$port" 2>/dev/null)
297+
if [ -n "$pid" ]; then
298+
echo "Port $port is already in use by PID $pid. Stopping it..."
299+
kill -9 $pid
300+
fi
301+
done
302+
289303
# Start backend and frontend
290304
echo "Starting backend server..."
291305
cd "$ROOT_DIR"
@@ -305,11 +319,43 @@ setup_environment() {
305319

306320
echo "Starting frontend server..."
307321
cd "$ROOT_DIR/src/App"
322+
323+
# Show server information before starting
324+
echo ""
325+
echo "========================================"
326+
echo "Both servers are now running:"
327+
echo " Backend: http://127.0.0.1:8000"
328+
echo " Frontend: http://localhost:3000"
329+
echo "========================================"
330+
echo "Press Ctrl+C to stop all servers"
331+
echo ""
332+
333+
# Setup cleanup function to kill both backend and frontend on Ctrl+C
334+
cleanup() {
335+
echo ""
336+
echo "Cleaning up processes..."
337+
sleep 0.5
338+
339+
for port in 8000 3000; do
340+
pid=$(lsof -ti :"$port" 2>/dev/null)
341+
if [ -n "$pid" ]; then
342+
echo "Stopping process on port $port (PID $pid)..."
343+
kill -9 $pid 2>/dev/null
344+
fi
345+
done
346+
347+
echo "All servers stopped."
348+
exit 0
349+
}
350+
351+
# Trap Ctrl+C and other termination signals
352+
trap cleanup INT TERM
353+
354+
# Start npm (this will block until Ctrl+C)
308355
npm start
309-
310-
echo "Both servers have been started."
311-
echo "Backend running at http://127.0.0.1:8000"
312-
echo "Frontend running at http://localhost:3000"
356+
357+
# Cleanup after npm exits normally
358+
cleanup
313359
}
314360

315361
# Check if .azure folder exists first

0 commit comments

Comments
 (0)