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"
}
} else {
$npmPrefix = (& npm config get prefix 2>$null).Trim()
$npmPrefix = try { (& npm config get prefix 2>$null).Trim() } catch { "" }
if ($path -like "$npmPrefix*") {
$script:INSTALLED_SOURCE = "npm"
} else {
@@ -423,7 +423,7 @@ function Install-FromNpm {
Update-EnvPath
$hapi = Get-Command "hapi" -ErrorAction SilentlyContinue
if (-not $hapi) {
$npmPrefix = (& npm config get prefix).Trim()
$npmPrefix = try { (& npm config get prefix).Trim() } catch { "" }
$npmBin = Join-Path $npmPrefix ""
[System.Environment]::SetEnvironmentVariable("Path", "$env:Path;$npmBin", "User")
Update-EnvPath
@@ -629,7 +629,7 @@ function Uninstall-Hapi {
Remove-Item $path -Force -ErrorAction SilentlyContinue
Write-Info "已删除 $path"
} else {
$npmPrefix = (& npm config get prefix 2>$null).Trim()
$npmPrefix = try { (& npm config get prefix 2>$null).Trim() } catch { "" }
if ($path -like "$npmPrefix*") {
Write-Info "从 npm 卸载 HAPI CLI..."
& 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
}
$hubTask = Get-ScheduledTask -TaskName "HAPI Hub" -ErrorAction SilentlyContinue
$runnerTask = Get-ScheduledTask -TaskName "HAPI Runner" -ErrorAction SilentlyContinue
$hubTask = try { Get-ScheduledTask -TaskName "HAPI Hub" -ErrorAction Stop } catch { $null }
$runnerTask = try { Get-ScheduledTask -TaskName "HAPI Runner" -ErrorAction Stop } catch { $null }
if ($hubTask) {
$script:STATUS_AUTOSTART_HUB = $hubTask.State
}
@@ -1061,10 +1061,12 @@ Print-Status
if ($isIexExecution) {
if ($Upgrade) {
Invoke-Upgrade
Read-Host "按 Enter 退出..."
exit
}
if ($Force -or $Source -or $SkipHub -or $InstallDir) {
Invoke-Install
Read-Host "按 Enter 退出..."
exit
}
if (-not $script:STATUS_CLI_INSTALLED) {
@@ -1072,6 +1074,8 @@ if ($isIexExecution) {
Write-Warn "HAPI CLI 未安装"
Write-Warn "请使用 -Source / -Force 等参数指定操作,或直接运行 install.ps1 进入交互模式"
}
Write-Host ""
Read-Host "按 Enter 退出..."
exit
}