Files
perler-lab/start-dev.bat

122 lines
4.8 KiB
Batchfile

@echo off
title PERLER LAB Dev Environment
setlocal
rem ============================================================
rem PERLER LAB one-click development startup script
rem 1. Check Node.js / npm environment
rem 2. Start backend (perler-lab-server, port 8788)
rem 3. Start frontend (perler-lab, Vite port 5173)
rem 4. Print access URLs and health check command
rem
rem NOTE (BUG-087): this file is intentionally pure ASCII and
rem resolves the project directory via %~dp0, so it runs correctly
rem under any console codepage (including GBK/cp936) without
rem relying on chcp or UTF-8 BOM handling.
rem ============================================================
echo ==========================================
echo PERLER LAB Dev Environment is starting...
echo ==========================================
echo.
rem ---------- 1. Check Node.js / npm availability ----------
where node >nul 2>nul
if errorlevel 1 (
echo [ERROR] Node.js not found. Please install Node.js 20 or later.
echo Download: https://nodejs.org/
echo Re-run this script after installation.
pause
exit /b 1
)
where npm >nul 2>nul
if errorlevel 1 (
echo [ERROR] npm not found. Please check your Node.js installation.
pause
exit /b 1
)
for /f "delims=" %%V in ('node --version') do set "NODE_VERSION=%%V"
echo [INFO] Node.js version: %NODE_VERSION%
rem ---------- 2. Check port usage (warning only, non-blocking) ----------
netstat -ano | findstr ":8788 " | findstr "LISTENING" >nul 2>nul
if not errorlevel 1 (
echo [WARNING] Port 8788 is already in use! The backend may already be running.
echo Run stop-dev.bat first to stop old processes.
)
netstat -ano | findstr ":5173 " | findstr "LISTENING" >nul 2>nul
if not errorlevel 1 (
echo [WARNING] Port 5173 is already in use! The frontend may already be running.
echo Run stop-dev.bat first to stop old processes.
)
echo.
rem ---------- 2.5 DB: SSH tunnel to test-env PostgreSQL, fallback SQLite ----------
rem Preferred: tunnel localhost:15432 -> test machine loopback 15432 ->
rem perler-lab-pg container 5432. Local dev then shares the test-env database
rem (same accounts/data as test.beads.520mxp.cn).
rem Fallback: if ssh is unavailable / test machine unreachable, use a local
rem SQLite file DB (perler-lab-server/data/perler-lab.db) via DB_DRIVER=sqlite.
set "DATABASE_URL="
set "DB_DRIVER=sqlite"
set "TUNNEL_UP="
rem Reuse an already-running tunnel (port 15432 listening)
netstat -ano | findstr ":15432 " | findstr "LISTENING" >nul 2>nul
if not errorlevel 1 set "TUNNEL_UP=1"
if not defined TUNNEL_UP (
where ssh >nul 2>nul
if not errorlevel 1 (
echo [START] Opening SSH tunnel to test-env PostgreSQL (localhost:15432)...
start "PERLER LAB DB Tunnel" /min cmd /c "ssh -N -L 15432:127.0.0.1:15432 -o BatchMode=yes -o StrictHostKeyChecking=accept-new -o ServerAliveInterval=30 test"
rem Wait for the tunnel to come up (6s)
ping -n 7 127.0.0.1 >nul
netstat -ano | findstr ":15432 " | findstr "LISTENING" >nul 2>nul
if not errorlevel 1 set "TUNNEL_UP=1"
)
)
if defined TUNNEL_UP (
echo [INFO] DB = test-env PostgreSQL via tunnel (localhost:15432/perler_lab)
set "DATABASE_URL=postgres://perler:7c5608fe52a5223a6c6107b6@127.0.0.1:15432/perler_lab"
set "DB_DRIVER="
) else (
echo [WARN] Tunnel unavailable - DB = local SQLite file (data/perler-lab.db)
)
echo.
rem ---------- 3. Start backend (separate minimized window) ----------
rem DB comes from section 2.5: DATABASE_URL (tunnel) or DB_DRIVER=sqlite (fallback).
echo [START] Starting backend service http://localhost:8788 ...
start "PERLER LAB Backend" /min cmd /c "cd /d ""%~dp0perler-lab-server"" && npx tsx src/server.ts"
rem ---------- 4. Wait 3 seconds for the backend to boot ----------
rem ping is used instead of timeout because timeout fails when stdin is
rem redirected (e.g. when run via "cmd /c ... < nul" in scripts/CI).
ping -n 4 127.0.0.1 >nul
rem ---------- 5. Start frontend (separate minimized window) ----------
echo [START] Starting frontend dev server http://localhost:5173 ...
start "PERLER LAB Frontend" /min cmd /c "cd /d ""%~dp0perler-lab"" && npm run dev"
rem ---------- 6. Print access info ----------
echo.
echo ==========================================
echo PERLER LAB Dev Environment is running!
echo ------------------------------------------
echo Frontend: http://localhost:5173
echo Backend: http://localhost:8788
echo Health: curl http://localhost:8788/health
echo ------------------------------------------
echo Note: backend/frontend run in separate minimized windows;
echo check logs by clicking the corresponding window title.
echo Stop services by running stop-dev.bat
echo ==========================================
echo.
rem Keep the window open when double-clicked
pause