选号网构建默认使用npmmirror并增加npm重试
缓解国内服务器访问registry.npmjs.org出现ECONNRESET的问题。
This commit is contained in:
+21
-12
@@ -278,34 +278,43 @@ prepare_show_site() {
|
||||
fi
|
||||
log_warn "使用已有 show-dist(--no-build)"
|
||||
else
|
||||
# 服务器通常无本机 Node:优先本机 npm,否则用 Docker 内 node 镜像构建
|
||||
log "构建选号网静态资源:${show_dir}"
|
||||
# 服务器通常无本机 Node:优先本机 npm,否则用 Docker 内 node 镜像构建。
|
||||
# 国内机访问 registry.npmjs.org 易 ECONNRESET,默认走 npmmirror,可用 NPM_REGISTRY 覆盖。
|
||||
local npm_registry npm_install
|
||||
npm_registry="$(env_value NPM_REGISTRY || true)"
|
||||
if [[ -z "${npm_registry}" ]]; then
|
||||
npm_registry="${NPM_REGISTRY:-https://registry.npmmirror.com}"
|
||||
fi
|
||||
if [[ -f "${show_dir}/package-lock.json" ]]; then
|
||||
npm_install="npm ci"
|
||||
else
|
||||
npm_install="npm install"
|
||||
fi
|
||||
log "构建选号网静态资源:${show_dir}(registry=${npm_registry})"
|
||||
if command -v npm >/dev/null 2>&1; then
|
||||
(
|
||||
cd "${show_dir}"
|
||||
if [[ -f package-lock.json ]]; then
|
||||
npm ci
|
||||
else
|
||||
npm install
|
||||
fi
|
||||
${npm_install} \
|
||||
--registry="${npm_registry}" \
|
||||
--fetch-retries=5 \
|
||||
--fetch-retry-mintimeout=20000 \
|
||||
--fetch-retry-maxtimeout=120000
|
||||
npm run build
|
||||
)
|
||||
else
|
||||
log_warn "本机无 npm,使用 Docker 镜像 node:24-alpine 构建选号网(无需安装 Node)"
|
||||
need_cmd docker
|
||||
local npm_cmd="npm ci && npm run build"
|
||||
if [[ ! -f "${show_dir}/package-lock.json" ]]; then
|
||||
npm_cmd="npm install && npm run build"
|
||||
fi
|
||||
# 挂载源码目录;node_modules/dist 写在挂载卷上,与本机 npm 行为一致
|
||||
docker run --rm \
|
||||
-e npm_config_registry="${npm_registry}" \
|
||||
-v "${show_dir}:/app" \
|
||||
-w /app \
|
||||
node:24-alpine \
|
||||
sh -c "${npm_cmd}"
|
||||
sh -c "${npm_install} --registry=${npm_registry} --fetch-retries=5 --fetch-retry-mintimeout=20000 --fetch-retry-maxtimeout=120000 && npm run build"
|
||||
fi
|
||||
if [[ ! -d "${show_dir}/dist" ]]; then
|
||||
log_error "选号网构建失败:未生成 dist/"
|
||||
log_error "若仍网络失败,可在 backend/.env 设置 NPM_REGISTRY=https://registry.npmmirror.com 后重试"
|
||||
exit 1
|
||||
fi
|
||||
rm -rf "${show_dist}"
|
||||
|
||||
Reference in New Issue
Block a user