Private
Public Access
1
0

feat: winget 安装 fnm 后智能搜索 PATH 避免重启终端

winget 安装 fnm 后 PATH 可能未立即生效,新增 Find-FnmPath
在 WinGet Links/Packages、.fnm、LOCALAPPDATA 等常见位置搜索
fnm.exe 并自动加入当前会话 PATH,无需重启终端。
This commit is contained in:
2026-03-20 23:25:49 +08:00
parent e20a93b480
commit fcb8c5e45e
5 changed files with 149 additions and 11 deletions

View File

@@ -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
}
}