fix: protect against null .Trim() and Get-ScheduledTask crashes; add pause before exit in pipe mode

This commit is contained in:
i
2026-06-02 01:08:36 +08:00
parent ff5b372bf5
commit 2b2a8cb70e
+9 -5
View File
@@ -205,7 +205,7 @@ function Detect-Installed {
$script:INSTALLED_SOURCE = "github-unknown" $script:INSTALLED_SOURCE = "github-unknown"
} }
} else { } else {
$npmPrefix = (& npm config get prefix 2>$null).Trim() $npmPrefix = try { (& npm config get prefix 2>$null).Trim() } catch { "" }
if ($path -like "$npmPrefix*") { if ($path -like "$npmPrefix*") {
$script:INSTALLED_SOURCE = "npm" $script:INSTALLED_SOURCE = "npm"
} else { } else {
@@ -423,7 +423,7 @@ function Install-FromNpm {
Update-EnvPath Update-EnvPath
$hapi = Get-Command "hapi" -ErrorAction SilentlyContinue $hapi = Get-Command "hapi" -ErrorAction SilentlyContinue
if (-not $hapi) { if (-not $hapi) {
$npmPrefix = (& npm config get prefix).Trim() $npmPrefix = try { (& npm config get prefix).Trim() } catch { "" }
$npmBin = Join-Path $npmPrefix "" $npmBin = Join-Path $npmPrefix ""
[System.Environment]::SetEnvironmentVariable("Path", "$env:Path;$npmBin", "User") [System.Environment]::SetEnvironmentVariable("Path", "$env:Path;$npmBin", "User")
Update-EnvPath Update-EnvPath
@@ -629,7 +629,7 @@ function Uninstall-Hapi {
Remove-Item $path -Force -ErrorAction SilentlyContinue Remove-Item $path -Force -ErrorAction SilentlyContinue
Write-Info "已删除 $path" Write-Info "已删除 $path"
} else { } else {
$npmPrefix = (& npm config get prefix 2>$null).Trim() $npmPrefix = try { (& npm config get prefix 2>$null).Trim() } catch { "" }
if ($path -like "$npmPrefix*") { if ($path -like "$npmPrefix*") {
Write-Info "从 npm 卸载 HAPI CLI..." Write-Info "从 npm 卸载 HAPI CLI..."
& npm uninstall -g $script:NPM_PACKAGE 2>$null | Out-File $LOG_FILE -Append & npm uninstall -g $script:NPM_PACKAGE 2>$null | Out-File $LOG_FILE -Append
@@ -712,8 +712,8 @@ function Collect-Status {
$script:STATUS_HUB_CONFIGURED = $true $script:STATUS_HUB_CONFIGURED = $true
} }
$hubTask = Get-ScheduledTask -TaskName "HAPI Hub" -ErrorAction SilentlyContinue $hubTask = try { Get-ScheduledTask -TaskName "HAPI Hub" -ErrorAction Stop } catch { $null }
$runnerTask = Get-ScheduledTask -TaskName "HAPI Runner" -ErrorAction SilentlyContinue $runnerTask = try { Get-ScheduledTask -TaskName "HAPI Runner" -ErrorAction Stop } catch { $null }
if ($hubTask) { if ($hubTask) {
$script:STATUS_AUTOSTART_HUB = $hubTask.State $script:STATUS_AUTOSTART_HUB = $hubTask.State
} }
@@ -1061,10 +1061,12 @@ Print-Status
if ($isIexExecution) { if ($isIexExecution) {
if ($Upgrade) { if ($Upgrade) {
Invoke-Upgrade Invoke-Upgrade
Read-Host "按 Enter 退出..."
exit exit
} }
if ($Force -or $Source -or $SkipHub -or $InstallDir) { if ($Force -or $Source -or $SkipHub -or $InstallDir) {
Invoke-Install Invoke-Install
Read-Host "按 Enter 退出..."
exit exit
} }
if (-not $script:STATUS_CLI_INSTALLED) { if (-not $script:STATUS_CLI_INSTALLED) {
@@ -1072,6 +1074,8 @@ if ($isIexExecution) {
Write-Warn "HAPI CLI 未安装" Write-Warn "HAPI CLI 未安装"
Write-Warn "请使用 -Source / -Force 等参数指定操作,或直接运行 install.ps1 进入交互模式" Write-Warn "请使用 -Source / -Force 等参数指定操作,或直接运行 install.ps1 进入交互模式"
} }
Write-Host ""
Read-Host "按 Enter 退出..."
exit exit
} }