-
Notifications
You must be signed in to change notification settings - Fork 424
Expand file tree
/
Copy pathlocal_dev.sh
More file actions
executable file
·383 lines (322 loc) · 12 KB
/
local_dev.sh
File metadata and controls
executable file
·383 lines (322 loc) · 12 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
#!/usr/bin/env bash
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
##############################################################
# Local development script for doris-website
#
# Usage:
# ./local_dev.sh [command] [options]
#
# Commands:
# start Start dev server (English, default)
# start-zh Start dev server (Chinese)
# build Full production build (en + zh-CN)
# build-en Production build (English only)
# build-docs-next Start docs-next dev server (hot reload, current only)
# serve Serve a previous production build
# install Install dependencies only
# clean Clean build artifacts and caches
# help Show this help message
#
# Options:
# --port PORT Dev server port (default: 3000)
# --host HOST Dev server host (default: localhost)
# --skip-install Skip yarn install step
# --versions LIST Comma-separated versions to build
# e.g. --versions "4.x" (faster builds)
# --max-mem MB Node.js max old space size in MB
# (default: 8192)
#
# Examples:
# ./local_dev.sh # start English dev server
# ./local_dev.sh start --port 8080 # start on port 8080
# ./local_dev.sh start-zh # start Chinese dev server
# ./local_dev.sh build # full build (slow)
# ./local_dev.sh build --versions "4.x" # build only 4.x version
# ./local_dev.sh build-docs-next # start docs-next dev server (hot reload)
# ./local_dev.sh clean # clean caches
##############################################################
set -euo pipefail
# ─── Resolve project root (directory of this script) ─────────
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="${SCRIPT_DIR}"
# ─── Colors ──────────────────────────────────────────────────
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
CYAN='\033[0;36m'
BOLD='\033[1m'
NC='\033[0m' # No Color
info() { echo -e "${BLUE}[INFO]${NC} $*"; }
ok() { echo -e "${GREEN}[OK]${NC} $*"; }
warn() { echo -e "${YELLOW}[WARN]${NC} $*"; }
error() { echo -e "${RED}[ERROR]${NC} $*"; }
step() { echo -e "\n${CYAN}${BOLD}==> $*${NC}"; }
# ─── Discover Node.js and Yarn (no global env modification) ──
setup_node_env() {
# Try common Node.js install locations (macOS + Linux)
local search_paths=(
"/usr/local/bin"
"/usr/bin"
"/opt/homebrew/bin"
"${HOME}/.nvm/versions/node/$(ls -1 ${HOME}/.nvm/versions/node/ 2>/dev/null | sort -V | tail -1)/bin"
"${HOME}/.volta/bin"
"${HOME}/.fnm/aliases/default/bin"
"${HOME}/.local/bin"
"/snap/node/current/bin"
)
# If node is already on PATH, no need to modify
if command -v node &>/dev/null && command -v yarn &>/dev/null; then
NODE_BIN="$(command -v node)"
YARN_BIN="$(command -v yarn)"
return 0
fi
# Search for node in common locations
local found_path=""
for p in "${search_paths[@]}"; do
if [[ -x "${p}/node" ]]; then
found_path="${p}"
break
fi
done
if [[ -z "${found_path}" ]]; then
error "Node.js not found! Please install Node.js >= 18."
error "Searched: ${search_paths[*]}"
echo ""
echo "Install options:"
echo " macOS: brew install node"
echo " Linux: curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash - && sudo apt-get install -y nodejs"
echo " or visit https://nodejs.org/"
exit 1
fi
# Prepend to PATH only within this script's process
export PATH="${found_path}:${PATH}"
NODE_BIN="${found_path}/node"
# Ensure yarn is available; install locally if needed
if [[ -x "${found_path}/yarn" ]]; then
YARN_BIN="${found_path}/yarn"
elif command -v yarn &>/dev/null; then
YARN_BIN="$(command -v yarn)"
else
warn "Yarn not found, installing via npm (local to this script)..."
"${found_path}/npm" install -g yarn 2>/dev/null || {
error "Failed to install yarn. Please install manually: npm install -g yarn"
exit 1
}
YARN_BIN="${found_path}/yarn"
fi
}
# ─── Validate environment ───────────────────────────────────
validate_env() {
step "Checking environment"
setup_node_env
local node_version
node_version="$("${NODE_BIN}" --version)"
local node_major="${node_version#v}"
node_major="${node_major%%.*}"
if (( node_major < 18 )); then
error "Node.js >= 18 is required, found ${node_version}"
exit 1
fi
local yarn_version
yarn_version="$("${YARN_BIN}" --version)"
ok "Node.js ${node_version} (${NODE_BIN})"
ok "Yarn v${yarn_version} (${YARN_BIN})"
ok "Project ${PROJECT_ROOT}"
}
# ─── Install dependencies ───────────────────────────────────
do_install() {
step "Installing dependencies"
cd "${PROJECT_ROOT}"
if [[ -d "node_modules" ]] && [[ -f "node_modules/.yarn-integrity" ]]; then
info "node_modules exists, running yarn to sync..."
else
info "Installing from scratch (this may take a few minutes)..."
fi
"${YARN_BIN}" install --frozen-lockfile 2>/dev/null || "${YARN_BIN}" install
ok "Dependencies installed"
}
# ─── Set DOCS_VERSIONS env var for selective version builds ──
# This uses the onlyIncludeVersions option in docusaurus.config.js
# instead of modifying versions.json (which is fragile).
apply_versions_env() {
local filter="$1"
if [[ -n "${filter}" ]]; then
export DOCS_VERSIONS="${filter}"
info "DOCS_VERSIONS=${filter} (only these versions will be built)"
else
info "DOCS_VERSIONS not set (all versions will be built)"
fi
}
# ─── Commands ────────────────────────────────────────────────
cmd_start() {
local port="${OPT_PORT}"
local host="${OPT_HOST}"
validate_env
if [[ "${OPT_SKIP_INSTALL}" != "true" ]]; then
do_install
fi
apply_versions_env "${OPT_VERSIONS}"
export NODE_OPTIONS="--max-old-space-size=${OPT_MAX_MEM}"
step "Starting dev server (English) on ${host}:${port}"
info "Press Ctrl+C to stop"
echo ""
cd "${PROJECT_ROOT}"
"${YARN_BIN}" docusaurus start --no-open --host "${host}" --port "${port}"
}
cmd_start_zh() {
local port="${OPT_PORT}"
local host="${OPT_HOST}"
validate_env
if [[ "${OPT_SKIP_INSTALL}" != "true" ]]; then
do_install
fi
apply_versions_env "${OPT_VERSIONS}"
export NODE_OPTIONS="--max-old-space-size=${OPT_MAX_MEM}"
step "Starting dev server (Chinese) on ${host}:${port}"
info "Press Ctrl+C to stop"
echo ""
cd "${PROJECT_ROOT}"
"${YARN_BIN}" docusaurus start --no-open --locale zh-CN --host "${host}" --port "${port}"
}
cmd_build() {
local locales="${1:-en}"
validate_env
if [[ "${OPT_SKIP_INSTALL}" != "true" ]]; then
do_install
fi
apply_versions_env "${OPT_VERSIONS}"
step "Building site (locales: ${locales})"
info "NODE_OPTIONS=--max-old-space-size=${OPT_MAX_MEM}"
info "This may take 10-30 minutes for full builds..."
echo ""
cd "${PROJECT_ROOT}"
export NODE_OPTIONS="--max-old-space-size=${OPT_MAX_MEM}"
# Build locale arguments
local locale_args=""
for locale in ${locales}; do
locale_args+=" --locale ${locale}"
done
"${YARN_BIN}" docusaurus build ${locale_args}
ok "Build completed! Output in: ${PROJECT_ROOT}/build/"
info "Run './local_dev.sh serve' to preview the build."
}
cmd_build_docs_next_only() {
local port="${OPT_PORT}"
local host="${OPT_HOST}"
validate_env
if [[ "${OPT_SKIP_INSTALL}" != "true" ]]; then
do_install
fi
apply_versions_env "current"
export NODE_OPTIONS="--max-old-space-size=${OPT_MAX_MEM}"
step "Starting docs-next dev server on ${host}:${port}"
info "Navigate to http://${host}:${port}/docs-next/dev/"
info "Press Ctrl+C to stop"
echo ""
cd "${PROJECT_ROOT}"
"${YARN_BIN}" docusaurus start --no-open --locale zh-CN --host "${host}" --port "${port}"
}
cmd_serve() {
validate_env
if [[ ! -d "${PROJECT_ROOT}/build" ]]; then
error "No build directory found. Run './local_dev.sh build' first."
exit 1
fi
local port="${OPT_PORT}"
local host="${OPT_HOST}"
step "Serving build on ${host}:${port}"
info "Press Ctrl+C to stop"
echo ""
cd "${PROJECT_ROOT}"
"${YARN_BIN}" docusaurus serve --host "${host}" --port "${port}"
}
cmd_clean() {
step "Cleaning build artifacts and caches"
cd "${PROJECT_ROOT}"
local dirs_to_clean=("build" ".docusaurus" "node_modules/.cache")
for d in "${dirs_to_clean[@]}"; do
if [[ -d "${d}" ]]; then
info "Removing ${d}/"
rm -rf "${d}"
fi
done
ok "Clean completed"
info "Run './local_dev.sh install' to reinstall dependencies if needed."
}
cmd_install() {
validate_env
do_install
}
cmd_help() {
# Print the header comments of this script
sed -n '/^##*$/,/^##*$/p' "${BASH_SOURCE[0]}" | head -40
echo ""
echo -e "${BOLD}Quick start:${NC}"
echo " ./local_dev.sh # Start English dev server"
echo " ./local_dev.sh start-zh # Start Chinese dev server"
echo " ./local_dev.sh build # Full build (en only, default)"
echo " ./local_dev.sh build-all # Full build (en + zh-CN)"
echo " ./local_dev.sh build-docs-next # docs-next dev server (hot reload)"
echo ""
}
# ─── Parse arguments ─────────────────────────────────────────
COMMAND="${1:-start}"
shift 2>/dev/null || true
OPT_PORT="3000"
OPT_HOST="localhost"
OPT_SKIP_INSTALL="false"
OPT_VERSIONS="current"
OPT_MAX_MEM="2048"
while [[ $# -gt 0 ]]; do
case "$1" in
--port)
OPT_PORT="$2"; shift 2 ;;
--host)
OPT_HOST="$2"; shift 2 ;;
--skip-install)
OPT_SKIP_INSTALL="true"; shift ;;
--versions)
OPT_VERSIONS="$2"; shift 2 ;;
--max-mem)
OPT_MAX_MEM="$2"; shift 2 ;;
-h|--help)
COMMAND="help"; shift ;;
*)
error "Unknown option: $1"
cmd_help
exit 1 ;;
esac
done
# ─── Dispatch command ────────────────────────────────────────
case "${COMMAND}" in
start) cmd_start ;;
start-zh) cmd_start_zh ;;
build) cmd_build "en" ;;
build-all) cmd_build "en zh-CN" ;;
build-en) cmd_build "en" ;;
build-docs-next) cmd_build_docs_next_only ;;
serve) cmd_serve ;;
install) cmd_install ;;
clean) cmd_clean ;;
help|-h|--help) cmd_help ;;
*)
error "Unknown command: ${COMMAND}"
cmd_help
exit 1 ;;
esac