Private
Public Access
1
0

fix: replace claude setup symlinks with files

This commit is contained in:
2025-12-26 15:32:05 +08:00
parent 096437cc8d
commit d8440a1ccf
11 changed files with 1931 additions and 203 deletions

200
setup.sh
View File

@@ -5,8 +5,7 @@
set -e
# 确保 PATH 包含常见的包管理器路径
export PATH="$HOME/.bun/bin:$HOME/.local/bin:$HOME/.npm-global/bin:/usr/local/bin:$PATH"
export BUN_INSTALL="$HOME/.bun"
export PATH="$HOME/.local/share/fnm:$HOME/.fnm:$HOME/.local/bin:$HOME/.npm-global/bin:/usr/local/bin:$PATH"
# ========== 颜色定义 ==========
RED='\033[0;31m'
@@ -45,12 +44,15 @@ reload_env() {
rc_file=$(get_shell_rc)
# 重新加载 PATH
export PATH="$HOME/.bun/bin:$HOME/.local/bin:$HOME/.npm-global/bin:/usr/local/bin:$PATH"
export BUN_INSTALL="$HOME/.bun"
export PATH="$HOME/.local/share/fnm:$HOME/.fnm:$HOME/.local/bin:$HOME/.npm-global/bin:/usr/local/bin:$PATH"
# 加载 fnm 环境(如果存在)
if command_exists fnm; then
eval "$(fnm env)" 2>/dev/null || true
fi
# 从配置文件中提取并加载环境变量
if [ -f "$rc_file" ]; then
# 提取 export 行并执行
while IFS= read -r line; do
if [[ "$line" =~ ^export\ ([A-Za-z_][A-Za-z0-9_]*)= ]]; then
eval "$line" 2>/dev/null || true
@@ -163,7 +165,7 @@ check_system_deps() {
missing_deps+=("curl")
fi
# 检测 unzip (bun 安装需要)
# 检测 unzip (fnm 安装需要)
if ! command_exists unzip; then
missing_deps+=("unzip")
fi
@@ -246,88 +248,128 @@ command_exists() {
command -v "$1" &> /dev/null
}
# ========== 获取包管理器 ==========
get_package_manager() {
# ========== Node.js 版本检测 ==========
get_node_version() {
if command_exists node; then
local node_version=$(node --version 2>/dev/null | sed 's/v//')
info "检测到 Node.js v$node_version" >&2
echo "npm"
return 0
node --version 2>/dev/null | sed 's/v//'
fi
if command_exists bun; then
local bun_version=$(bun --version 2>/dev/null)
info "检测到 Bun v$bun_version" >&2
echo "bun"
return 0
fi
return 1
}
# ========== 安装 Bun ==========
install_bun() {
get_node_major_version() {
local version
version=$(get_node_version)
if [ -n "$version" ]; then
echo "$version" | cut -d. -f1
fi
}
# ========== 安装 fnm ==========
install_fnm() {
echo ""
warning "未检测到 Node.js 或 Bun"
info "推荐安装 Bun轻量级 JavaScript 运行时)"
echo ""
echo -e " 安装命令: ${CYAN}curl -fsSL https://bun.sh/install | bash${NC}"
info "正在安装 fnm (Fast Node Manager)..."
if curl -fsSL https://fnm.vercel.app/install | bash; then
export PATH="$HOME/.local/share/fnm:$PATH"
if [ -f "$HOME/.local/share/fnm/fnm" ]; then
eval "$($HOME/.local/share/fnm/fnm env)" 2>/dev/null || true
fi
if command_exists fnm; then
success "fnm 安装成功!"
return 0
else
warning "fnm 可能已安装,但需要重新打开终端才能生效"
info "请重新打开终端后再运行此脚本"
return 1
fi
else
error "fnm 安装失败"
return 1
fi
}
# ========== 使用 fnm 安装 Node.js ==========
install_node_with_fnm() {
info "使用 fnm 安装 Node.js 24.x..."
if fnm install 24 && fnm use 24 && fnm default 24; then
eval "$(fnm env)" 2>/dev/null || true
if command_exists node; then
local version
version=$(get_node_version)
success "Node.js v$version 安装成功!"
return 0
else
warning "Node.js 可能已安装,但需要重新打开终端才能生效"
return 1
fi
else
error "Node.js 安装失败"
return 1
fi
}
# ========== 确保 Node.js 环境就绪 ==========
ensure_node_environment() {
local version major
version=$(get_node_version)
if [ -n "$version" ]; then
major=$(get_node_major_version)
info "检测到 Node.js v$version"
if [ "$major" -lt 20 ]; then
warning "Node.js 版本过低 (需要 >= 20.x)"
echo ""
read -p "是否使用 fnm 安装 Node.js 24.x? (Y/n) " -n 1 -r
echo ""
if [[ $REPLY =~ ^[Nn]$ ]]; then
error "Node.js 版本不满足要求,请手动升级后重试"
return 1
fi
if ! command_exists fnm; then
install_fnm || return 1
fi
install_node_with_fnm || return 1
fi
return 0
fi
warning "未检测到 Node.js"
info "将使用 fnm 安装 Node.js 24.x"
echo ""
read -p "是否立即安装 Bun? (Y/n) " -n 1 -r
read -p "是否继续? (Y/n) " -n 1 -r
echo ""
if [[ $REPLY =~ ^[Nn]$ ]]; then
return 1
fi
info "正在安装 Bun..."
if curl -fsSL https://bun.sh/install | bash; then
# 刷新 PATH
export BUN_INSTALL="$HOME/.bun"
export PATH="$BUN_INSTALL/bin:$PATH"
if command_exists bun; then
success "Bun 安装成功!"
return 0
else
warning "Bun 可能已安装,但需要重新打开终端才能生效"
info "请重新打开终端后再运行此脚本"
return 1
fi
else
error "Bun 安装失败"
return 1
if ! command_exists fnm; then
install_fnm || return 1
fi
install_node_with_fnm || return 1
}
# ========== 安装工具 ==========
install_tool() {
local tool_num=$1
local pm=$2
local tool_name=$(get_tool_name "$tool_num")
local tool_pkg=$(get_tool_pkg "$tool_num")
info "安装 $tool_name..."
local install_cmd=""
if [[ "$pm" == "npm" ]]; then
install_cmd="npm install -g $tool_pkg"
else
install_cmd="bun add -g $tool_pkg"
fi
local install_cmd="npm install -g $tool_pkg"
echo -e " 执行: ${CYAN}$install_cmd${NC}"
if eval "$install_cmd"; then
hash -r 2>/dev/null || true
# 如果使用 bun 安装,创建 node 符号链接(如果不存在)
if [[ "$pm" == "bun" ]] && ! command_exists node; then
if [[ -f "$HOME/.bun/bin/bun" ]]; then
ln -sf "$HOME/.bun/bin/bun" "$HOME/.bun/bin/node" 2>/dev/null || true
info "已创建 node 符号链接 (bun -> node)"
fi
fi
success "$tool_name 安装成功!"
return 0
else
@@ -364,6 +406,26 @@ show_status() {
echo -e "获取 API 密钥: ${CYAN}https://api2.xcodecli.com/console/token${NC}"
echo -e "步骤: 添加令牌 → 输入令牌名称 → 提交"
echo ""
# 显示 Node.js 状态
local version major
version=$(get_node_version)
if [ -n "$version" ]; then
major=$(get_node_major_version)
local status color
if [ "$major" -ge 20 ]; then
status="[OK]"
color="${GREEN}"
else
status="[需升级]"
color="${YELLOW}"
fi
echo -e " Node.js: v$version ${color}${status}${NC}"
else
echo -e " Node.js: ${RED}[未安装]${NC}"
fi
echo ""
echo -e "${YELLOW}工具安装状态:${NC}"
echo ""
@@ -452,17 +514,9 @@ main() {
info "API 密钥: ${api_key:0:8}..."
echo ""
# 获取包管理器
local pm=$(get_package_manager)
if [[ -z "$pm" ]]; then
if ! install_bun; then
exit 1
fi
pm=$(get_package_manager)
if [[ -z "$pm" ]]; then
error "仍未检测到可用的包管理器"
exit 1
fi
# 确保 Node.js 环境就绪
if ! ensure_node_environment; then
exit 1
fi
echo ""
@@ -487,7 +541,7 @@ main() {
# 检测是否已安装
if ! command_exists "$tool_cmd"; then
if ! install_tool "$i" "$pm"; then
if ! install_tool "$i"; then
failed=$((failed + 1))
continue
fi