#!/usr/bin/env bash
# 发布到公司 Nexus npm 私仓（凭据经环境变量传入，绝不落盘入库）
# 用法: NPM_USER=xxx NPM_PASS=xxx ./scripts/publish-nexus.sh
# 前置: 账号需有 npm-hosted 的部署权限（nx-repository-view-npm-npm-hosted-add/edit）
set -euo pipefail
REGISTRY="${NPM_REGISTRY:?需要 NPM_REGISTRY（如 https://nexus.example.com/repository/npm-hosted/）}"
: "${NPM_USER:?需要 NPM_USER}"; : "${NPM_PASS:?需要 NPM_PASS}"
KEY="$(echo "$REGISTRY" | sed 's|https\?://||; s|/$|/:|; s|^|//|')"
AUTH="$(printf '%s:%s' "$NPM_USER" "$NPM_PASS" | base64)"
CFG="$(mktemp)"; trap 'rm -f "$CFG"' EXIT
printf '%s_auth=%s\n' "$KEY" "$AUTH" > "$CFG"
echo "→ 目标: $REGISTRY ($(NPM_CONFIG_USERCONFIG="$CFG" npm whoami --registry="$REGISTRY" 2>/dev/null || echo '认证待验证'))"
NPM_CONFIG_USERCONFIG="$CFG" npm publish --registry="$REGISTRY"
echo "✅ 已发布。安装侧（npm-group 聚合 hosted+代理，依赖可一并解析）:"
echo "   npx --registry=$REGISTRY aims-workstation"
