Private
Public Access
1
0

refactor: Claude Code 安装方式从官方原生脚本改为 npm install
Some checks failed
Deploy to Cloudflare Pages / deploy (push) Failing after 11s

国内用户无法访问 claude.ai/install.sh,统一三个工具的安装路径:
- 移除 setup.sh/ps1 中 Claude Code 的特殊安装分支
- ClaudeCode 独立脚本添加 Node.js/fnm 检测安装函数链
- 三个工具均使用 npm install -g 安装 (需要 Node.js >= 20)
- 更新 CLAUDE.md 和文档中的安装说明
This commit is contained in:
2026-02-28 19:38:00 +08:00
parent f1ab858212
commit 34c950057c
8 changed files with 521 additions and 115 deletions

View File

@@ -31,6 +31,7 @@ $ClaudeConfigDir = "$env:USERPROFILE\.claude"
$ClaudeSettingsFile = "$ClaudeConfigDir\settings.json"
$ToolCommand = "claude"
$ToolName = "Claude Code"
$ToolPackage = "@anthropic-ai/claude-code"
# ========== 工具函数 ==========
function Test-Command {
@@ -80,14 +81,138 @@ function Write-Error {
Write-Host " $Message"
}
function Install-Tool {
Write-Info "使用官方安装脚本安装 $ToolName..."
Write-Host " 执行: irm https://claude.ai/install.ps1 | iex" -ForegroundColor Gray
# ========== Node.js 环境检测 ==========
function Get-NodeVersion {
if (Test-Command "node") {
try {
$versionStr = (& node --version 2>$null) -replace 'v', ''
$major = [int]($versionStr -split '\.')[0]
return @{ Version = $versionStr; Major = $major }
}
catch { }
}
return $null
}
function Install-Fnm {
Write-Host ""
Write-Info "正在安装 fnm (Fast Node Manager)..."
try {
Invoke-Expression "& { $(Invoke-RestMethod https://claude.ai/install.ps1) }"
if (Test-Command "winget") {
Write-Info "使用 winget 安装 fnm..."
& winget install Schniz.fnm -e --accept-source-agreements --accept-package-agreements
}
else {
Write-Info "使用 PowerShell 脚本安装 fnm..."
Invoke-Expression "& { $(Invoke-RestMethod https://fnm.vercel.app/install.ps1) }"
}
Refresh-Path
if (Test-Command "fnm") {
Write-Success "fnm 安装成功!"
$fnmEnv = & fnm env --use-on-cd 2>$null
if ($fnmEnv) {
$fnmEnv | Out-String | Invoke-Expression
}
return $true
}
else {
Write-Warning "fnm 可能已安装,但需要重新打开终端才能生效"
Write-Info "请重新打开 PowerShell 后再运行此脚本"
return $false
}
}
catch {
Write-Error "fnm 安装失败: $($_.Exception.Message)"
return $false
}
}
function Install-NodeWithFnm {
Write-Info "使用 fnm 安装 Node.js 24.x..."
try {
& fnm install 24
& fnm use 24
& fnm default 24
Refresh-Path
if (Test-Command "node") {
$nodeInfo = Get-NodeVersion
Write-Success "Node.js v$($nodeInfo.Version) 安装成功!"
return $true
}
else {
Write-Warning "Node.js 可能已安装,但需要重新打开终端才能生效"
return $false
}
}
catch {
Write-Error "Node.js 安装失败: $($_.Exception.Message)"
return $false
}
}
function Ensure-NodeEnvironment {
$nodeInfo = Get-NodeVersion
if ($nodeInfo) {
Write-Info "检测到 Node.js v$($nodeInfo.Version)"
if ($nodeInfo.Major -lt 20) {
Write-Warning "Node.js 版本过低 (需要 >= 20.x)"
$upgrade = Read-Host "是否使用 fnm 安装 Node.js 24.x? (Y/n)"
if ($upgrade -eq "n" -or $upgrade -eq "N") {
Write-Error "Node.js 版本不满足要求,请手动升级后重试"
return $false
}
if (-not (Test-Command "fnm")) {
if (-not (Install-Fnm)) { return $false }
}
return Install-NodeWithFnm
}
return $true
}
Write-Warning "未检测到 Node.js"
Write-Info "将使用 fnm 安装 Node.js 24.x"
$install = Read-Host "是否继续? (Y/n)"
if ($install -eq "n" -or $install -eq "N") {
return $false
}
if (-not (Test-Command "fnm")) {
if (-not (Install-Fnm)) { return $false }
}
return Install-NodeWithFnm
}
function Install-Tool {
if (-not (Ensure-NodeEnvironment)) {
return $false
}
Write-Info "使用 npm 安装 $ToolName..."
Write-Host " 执行: npm install -g $ToolPackage" -ForegroundColor Gray
try {
Invoke-Expression "npm install -g $ToolPackage"
$exitCode = $LASTEXITCODE
Refresh-Path
if ($exitCode -ne 0) {
Write-Error "安装命令返回错误码: $exitCode"
return $false
}
if (Test-Command $ToolCommand) {
Write-Success "$ToolName 安装成功!"
return $true