Private
Public Access
1
0

fix: 修复 PowerShell 函数输出流污染导致返回值被忽略

外部命令(winget/fnm/npm)的 stdout 输出进入函数输出流,
导致 return $false 实际返回数组, if (-not (Func)) 判断失效,
脚本在 fnm 不可用时仍继续执行 Install-NodeWithFnm 导致报错。
所有外部命令调用加 | Out-Host 将输出重定向到控制台。
This commit is contained in:
2026-03-20 23:08:55 +08:00
parent 783d1fe8d8
commit 498122dbed
5 changed files with 37 additions and 37 deletions

View File

@@ -101,11 +101,11 @@ function Install-Fnm {
try { try {
if (Test-Command "winget") { if (Test-Command "winget") {
Write-Info "使用 winget 安装 fnm..." Write-Info "使用 winget 安装 fnm..."
& winget install Schniz.fnm -e --accept-source-agreements --accept-package-agreements & winget install Schniz.fnm -e --accept-source-agreements --accept-package-agreements | Out-Host
} }
else { else {
Write-Info "使用 PowerShell 脚本安装 fnm..." Write-Info "使用 PowerShell 脚本安装 fnm..."
Invoke-Expression "& { $(Invoke-RestMethod https://fnm.vercel.app/install.ps1) }" Invoke-Expression "& { $(Invoke-RestMethod https://fnm.vercel.app/install.ps1) }" | Out-Host
} }
Refresh-Path Refresh-Path
@@ -114,7 +114,7 @@ function Install-Fnm {
Write-Success "fnm 安装成功!" Write-Success "fnm 安装成功!"
$fnmEnv = & fnm env --use-on-cd 2>$null $fnmEnv = & fnm env --use-on-cd 2>$null
if ($fnmEnv) { if ($fnmEnv) {
$fnmEnv | Out-String | Invoke-Expression $fnmEnv | Out-String | Invoke-Expression | Out-Null
} }
return $true return $true
} }
@@ -134,9 +134,9 @@ function Install-NodeWithFnm {
Write-Info "使用 fnm 安装 Node.js 24.x..." Write-Info "使用 fnm 安装 Node.js 24.x..."
try { try {
& fnm install 24 & fnm install 24 | Out-Host
& fnm use 24 & fnm use 24 | Out-Host
& fnm default 24 & fnm default 24 | Out-Host
Refresh-Path Refresh-Path
if (Test-Command "node") { if (Test-Command "node") {
@@ -204,7 +204,7 @@ function Install-Tool {
Write-Host " 执行: npm install -g $ToolPackage" -ForegroundColor Gray Write-Host " 执行: npm install -g $ToolPackage" -ForegroundColor Gray
try { try {
Invoke-Expression "npm install -g $ToolPackage" Invoke-Expression "npm install -g $ToolPackage" | Out-Host
$exitCode = $LASTEXITCODE $exitCode = $LASTEXITCODE
Refresh-Path Refresh-Path

View File

@@ -99,11 +99,11 @@ function Install-Fnm {
try { try {
if (Test-Command "winget") { if (Test-Command "winget") {
Write-Info "使用 winget 安装 fnm..." Write-Info "使用 winget 安装 fnm..."
& winget install Schniz.fnm -e --accept-source-agreements --accept-package-agreements & winget install Schniz.fnm -e --accept-source-agreements --accept-package-agreements | Out-Host
} }
else { else {
Write-Info "使用 PowerShell 脚本安装 fnm..." Write-Info "使用 PowerShell 脚本安装 fnm..."
Invoke-Expression "& { $(Invoke-RestMethod https://fnm.vercel.app/install.ps1) }" Invoke-Expression "& { $(Invoke-RestMethod https://fnm.vercel.app/install.ps1) }" | Out-Host
} }
Refresh-Path Refresh-Path
@@ -112,7 +112,7 @@ function Install-Fnm {
Write-Success "fnm 安装成功!" Write-Success "fnm 安装成功!"
$fnmEnv = & fnm env --use-on-cd 2>$null $fnmEnv = & fnm env --use-on-cd 2>$null
if ($fnmEnv) { if ($fnmEnv) {
$fnmEnv | Out-String | Invoke-Expression $fnmEnv | Out-String | Invoke-Expression | Out-Null
} }
return $true return $true
} }
@@ -132,9 +132,9 @@ function Install-NodeWithFnm {
Write-Info "使用 fnm 安装 Node.js 24.x..." Write-Info "使用 fnm 安装 Node.js 24.x..."
try { try {
& fnm install 24 & fnm install 24 | Out-Host
& fnm use 24 & fnm use 24 | Out-Host
& fnm default 24 & fnm default 24 | Out-Host
Refresh-Path Refresh-Path
if (Test-Command "node") { if (Test-Command "node") {
@@ -203,7 +203,7 @@ function Install-Tool {
Write-Host " 执行: $installCmd" -ForegroundColor Gray Write-Host " 执行: $installCmd" -ForegroundColor Gray
try { try {
Invoke-Expression $installCmd Invoke-Expression $installCmd | Out-Host
$exitCode = $LASTEXITCODE $exitCode = $LASTEXITCODE
Refresh-Path Refresh-Path

View File

@@ -101,11 +101,11 @@ function Install-Fnm {
try { try {
if (Test-Command "winget") { if (Test-Command "winget") {
Write-Info "使用 winget 安装 fnm..." Write-Info "使用 winget 安装 fnm..."
& winget install Schniz.fnm -e --accept-source-agreements --accept-package-agreements & winget install Schniz.fnm -e --accept-source-agreements --accept-package-agreements | Out-Host
} }
else { else {
Write-Info "使用 PowerShell 脚本安装 fnm..." Write-Info "使用 PowerShell 脚本安装 fnm..."
Invoke-Expression "& { $(Invoke-RestMethod https://fnm.vercel.app/install.ps1) }" Invoke-Expression "& { $(Invoke-RestMethod https://fnm.vercel.app/install.ps1) }" | Out-Host
} }
Refresh-Path Refresh-Path
@@ -114,7 +114,7 @@ function Install-Fnm {
Write-Success "fnm 安装成功!" Write-Success "fnm 安装成功!"
$fnmEnv = & fnm env --use-on-cd 2>$null $fnmEnv = & fnm env --use-on-cd 2>$null
if ($fnmEnv) { if ($fnmEnv) {
$fnmEnv | Out-String | Invoke-Expression $fnmEnv | Out-String | Invoke-Expression | Out-Null
} }
return $true return $true
} }
@@ -134,9 +134,9 @@ function Install-NodeWithFnm {
Write-Info "使用 fnm 安装 Node.js 24.x..." Write-Info "使用 fnm 安装 Node.js 24.x..."
try { try {
& fnm install 24 & fnm install 24 | Out-Host
& fnm use 24 & fnm use 24 | Out-Host
& fnm default 24 & fnm default 24 | Out-Host
Refresh-Path Refresh-Path
if (Test-Command "node") { if (Test-Command "node") {
@@ -205,7 +205,7 @@ function Install-Tool {
Write-Host " 执行: $installCmd" -ForegroundColor Gray Write-Host " 执行: $installCmd" -ForegroundColor Gray
try { try {
Invoke-Expression $installCmd Invoke-Expression $installCmd | Out-Host
$exitCode = $LASTEXITCODE $exitCode = $LASTEXITCODE
Refresh-Path Refresh-Path

View File

@@ -101,11 +101,11 @@ function Install-Fnm {
try { try {
if (Test-Command "winget") { if (Test-Command "winget") {
Write-Info "使用 winget 安装 fnm..." Write-Info "使用 winget 安装 fnm..."
& winget install Schniz.fnm -e --accept-source-agreements --accept-package-agreements & winget install Schniz.fnm -e --accept-source-agreements --accept-package-agreements | Out-Host
} }
else { else {
Write-Info "使用 PowerShell 脚本安装 fnm..." Write-Info "使用 PowerShell 脚本安装 fnm..."
Invoke-Expression "& { $(Invoke-RestMethod https://fnm.vercel.app/install.ps1) }" Invoke-Expression "& { $(Invoke-RestMethod https://fnm.vercel.app/install.ps1) }" | Out-Host
} }
Refresh-Path Refresh-Path
@@ -114,7 +114,7 @@ function Install-Fnm {
Write-Success "fnm 安装成功!" Write-Success "fnm 安装成功!"
$fnmEnv = & fnm env --use-on-cd 2>$null $fnmEnv = & fnm env --use-on-cd 2>$null
if ($fnmEnv) { if ($fnmEnv) {
$fnmEnv | Out-String | Invoke-Expression $fnmEnv | Out-String | Invoke-Expression | Out-Null
} }
return $true return $true
} }
@@ -134,9 +134,9 @@ function Install-NodeWithFnm {
Write-Info "使用 fnm 安装 Node.js 24.x..." Write-Info "使用 fnm 安装 Node.js 24.x..."
try { try {
& fnm install 24 & fnm install 24 | Out-Host
& fnm use 24 & fnm use 24 | Out-Host
& fnm default 24 & fnm default 24 | Out-Host
Refresh-Path Refresh-Path
if (Test-Command "node") { if (Test-Command "node") {
@@ -204,7 +204,7 @@ function Install-Tool {
Write-Host " 执行: npm install -g $ToolPackage" -ForegroundColor Gray Write-Host " 执行: npm install -g $ToolPackage" -ForegroundColor Gray
try { try {
Invoke-Expression "npm install -g $ToolPackage" Invoke-Expression "npm install -g $ToolPackage" | Out-Host
$exitCode = $LASTEXITCODE $exitCode = $LASTEXITCODE
Refresh-Path Refresh-Path

View File

@@ -107,12 +107,12 @@ function Install-Fnm {
# 使用 winget 安装 fnm # 使用 winget 安装 fnm
if (Test-Command "winget") { if (Test-Command "winget") {
Write-Info "使用 winget 安装 fnm..." Write-Info "使用 winget 安装 fnm..."
& winget install Schniz.fnm -e --accept-source-agreements --accept-package-agreements & winget install Schniz.fnm -e --accept-source-agreements --accept-package-agreements | Out-Host
} }
else { else {
# 备选:使用 cargo 或手动下载 # 备选:使用 cargo 或手动下载
Write-Info "使用 PowerShell 脚本安装 fnm..." Write-Info "使用 PowerShell 脚本安装 fnm..."
Invoke-Expression "& { $(Invoke-RestMethod https://fnm.vercel.app/install.ps1) }" Invoke-Expression "& { $(Invoke-RestMethod https://fnm.vercel.app/install.ps1) }" | Out-Host
} }
Refresh-Path Refresh-Path
@@ -123,7 +123,7 @@ function Install-Fnm {
# 初始化 fnm # 初始化 fnm
$fnmEnv = & fnm env --use-on-cd 2>$null $fnmEnv = & fnm env --use-on-cd 2>$null
if ($fnmEnv) { if ($fnmEnv) {
$fnmEnv | Out-String | Invoke-Expression $fnmEnv | Out-String | Invoke-Expression | Out-Null
} }
return $true return $true
} }
@@ -144,9 +144,9 @@ function Install-NodeWithFnm {
Write-Info "使用 fnm 安装 Node.js 24.x..." Write-Info "使用 fnm 安装 Node.js 24.x..."
try { try {
& fnm install 24 & fnm install 24 | Out-Host
& fnm use 24 & fnm use 24 | Out-Host
& fnm default 24 & fnm default 24 | Out-Host
Refresh-Path Refresh-Path
if (Test-Command "node") { if (Test-Command "node") {
@@ -226,7 +226,7 @@ function Install-Tool {
Write-Host " 执行: $installCmd" -ForegroundColor Gray Write-Host " 执行: $installCmd" -ForegroundColor Gray
try { try {
Invoke-Expression $installCmd Invoke-Expression $installCmd | Out-Host
$exitCode = $LASTEXITCODE $exitCode = $LASTEXITCODE
Refresh-Path Refresh-Path
@@ -272,10 +272,10 @@ function Invoke-RemoteSetup {
# 执行脚本,正确传递参数 # 执行脚本,正确传递参数
$LASTEXITCODE = 0 $LASTEXITCODE = 0
if ($ApiKey) { if ($ApiKey) {
& $tempFile -ApiKey $ApiKey & $tempFile -ApiKey $ApiKey | Out-Host
} }
else { else {
& $tempFile & $tempFile | Out-Host
} }
$scriptSucceeded = $? $scriptSucceeded = $?