diff --git a/scripts/deploy-ubuntu.sh b/scripts/deploy-ubuntu.sh index 0c6246b..cebccba 100755 --- a/scripts/deploy-ubuntu.sh +++ b/scripts/deploy-ubuntu.sh @@ -80,6 +80,23 @@ require_command() { fi } +resolve_npm_bin() { + if [[ -n "${CLAW_NODE_BIN_DIR:-}" ]]; then + export PATH="${CLAW_NODE_BIN_DIR}:${PATH}" + fi + local npm_bin="${CLAW_NPM_BIN:-$(command -v npm || true)}" + if [[ -z "${npm_bin}" || ! -x "${npm_bin}" ]]; then + fail "缺少命令:npm。请先安装 Node.js 和 npm,建议 Node.js 20 或 22。" + fi + local node_bin_dir + node_bin_dir="$(dirname "${npm_bin}")" + export PATH="${node_bin_dir}:${PATH}" + if ! "${npm_bin}" --version >/dev/null 2>&1; then + fail "npm 不可用:${npm_bin}。请确认同目录下存在 node,或重新安装 Node.js/npm。" + fi + printf "%s\n" "${npm_bin}" +} + bootstrap_system_packages() { if [[ "${BOOTSTRAP_SYSTEM}" != "1" && -f "${ENV_FILE}" ]]; then return @@ -222,25 +239,27 @@ ensure_python() { } build_frontend() { - require_command npm "请先安装 Node.js 和 npm,建议 Node.js 20 或 22。" + local npm_bin + npm_bin="$(resolve_npm_bin)" + export PATH="$(dirname "${npm_bin}"):${PATH}" log "安装并构建前端" cd "${ROOT_DIR}/frontend/app" if [[ -f package-lock.json ]]; then - npm ci + "${npm_bin}" ci else - npm install + "${npm_bin}" install fi - npm run build + "${npm_bin}" run build } remember_node_runtime() { local npm_bin node_bin node_bin_dir tmp_file - npm_bin="$(command -v npm || true)" - node_bin="$(command -v node || true)" - [[ -n "${npm_bin}" ]] || fail "无法定位 npm。" + npm_bin="$(resolve_npm_bin)" node_bin_dir="$(dirname "${npm_bin}")" + export PATH="${node_bin_dir}:${PATH}" + node_bin="$(PATH="${node_bin_dir}:${PATH}" command -v node || true)" tmp_file="$(mktemp)" - grep -v -E '^(export )?CLAW_(NPM_BIN|NODE_BIN|NODE_BIN_DIR)=' "${ENV_FILE}" >"${tmp_file}" || true + grep -v -E '^(# systemd 用户服务不会读取交互式 shell 配置|export CLAW_(NPM_BIN|NODE_BIN|NODE_BIN_DIR)=)' "${ENV_FILE}" >"${tmp_file}" || true cat >>"${tmp_file}" </dev/null 2>&1; then + fail "npm 不可用:${npm_bin}。请确认同目录下存在 node,或重新安装 Node.js/npm。" + fi + printf "%s\n" "${npm_bin}" +} + cd "${ROOT_DIR}" [[ -d .git ]] || fail "${ROOT_DIR} 不是 git 仓库。" @@ -38,7 +60,9 @@ git pull --ff-only origin "${BRANCH}" log "构建前端" cd "${ROOT_DIR}/frontend/app" -npm run build +npm_bin="$(resolve_npm_bin)" +export PATH="$(dirname "${npm_bin}"):${PATH}" +"${npm_bin}" run build log "重启用户服务" systemctl --user restart "${BACKEND_SERVICE}" "${FRONTEND_SERVICE}"