feat: winget 安装 fnm 后智能搜索 PATH 避免重启终端
winget 安装 fnm 后 PATH 可能未立即生效,新增 Find-FnmPath 在 WinGet Links/Packages、.fnm、LOCALAPPDATA 等常见位置搜索 fnm.exe 并自动加入当前会话 PATH,无需重启终端。
This commit is contained in:
@@ -94,6 +94,25 @@ function Get-NodeVersion {
|
|||||||
return $null
|
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 {
|
function Install-Fnm {
|
||||||
Write-Host ""
|
Write-Host ""
|
||||||
Write-Info "正在安装 fnm (Fast Node Manager)..."
|
Write-Info "正在安装 fnm (Fast Node Manager)..."
|
||||||
@@ -110,6 +129,16 @@ function Install-Fnm {
|
|||||||
|
|
||||||
Refresh-Path
|
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") {
|
if (Test-Command "fnm") {
|
||||||
Write-Success "fnm 安装成功!"
|
Write-Success "fnm 安装成功!"
|
||||||
$fnmEnv = & fnm env --use-on-cd 2>$null
|
$fnmEnv = & fnm env --use-on-cd 2>$null
|
||||||
@@ -119,8 +148,7 @@ function Install-Fnm {
|
|||||||
return $true
|
return $true
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
Write-Warning "fnm 可能已安装,但需要重新打开终端才能生效"
|
Write-Warning "fnm 安装后未能在 PATH 中找到,请重新打开终端后再运行此脚本"
|
||||||
Write-Info "请重新打开 PowerShell 后再运行此脚本"
|
|
||||||
return $false
|
return $false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -92,6 +92,25 @@ function Get-NodeVersion {
|
|||||||
return $null
|
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 {
|
function Install-Fnm {
|
||||||
Write-Host ""
|
Write-Host ""
|
||||||
Write-Info "正在安装 fnm (Fast Node Manager)..."
|
Write-Info "正在安装 fnm (Fast Node Manager)..."
|
||||||
@@ -108,6 +127,15 @@ function Install-Fnm {
|
|||||||
|
|
||||||
Refresh-Path
|
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") {
|
if (Test-Command "fnm") {
|
||||||
Write-Success "fnm 安装成功!"
|
Write-Success "fnm 安装成功!"
|
||||||
$fnmEnv = & fnm env --use-on-cd 2>$null
|
$fnmEnv = & fnm env --use-on-cd 2>$null
|
||||||
@@ -117,8 +145,7 @@ function Install-Fnm {
|
|||||||
return $true
|
return $true
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
Write-Warning "fnm 可能已安装,但需要重新打开终端才能生效"
|
Write-Warning "fnm 安装后未能在 PATH 中找到,请重新打开终端后再运行此脚本"
|
||||||
Write-Info "请重新打开 PowerShell 后再运行此脚本"
|
|
||||||
return $false
|
return $false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -94,6 +94,25 @@ function Get-NodeVersion {
|
|||||||
return $null
|
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 {
|
function Install-Fnm {
|
||||||
Write-Host ""
|
Write-Host ""
|
||||||
Write-Info "正在安装 fnm (Fast Node Manager)..."
|
Write-Info "正在安装 fnm (Fast Node Manager)..."
|
||||||
@@ -110,6 +129,15 @@ function Install-Fnm {
|
|||||||
|
|
||||||
Refresh-Path
|
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") {
|
if (Test-Command "fnm") {
|
||||||
Write-Success "fnm 安装成功!"
|
Write-Success "fnm 安装成功!"
|
||||||
$fnmEnv = & fnm env --use-on-cd 2>$null
|
$fnmEnv = & fnm env --use-on-cd 2>$null
|
||||||
@@ -119,8 +147,7 @@ function Install-Fnm {
|
|||||||
return $true
|
return $true
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
Write-Warning "fnm 可能已安装,但需要重新打开终端才能生效"
|
Write-Warning "fnm 安装后未能在 PATH 中找到,请重新打开终端后再运行此脚本"
|
||||||
Write-Info "请重新打开 PowerShell 后再运行此脚本"
|
|
||||||
return $false
|
return $false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -94,6 +94,25 @@ function Get-NodeVersion {
|
|||||||
return $null
|
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 {
|
function Install-Fnm {
|
||||||
Write-Host ""
|
Write-Host ""
|
||||||
Write-Info "正在安装 fnm (Fast Node Manager)..."
|
Write-Info "正在安装 fnm (Fast Node Manager)..."
|
||||||
@@ -110,6 +129,15 @@ function Install-Fnm {
|
|||||||
|
|
||||||
Refresh-Path
|
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") {
|
if (Test-Command "fnm") {
|
||||||
Write-Success "fnm 安装成功!"
|
Write-Success "fnm 安装成功!"
|
||||||
$fnmEnv = & fnm env --use-on-cd 2>$null
|
$fnmEnv = & fnm env --use-on-cd 2>$null
|
||||||
@@ -119,8 +147,7 @@ function Install-Fnm {
|
|||||||
return $true
|
return $true
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
Write-Warning "fnm 可能已安装,但需要重新打开终端才能生效"
|
Write-Warning "fnm 安装后未能在 PATH 中找到,请重新打开终端后再运行此脚本"
|
||||||
Write-Info "请重新打开 PowerShell 后再运行此脚本"
|
|
||||||
return $false
|
return $false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
35
setup.ps1
35
setup.ps1
@@ -98,6 +98,26 @@ function Get-NodeVersion {
|
|||||||
return $null
|
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 安装 ==========
|
# ========== fnm 安装 ==========
|
||||||
function Install-Fnm {
|
function Install-Fnm {
|
||||||
Write-Host ""
|
Write-Host ""
|
||||||
@@ -110,13 +130,23 @@ function Install-Fnm {
|
|||||||
& winget install Schniz.fnm -e --source winget --accept-source-agreements --accept-package-agreements | Out-Host
|
& winget install Schniz.fnm -e --source winget --accept-source-agreements --accept-package-agreements | Out-Host
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
# 备选:使用 cargo 或手动下载
|
# 备选:使用 PowerShell 脚本安装 fnm
|
||||||
Write-Info "使用 PowerShell 脚本安装 fnm..."
|
Write-Info "使用 PowerShell 脚本安装 fnm..."
|
||||||
Invoke-Expression "& { $(Invoke-RestMethod https://fnm.vercel.app/install.ps1) }" | Out-Host
|
Invoke-Expression "& { $(Invoke-RestMethod https://fnm.vercel.app/install.ps1) }" | Out-Host
|
||||||
}
|
}
|
||||||
|
|
||||||
Refresh-Path
|
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 环境
|
# 配置 fnm 环境
|
||||||
if (Test-Command "fnm") {
|
if (Test-Command "fnm") {
|
||||||
Write-Success "fnm 安装成功!"
|
Write-Success "fnm 安装成功!"
|
||||||
@@ -128,8 +158,7 @@ function Install-Fnm {
|
|||||||
return $true
|
return $true
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
Write-Warning "fnm 可能已安装,但需要重新打开终端才能生效"
|
Write-Warning "fnm 安装后未能在 PATH 中找到,请重新打开终端后再运行此脚本"
|
||||||
Write-Info "请重新打开 PowerShell 后再运行此脚本"
|
|
||||||
return $false
|
return $false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user