From fcb8c5e45ea1c6b84df6ebf775d4587ab7a50b0f Mon Sep 17 00:00:00 2001 From: sususu Date: Fri, 20 Mar 2026 23:25:49 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20winget=20=E5=AE=89=E8=A3=85=20fnm=20?= =?UTF-8?q?=E5=90=8E=E6=99=BA=E8=83=BD=E6=90=9C=E7=B4=A2=20PATH=20?= =?UTF-8?q?=E9=81=BF=E5=85=8D=E9=87=8D=E5=90=AF=E7=BB=88=E7=AB=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit winget 安装 fnm 后 PATH 可能未立即生效,新增 Find-FnmPath 在 WinGet Links/Packages、.fnm、LOCALAPPDATA 等常见位置搜索 fnm.exe 并自动加入当前会话 PATH,无需重启终端。 --- ClaudeCode/setup-claude-code.ps1 | 32 +++++++++++++++++++++++++++-- GeminiCLI/setup-gemini.ps1 | 31 ++++++++++++++++++++++++++-- codex/setup-codex.ps1 | 31 ++++++++++++++++++++++++++-- setup-claude-code.ps1 | 31 ++++++++++++++++++++++++++-- setup.ps1 | 35 +++++++++++++++++++++++++++++--- 5 files changed, 149 insertions(+), 11 deletions(-) diff --git a/ClaudeCode/setup-claude-code.ps1 b/ClaudeCode/setup-claude-code.ps1 index bd08295..0124589 100644 --- a/ClaudeCode/setup-claude-code.ps1 +++ b/ClaudeCode/setup-claude-code.ps1 @@ -94,6 +94,25 @@ function Get-NodeVersion { return $null } +# ========== fnm PATH 搜索 ========== +function Find-FnmPath { + $searchPaths = @( + "$env:LOCALAPPDATA\Microsoft\WinGet\Links", + "$env:LOCALAPPDATA\Microsoft\WinGet\Packages\Schniz.fnm_*", + "$env:USERPROFILE\.fnm", + "$env:LOCALAPPDATA\fnm", + "$env:ProgramFiles\fnm" + ) + foreach ($pattern in $searchPaths) { + $resolved = Resolve-Path $pattern -ErrorAction SilentlyContinue + foreach ($dir in $resolved) { + $fnmExe = Get-ChildItem -Path $dir.Path -Filter "fnm.exe" -Recurse -Depth 2 -ErrorAction SilentlyContinue | Select-Object -First 1 + if ($fnmExe) { return $fnmExe.DirectoryName } + } + } + return $null +} + function Install-Fnm { Write-Host "" Write-Info "正在安装 fnm (Fast Node Manager)..." @@ -110,6 +129,16 @@ function Install-Fnm { Refresh-Path + # 如果 fnm 不在 PATH 中,搜索常见安装位置 + if (-not (Test-Command "fnm")) { + Write-Info "正在搜索 fnm 安装位置..." + $fnmDir = Find-FnmPath + if ($fnmDir) { + Write-Info "在 $fnmDir 找到 fnm" + $env:Path = "$fnmDir;$env:Path" + } + } + if (Test-Command "fnm") { Write-Success "fnm 安装成功!" $fnmEnv = & fnm env --use-on-cd 2>$null @@ -119,8 +148,7 @@ function Install-Fnm { return $true } else { - Write-Warning "fnm 可能已安装,但需要重新打开终端才能生效" - Write-Info "请重新打开 PowerShell 后再运行此脚本" + Write-Warning "fnm 安装后未能在 PATH 中找到,请重新打开终端后再运行此脚本" return $false } } diff --git a/GeminiCLI/setup-gemini.ps1 b/GeminiCLI/setup-gemini.ps1 index 92cd7bf..69f61e9 100644 --- a/GeminiCLI/setup-gemini.ps1 +++ b/GeminiCLI/setup-gemini.ps1 @@ -92,6 +92,25 @@ function Get-NodeVersion { return $null } +# ========== fnm PATH 搜索 ========== +function Find-FnmPath { + $searchPaths = @( + "$env:LOCALAPPDATA\Microsoft\WinGet\Links", + "$env:LOCALAPPDATA\Microsoft\WinGet\Packages\Schniz.fnm_*", + "$env:USERPROFILE\.fnm", + "$env:LOCALAPPDATA\fnm", + "$env:ProgramFiles\fnm" + ) + foreach ($pattern in $searchPaths) { + $resolved = Resolve-Path $pattern -ErrorAction SilentlyContinue + foreach ($dir in $resolved) { + $fnmExe = Get-ChildItem -Path $dir.Path -Filter "fnm.exe" -Recurse -Depth 2 -ErrorAction SilentlyContinue | Select-Object -First 1 + if ($fnmExe) { return $fnmExe.DirectoryName } + } + } + return $null +} + function Install-Fnm { Write-Host "" Write-Info "正在安装 fnm (Fast Node Manager)..." @@ -108,6 +127,15 @@ function Install-Fnm { Refresh-Path + if (-not (Test-Command "fnm")) { + Write-Info "正在搜索 fnm 安装位置..." + $fnmDir = Find-FnmPath + if ($fnmDir) { + Write-Info "在 $fnmDir 找到 fnm" + $env:Path = "$fnmDir;$env:Path" + } + } + if (Test-Command "fnm") { Write-Success "fnm 安装成功!" $fnmEnv = & fnm env --use-on-cd 2>$null @@ -117,8 +145,7 @@ function Install-Fnm { return $true } else { - Write-Warning "fnm 可能已安装,但需要重新打开终端才能生效" - Write-Info "请重新打开 PowerShell 后再运行此脚本" + Write-Warning "fnm 安装后未能在 PATH 中找到,请重新打开终端后再运行此脚本" return $false } } diff --git a/codex/setup-codex.ps1 b/codex/setup-codex.ps1 index 52e3377..4af57f3 100644 --- a/codex/setup-codex.ps1 +++ b/codex/setup-codex.ps1 @@ -94,6 +94,25 @@ function Get-NodeVersion { return $null } +# ========== fnm PATH 搜索 ========== +function Find-FnmPath { + $searchPaths = @( + "$env:LOCALAPPDATA\Microsoft\WinGet\Links", + "$env:LOCALAPPDATA\Microsoft\WinGet\Packages\Schniz.fnm_*", + "$env:USERPROFILE\.fnm", + "$env:LOCALAPPDATA\fnm", + "$env:ProgramFiles\fnm" + ) + foreach ($pattern in $searchPaths) { + $resolved = Resolve-Path $pattern -ErrorAction SilentlyContinue + foreach ($dir in $resolved) { + $fnmExe = Get-ChildItem -Path $dir.Path -Filter "fnm.exe" -Recurse -Depth 2 -ErrorAction SilentlyContinue | Select-Object -First 1 + if ($fnmExe) { return $fnmExe.DirectoryName } + } + } + return $null +} + function Install-Fnm { Write-Host "" Write-Info "正在安装 fnm (Fast Node Manager)..." @@ -110,6 +129,15 @@ function Install-Fnm { Refresh-Path + if (-not (Test-Command "fnm")) { + Write-Info "正在搜索 fnm 安装位置..." + $fnmDir = Find-FnmPath + if ($fnmDir) { + Write-Info "在 $fnmDir 找到 fnm" + $env:Path = "$fnmDir;$env:Path" + } + } + if (Test-Command "fnm") { Write-Success "fnm 安装成功!" $fnmEnv = & fnm env --use-on-cd 2>$null @@ -119,8 +147,7 @@ function Install-Fnm { return $true } else { - Write-Warning "fnm 可能已安装,但需要重新打开终端才能生效" - Write-Info "请重新打开 PowerShell 后再运行此脚本" + Write-Warning "fnm 安装后未能在 PATH 中找到,请重新打开终端后再运行此脚本" return $false } } diff --git a/setup-claude-code.ps1 b/setup-claude-code.ps1 index bd08295..0b724e1 100644 --- a/setup-claude-code.ps1 +++ b/setup-claude-code.ps1 @@ -94,6 +94,25 @@ function Get-NodeVersion { return $null } +# ========== fnm PATH 搜索 ========== +function Find-FnmPath { + $searchPaths = @( + "$env:LOCALAPPDATA\Microsoft\WinGet\Links", + "$env:LOCALAPPDATA\Microsoft\WinGet\Packages\Schniz.fnm_*", + "$env:USERPROFILE\.fnm", + "$env:LOCALAPPDATA\fnm", + "$env:ProgramFiles\fnm" + ) + foreach ($pattern in $searchPaths) { + $resolved = Resolve-Path $pattern -ErrorAction SilentlyContinue + foreach ($dir in $resolved) { + $fnmExe = Get-ChildItem -Path $dir.Path -Filter "fnm.exe" -Recurse -Depth 2 -ErrorAction SilentlyContinue | Select-Object -First 1 + if ($fnmExe) { return $fnmExe.DirectoryName } + } + } + return $null +} + function Install-Fnm { Write-Host "" Write-Info "正在安装 fnm (Fast Node Manager)..." @@ -110,6 +129,15 @@ function Install-Fnm { Refresh-Path + if (-not (Test-Command "fnm")) { + Write-Info "正在搜索 fnm 安装位置..." + $fnmDir = Find-FnmPath + if ($fnmDir) { + Write-Info "在 $fnmDir 找到 fnm" + $env:Path = "$fnmDir;$env:Path" + } + } + if (Test-Command "fnm") { Write-Success "fnm 安装成功!" $fnmEnv = & fnm env --use-on-cd 2>$null @@ -119,8 +147,7 @@ function Install-Fnm { return $true } else { - Write-Warning "fnm 可能已安装,但需要重新打开终端才能生效" - Write-Info "请重新打开 PowerShell 后再运行此脚本" + Write-Warning "fnm 安装后未能在 PATH 中找到,请重新打开终端后再运行此脚本" return $false } } diff --git a/setup.ps1 b/setup.ps1 index 935a901..358acf9 100644 --- a/setup.ps1 +++ b/setup.ps1 @@ -98,6 +98,26 @@ function Get-NodeVersion { return $null } +# ========== fnm PATH 搜索 ========== +function Find-FnmPath { + # 在常见安装位置搜索 fnm.exe + $searchPaths = @( + "$env:LOCALAPPDATA\Microsoft\WinGet\Links", + "$env:LOCALAPPDATA\Microsoft\WinGet\Packages\Schniz.fnm_*", + "$env:USERPROFILE\.fnm", + "$env:LOCALAPPDATA\fnm", + "$env:ProgramFiles\fnm" + ) + foreach ($pattern in $searchPaths) { + $resolved = Resolve-Path $pattern -ErrorAction SilentlyContinue + foreach ($dir in $resolved) { + $fnmExe = Get-ChildItem -Path $dir.Path -Filter "fnm.exe" -Recurse -Depth 2 -ErrorAction SilentlyContinue | Select-Object -First 1 + if ($fnmExe) { return $fnmExe.DirectoryName } + } + } + return $null +} + # ========== fnm 安装 ========== function Install-Fnm { Write-Host "" @@ -110,13 +130,23 @@ function Install-Fnm { & winget install Schniz.fnm -e --source winget --accept-source-agreements --accept-package-agreements | Out-Host } else { - # 备选:使用 cargo 或手动下载 + # 备选:使用 PowerShell 脚本安装 fnm Write-Info "使用 PowerShell 脚本安装 fnm..." Invoke-Expression "& { $(Invoke-RestMethod https://fnm.vercel.app/install.ps1) }" | Out-Host } Refresh-Path + # 如果 fnm 不在 PATH 中,搜索常见安装位置 + if (-not (Test-Command "fnm")) { + Write-Info "正在搜索 fnm 安装位置..." + $fnmDir = Find-FnmPath + if ($fnmDir) { + Write-Info "在 $fnmDir 找到 fnm" + $env:Path = "$fnmDir;$env:Path" + } + } + # 配置 fnm 环境 if (Test-Command "fnm") { Write-Success "fnm 安装成功!" @@ -128,8 +158,7 @@ function Install-Fnm { return $true } else { - Write-Warning "fnm 可能已安装,但需要重新打开终端才能生效" - Write-Info "请重新打开 PowerShell 后再运行此脚本" + Write-Warning "fnm 安装后未能在 PATH 中找到,请重新打开终端后再运行此脚本" return $false } }