-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStart-BlockPilot-TW.bat
More file actions
153 lines (140 loc) · 6.26 KB
/
Copy pathStart-BlockPilot-TW.bat
File metadata and controls
153 lines (140 loc) · 6.26 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
@echo off
REM SPDX-License-Identifier: Apache-2.0
REM BlockPilot Minecraft Panel v1.0.5
REM 功能說明:檢查 npm、安裝相依套件、建置專案,並將 WEB 與 API 控制台分開顯示。
REM 維護原則:分離控制台的啟動流程若有調整,必須同步更新公開文件。
setlocal EnableExtensions EnableDelayedExpansion
chcp 65001 >nul
title BlockPilotMC WEB - 中文繁體
cd /d "%~dp0"
set "PANEL_PORT=8787"
call :PrintHeader
call :EnsureNpm
if errorlevel 1 goto :Failed
REM Install dependencies only when node_modules is missing or package-lock.json has changed.
set "NEED_INSTALL=0"
if not exist "node_modules\" set "NEED_INSTALL=1"
if not exist "package-lock.json" (
echo [錯誤] 找不到 package-lock.json,請確認原始碼已完整解壓縮。
goto :Failed
)
for /f "usebackq delims=" %%H in (`powershell -NoProfile -ExecutionPolicy Bypass -Command "(Get-FileHash -Algorithm SHA256 'package-lock.json').Hash"`) do set "CURRENT_LOCK_HASH=%%H"
if not defined CURRENT_LOCK_HASH (
echo [警告] 無法計算 package-lock.json 雜湊,將重新執行 npm ci。
set "NEED_INSTALL=1"
)
set "SAVED_LOCK_HASH="
if exist ".blockpilot-npm-lock.sha256" set /p SAVED_LOCK_HASH=<".blockpilot-npm-lock.sha256"
if /I not "!CURRENT_LOCK_HASH!"=="!SAVED_LOCK_HASH!" set "NEED_INSTALL=1"
REM Reinstall when a required runtime dependency is missing, even if the saved lock hash matches.
if "!NEED_INSTALL!"=="0" (
node -e "require.resolve('iconv-lite')" >nul 2>&1
if errorlevel 1 set "NEED_INSTALL=1"
)
if "!NEED_INSTALL!"=="1" (
echo [資訊] 正在準備專案相依套件...
powershell -NoProfile -ExecutionPolicy Bypass -File "scripts\windows\Install-Dependencies.ps1" -Locale "zh-TW"
if errorlevel 1 goto :Failed
if defined CURRENT_LOCK_HASH >".blockpilot-npm-lock.sha256" echo !CURRENT_LOCK_HASH!
) else (
echo [資訊] npm 與專案相依套件已就緒,略過安裝。
)
REM GitHub source archives do not include dist folders, so build the API and web interface before startup.
echo [資訊] 正在建置 API 與 WEB 介面...
call npm run build
if errorlevel 1 (
echo [錯誤] BlockPilotMC 建置失敗。
goto :Failed
)
call :ReadPanelPort
call :ApiIsReady
if not errorlevel 1 (
echo [資訊] 偵測到連接埠 !PANEL_PORT! 已有 BlockPilotMC API 正常運行。
goto :ApiReady
)
echo [資訊] 正在開啟獨立的 BlockPilotMC API 控制台...
call :StartApiConsole
if errorlevel 1 goto :Failed
echo [資訊] 正在等待 API 啟動完成...
call :WaitForApi
if errorlevel 1 (
echo [錯誤] API 未能在時間內啟動,請查看另一個 API 控制台的錯誤訊息。
goto :Failed
)
:ApiReady
set "PANEL_URL=http://127.0.0.1:!PANEL_PORT!"
echo.
echo [資訊] WEB 介面已就緒:
echo !PANEL_URL!
echo [資訊] API 訊息只會顯示在獨立的 API 控制台。
start "" "!PANEL_URL!" >nul 2>&1
echo [資訊] 啟動完成,WEB 啟動控制台將自動關閉;API 控制台會繼續運行。
REM Successful startup no longer leaves the WEB launcher console open.
REM EXIT (not EXIT /B) closes the launcher cmd.exe / Windows Terminal tab while
REM the independent API console keeps running.
endlocal
exit 0
:ReadPanelPort
REM Read PORT from .env without exposing any other environment values in the console.
for /f "usebackq delims=" %%P in (`powershell -NoProfile -ExecutionPolicy Bypass -Command "$p=8787; if(Test-Path '.env'){ $line=Get-Content '.env' ^| Where-Object { $_ -match '^\s*PORT\s*=' } ^| Select-Object -Last 1; if($line){ $candidate=($line -split '=',2)[1].Trim(); $number=0; if([int]::TryParse($candidate,[ref]$number) -and $number -ge 1 -and $number -le 65535){ $p=$number } } }; Write-Output $p"`) do set "PANEL_PORT=%%P"
exit /b 0
:ApiIsReady
powershell -NoProfile -ExecutionPolicy Bypass -Command "try { $response=Invoke-WebRequest -UseBasicParsing -Uri 'http://127.0.0.1:!PANEL_PORT!/api/health' -TimeoutSec 2; if($response.StatusCode -eq 200){ exit 0 } } catch {}; exit 1" >nul 2>&1
exit /b %ERRORLEVEL%
:StartApiConsole
if not exist "scripts\windows\Start-BlockPilot-API.cmd" (
echo [錯誤] 找不到 scripts\windows\Start-BlockPilot-API.cmd。
exit /b 1
)
REM The previous API health check intentionally returns ERRORLEVEL 1 when no API is running.
REM START can preserve that old value even after opening the console successfully, so clear it first.
"%ComSpec%" /d /c exit 0
call "%CD%\scripts\windows\Start-BlockPilot-API.cmd" zh-TW
if errorlevel 1 (
echo [錯誤] 無法開啟獨立 API 控制台。
exit /b 1
)
exit /b 0
:WaitForApi
powershell -NoProfile -ExecutionPolicy Bypass -Command "$url='http://127.0.0.1:!PANEL_PORT!/api/health'; for($i=0; $i -lt 45; $i++){ try { $response=Invoke-WebRequest -UseBasicParsing -Uri $url -TimeoutSec 2; if($response.StatusCode -eq 200){ exit 0 } } catch {}; Start-Sleep -Milliseconds 750 }; exit 1" >nul 2>&1
exit /b %ERRORLEVEL%
:EnsureNpm
REM 優先使用系統現有的 npm;找不到時,再透過 winget 安裝包含 npm 的 Node.js LTS。
where npm >nul 2>&1
if not errorlevel 1 (
for /f "usebackq delims=" %%V in (`npm --version`) do echo [資訊] 已偵測 npm %%V。
exit /b 0
)
echo [警告] 未偵測到 npm,將嘗試透過 winget 安裝 Node.js LTS(包含 npm)。
where winget >nul 2>&1
if errorlevel 1 (
echo [錯誤] 此電腦沒有 npm,也無法使用 winget 自動安裝 Node.js。
echo [資訊] 請先從 Node.js 官方網站安裝 Node.js 22 或更新版本,再重新執行本檔案。
exit /b 1
)
winget install --id OpenJS.NodeJS.LTS --exact --accept-package-agreements --accept-source-agreements
if errorlevel 1 (
echo [錯誤] Node.js LTS 自動安裝失敗。
exit /b 1
)
REM Add the common Node.js installation path to this console session so Windows does not need to be restarted.
if exist "%ProgramFiles%\nodejs\npm.cmd" set "PATH=%ProgramFiles%\nodejs;%PATH%"
where npm >nul 2>&1
if errorlevel 1 (
echo [錯誤] Node.js 已安裝,但目前控制台仍找不到 npm。
echo [資訊] 請關閉此視窗後重新執行 Start-BlockPilot-TW.bat。
exit /b 1
)
exit /b 0
:PrintHeader
echo ============================================================
echo BlockPilotMC v1.0.5
echo WEB 啟動控制台 - 中文繁體
echo ============================================================
echo.
exit /b 0
:Failed
echo.
echo [資訊] 請依照上方訊息檢查啟動問題。
pause
exit /b 1