-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstart.bat
More file actions
146 lines (130 loc) · 4.84 KB
/
Copy pathstart.bat
File metadata and controls
146 lines (130 loc) · 4.84 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
@echo off
chcp 65001 >nul
setlocal enabledelayedexpansion
title Smart Calendar Planner
cd /d "%~dp0"
REM -- Disable Vite auto-open (we open browser ourselves after services are ready) --
set "VITE_OPEN=false"
echo.
echo +-----------------------------------------+
echo ^| Smart Calendar Planner ^|
echo +-----------------------------------------+
echo.
REM -- 1. Check Node.js --
where node >nul 2>nul
if %ERRORLEVEL% neq 0 (
echo [ERROR] Node.js is not installed!
echo Please download from https://nodejs.org LTS 18+ and retry.
pause
exit /b 1
)
for /f "tokens=1 delims=." %%v in ('node -v 2^>nul') do set "NODE_VER=%%v"
set "NODE_VER=%NODE_VER:v=%"
if %NODE_VER% lss 18 (
echo [WARNING] Node.js version is below 18. Some features may not work.
) else (
echo [OK] Node.js %NODE_VER%
)
REM -- 2. Init .env if missing --
if not exist .env (
if exist .env.example (
echo [INFO] Creating .env from .env.example ...
copy .env.example .env >nul
)
)
REM -- 3. Install dependencies if node_modules missing --
if not exist node_modules (
echo.
echo -- Installing dependencies --
call npm install
if %ERRORLEVEL% neq 0 (
echo [ERROR] npm install failed! Check your network connection.
pause
exit /b 1
)
echo [OK] Dependencies installed.
echo.
)
REM -- 4. Read backend port from .env --
set "BACKEND_PORT=3000"
if exist .env (
for /f "usebackq tokens=1,* delims==" %%a in (".env") do (
if /i "%%a"=="PORT" set "BACKEND_PORT=%%b"
)
)
REM -- 5. Detect LAN IP (the one with default gateway = router-assigned) --
set "LAN_IP="
for /f "usebackq" %%a in (`powershell -NoProfile -Command "(Get-NetRoute -DestinationPrefix '0.0.0.0/0' -ErrorAction SilentlyContinue | Select-Object -First 1).RouteMetric" 2^>nul`) do (
set "LAN_METRIC=%%a"
)
for /f "usebackq" %%a in (`powershell -NoProfile -Command "try { $r = Get-NetRoute -DestinationPrefix '0.0.0.0/0' -ErrorAction Stop | Sort-Object RouteMetric | Select-Object -First 1; $a = Get-NetAdapter -InterfaceIndex $r.InterfaceIndex -ErrorAction Stop; if ($a.Status -eq 'Up') { (Get-NetIPAddress -InterfaceIndex $r.InterfaceIndex -AddressFamily IPv4 -ErrorAction Stop).IPAddress } } catch {}" 2^>nul`) do (
set "LAN_IP=%%a"
)
REM -- 6. Kill leftover processes on target ports --
for /f "tokens=5" %%a in ('netstat -aon 2^>nul ^| findstr ":%BACKEND_PORT%.*LISTENING"') do (
taskkill /f /t /pid %%a >nul 2>&1
)
for /f "tokens=5" %%a in ('netstat -aon 2^>nul ^| findstr ":5173.*LISTENING"') do (
taskkill /f /t /pid %%a >nul 2>&1
)
REM -- 7. Start backend server --
echo -- Starting backend server (port %BACKEND_PORT%) ...
start /b cmd /c "set PORT=%BACKEND_PORT% && npm run dev:server >nul 2>nul"
echo Waiting for backend ...
set "RETRY=0"
:wait_backend
timeout /t 1 /nobreak >nul
set /a "RETRY=RETRY+1"
if %RETRY% gtr 30 (
echo [WARNING] Backend did not start within 30s.
goto backend_ready
)
powershell -Command "try { $t = New-Object System.Net.Sockets.TcpClient; $t.Connect('localhost', %BACKEND_PORT%); $t.Close(); exit 0 } catch { exit 1 }" >nul 2>nul
if %ERRORLEVEL% neq 0 goto wait_backend
echo [OK] Backend is ready.
:backend_ready
REM -- 8. Start frontend dev server --
echo -- Starting frontend server (port 5173) ...
start /b cmd /c "npm run dev >nul 2>nul"
echo Waiting for frontend ...
set "RETRY=0"
:wait_frontend
timeout /t 1 /nobreak >nul
set /a "RETRY=RETRY+1"
if %RETRY% gtr 30 (
echo [WARNING] Frontend did not start within 30s.
echo Try opening http://localhost:5173 manually.
goto frontend_ready
)
powershell -Command "try { $t = New-Object System.Net.Sockets.TcpClient; $t.Connect('localhost', 5173); $t.Close(); exit 0 } catch { exit 1 }" >nul 2>nul
if %ERRORLEVEL% neq 0 goto wait_frontend
echo [OK] Frontend is ready.
:frontend_ready
REM -- 9. Open default browser --
echo -- Opening browser ...
start "" "http://localhost:5173"
echo.
echo +-----------------------------------------+
echo ^| Services are running! ^|
echo ^| ^|
echo ^| Local: http://localhost:5173 ^|
if defined LAN_IP (
echo ^| Network: http://%LAN_IP%:5173 ^|
)
echo ^| ^|
echo ^| API: http://localhost:%BACKEND_PORT%/api/health ^|
echo ^| ^|
echo ^| Press any key to stop ... ^|
echo +-----------------------------------------+
echo.
pause >nul
REM -- 10. Cleanup: kill processes on both ports --
echo Stopping services ...
for /f "tokens=5" %%a in ('netstat -aon 2^>nul ^| findstr ":%BACKEND_PORT%" ^| findstr "LISTENING"') do (
taskkill /f /t /pid %%a >nul 2>&1
)
for /f "tokens=5" %%a in ('netstat -aon 2^>nul ^| findstr ":5173" ^| findstr "LISTENING"') do (
taskkill /f /t /pid %%a >nul 2>&1
)
echo Done.
pause