fix: show actual hapi install path when already installed via npm in quick mode

This commit is contained in:
i
2026-06-02 00:41:15 +08:00
parent 19ee388805
commit 71ba9eba4c
2 changed files with 33 additions and 2 deletions
+23 -2
View File
@@ -219,6 +219,18 @@ function Detect-Installed {
$script:INSTALLED_VERSION = (Get-Content $script:VERSION_FILE -Raw).Trim()
}
}
# 如果 hapi 命令未找到,尝试通过 npm list -g 检测
if (-not $hapiCmd -and (Get-Command "npm" -ErrorAction SilentlyContinue)) {
$npmPkg = (& npm list -g $script:NPM_PACKAGE --depth=0 2>$null) | Select-String -Pattern '@twsxtd/hapi@'
if ($npmPkg) {
$verMatch = $npmPkg | Select-String -Pattern '\d+\.\d+\.\d+'
if ($verMatch) {
$script:INSTALLED_VERSION = $verMatch.Matches.Value
$script:INSTALLED_SOURCE = "npm"
}
}
}
}
# ─────────────────────────────────────────
@@ -227,8 +239,11 @@ function Detect-Installed {
function Get-RemoteNpmVersion {
if (-not (Get-Command "npm" -ErrorAction SilentlyContinue)) { return $null }
try {
$regArg = if ($script:NPM_REGISTRY -and $script:NPM_REGISTRY -ne "https://registry.npmjs.org") { "--registry=$($script:NPM_REGISTRY)" } else { "" }
$ver = (& npm view $script:NPM_PACKAGE version $regArg 2>$null).Trim()
$npmArgs = @("view", $script:NPM_PACKAGE, "version")
if ($script:NPM_REGISTRY -and $script:NPM_REGISTRY -ne "https://registry.npmjs.org") {
$npmArgs += "--registry=$($script:NPM_REGISTRY)"
}
$ver = (& npm @npmArgs 2>$null).Trim()
if ($ver) { return $ver }
} catch {}
return $null
@@ -1165,6 +1180,12 @@ if ($Upgrade -or $Force -or $Source -or $SkipHub -or $InstallDir -or $isIexExecu
Write-Host "║ HAPI 快速模式 ║" -ForegroundColor White
Write-Host "╚══════════════════════════════════════════════════════════════════╝" -ForegroundColor White
Write-Host ""
Detect-Installed
if ($script:INSTALLED_VERSION) {
$srcDisplay = $script:INSTALLED_SOURCE
if ($srcDisplay -like "github:*") { $srcDisplay = $srcDisplay.Substring(7) }
Write-Info "检测到已安装 HAPI CLI v$($script:INSTALLED_VERSION) (来源: $srcDisplay, 路径: $((Get-Command hapi -ErrorAction SilentlyContinue).Source))"
}
Setup-InstallDir
Check-Prerequisites
Run-QuickMode
+10
View File
@@ -1390,6 +1390,16 @@ check_prerequisites() {
# 快速模式:向后兼容的命令行参数
# ─────────────────────────────────────────
run_quick_mode() {
detect_installed
if [[ -n "$INSTALLED_VERSION" ]]; then
local actual_path
actual_path=$(command -v hapi 2>/dev/null || true)
local src_display="$INSTALLED_SOURCE"
[[ "$src_display" == github:* ]] && src_display="${src_display#github:}"
info "检测到已安装 HAPI CLI v$INSTALLED_VERSION (来源: $src_display, 路径: $actual_path)"
fi
setup_install_dir
check_prerequisites