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

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