-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstart-linux.sh
More file actions
63 lines (52 loc) · 2.7 KB
/
Copy pathstart-linux.sh
File metadata and controls
63 lines (52 loc) · 2.7 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
#!/usr/bin/env bash
# SPDX-License-Identifier: Apache-2.0
# 功能說明:在 Linux 上準備並啟動 BlockPilotMC。
# 維護原則:相依套件與建置只在需要時執行;正式 systemd 服務由安裝程式預先完成建置。
set -Eeuo pipefail
ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
cd "$ROOT"
info() { printf '\033[90m[%(%H:%M:%S)T]\033[0m\033[92m[INFO]\033[0m %s\n' -1 "$*"; }
warn() { printf '\033[90m[%(%H:%M:%S)T]\033[0m\033[93m[WARN]\033[0m %s\n' -1 "$*"; }
fail() { printf '\033[90m[%(%H:%M:%S)T]\033[0m\033[91m[ERROR]\033[0m %s\n' -1 "$*" >&2; exit 1; }
command -v node >/dev/null 2>&1 || fail "Node.js was not found. Install Node.js 22–26 first."
command -v npm >/dev/null 2>&1 || fail "npm was not found. Install npm 10 or later first."
NODE_MAJOR="$(node -p 'Number(process.versions.node.split(".")[0])')"
NPM_MAJOR="$(npm -v | cut -d. -f1)"
if (( NODE_MAJOR < 22 || NODE_MAJOR >= 27 )); then
fail "Unsupported Node.js version: $(node -v). BlockPilotMC requires Node.js 22–26."
fi
if [[ ! "$NPM_MAJOR" =~ ^[0-9]+$ ]] || (( NPM_MAJOR < 10 )); then
fail "Unsupported npm version: $(npm -v). BlockPilotMC requires npm 10 or later."
fi
# 使用單引號包住 JavaScript 表達式,避免 Bash 將反斜線原樣傳給 Node.js。
PLATFORM_ID="$(node -p 'process.platform + "-" + process.arch')"
PLATFORM_FILE="node_modules/.blockpilot-platform"
INSTALLED_PLATFORM="$(cat "$PLATFORM_FILE" 2>/dev/null || true)"
if [[ ! -d node_modules ]] || [[ ! -f node_modules/.package-lock.json ]] || [[ "$INSTALLED_PLATFORM" != "$PLATFORM_ID" ]]; then
warn "Installing dependencies for $PLATFORM_ID. This is required once after a clean checkout or platform change..."
rm -rf node_modules
npm ci
printf '%s\n' "$PLATFORM_ID" > "$PLATFORM_FILE"
elif ! node -e "require('better-sqlite3')" >/dev/null 2>&1; then
warn "Rebuilding native modules for $PLATFORM_ID..."
npm rebuild better-sqlite3 esbuild
fi
# 直接執行本檔時,若尚未建立 .env,替使用者產生安全的首次登入密碼。
if [[ ! -f .env ]]; then
warn "No .env file was found. Creating the initial administrator configuration..."
node scripts/setup-env.mjs
fi
needs_build=0
if [[ ! -f apps/api/dist/index.js ]] || [[ ! -f apps/web/dist/index.html ]] || [[ ! -f apps/web/dist/.blockpilot-build ]]; then
needs_build=1
elif find apps/api/src apps/web/src apps/web/index.html -type f -newer apps/web/dist/.blockpilot-build -print -quit 2>/dev/null | grep -q .; then
needs_build=1
fi
if (( needs_build )); then
warn "Building BlockPilotMC for production..."
npm run build
fi
mkdir -p runtime Server
chmod 700 runtime 2>/dev/null || true
info "Starting BlockPilotMC with $(node -v) on $(uname -s) ($PLATFORM_ID)..."
exec node app.js