fix: remove npm version/source file writes, infer from path instead

npm installs to global node_modules, so writing .hapi-version and
.hapi-source under /opt/hapi (or C:\hapi) is unnecessary and fails
when that directory doesn't exist yet.

- Stop writing VERSION_FILE and SOURCE_FILE in install_from_npm
- Infer "npm" source from hapi path in print_result when files absent
- Sync both install.sh and install.ps1

via [HAPI](https://hapi.run)

Co-Authored-By: HAPI <[email protected]>
This commit is contained in:
i
2026-06-01 23:18:11 +08:00
co-authored by HAPI
parent 8cb35598af
commit 65c6e960a9
2 changed files with 32 additions and 5 deletions
+13 -4
View File
@@ -413,9 +413,6 @@ function Install-FromNpm {
$installedVer = ((& hapi --version 2>$null) | Select-String -Pattern '\d+\.\d+\.\d+' | ForEach-Object { $_.Matches.Value } | Select-Object -First 1)
if (-not $installedVer) { $installedVer = "unknown" }
$installedVer | Set-Content $script:VERSION_FILE -Encoding UTF8
"npm" | Set-Content $script:SOURCE_FILE -Encoding UTF8
Write-Ok "HAPI CLI v$installedVer 安装成功 (来源: npm, 路径: $($hapi.Source))"
return $true
}
@@ -855,7 +852,19 @@ function Print-Result {
} else {
(Get-Content $VERSION_FILE -Raw).Trim()
}
$hapiSource = if (Test-Path $SOURCE_FILE) { (Get-Content $SOURCE_FILE -Raw).Trim() } else { "unknown" }
$hapiSource = if (Test-Path $SOURCE_FILE) {
(Get-Content $SOURCE_FILE -Raw).Trim()
} elseif ($script:INSTALLED_SOURCE) {
$script:INSTALLED_SOURCE
} else {
$currentHapi = (Get-Command hapi -ErrorAction SilentlyContinue).Source
if ($currentHapi -and $currentHapi -notlike "$script:HAPI_BIN_DIR*") {
$npmPrefix = (& npm config get prefix 2>$null).Trim()
if ($currentHapi -like "$npmPrefix*") { "npm" } else { "unknown" }
} else {
"unknown"
}
}
Write-Host ""
Write-Host "╔══════════════════════════════════════════════════════════════════╗" -ForegroundColor Green
+19 -1
View File
@@ -1106,7 +1106,25 @@ print_result() {
echo -e "${GREEN}║${NC} ${BOLD}HAPI 安装完成${NC} ${GREEN}║${NC}"
echo -e "${GREEN}╚══════════════════════════════════════════════════════════════════╝${NC}"
echo ""
echo -e " ${BOLD}HAPI CLI:${NC} v$hapi_ver (来源: $(cat "$SOURCE_FILE" 2>/dev/null || echo unknown))"
local hapi_source
if [[ -f "$SOURCE_FILE" ]]; then
hapi_source=$(cat "$SOURCE_FILE" 2>/dev/null)
else
hapi_source="$INSTALLED_SOURCE"
if [[ -z "$hapi_source" || "$hapi_source" == "unknown" ]]; then
local current_hapi_path
current_hapi_path=$(command -v hapi 2>/dev/null || true)
if [[ -n "$current_hapi_path" && "$current_hapi_path" != "$HAPI_BIN_DIR"* ]]; then
local npm_prefix
npm_prefix=$(npm config get prefix 2>/dev/null || echo "/usr/local")
[[ "$current_hapi_path" == "$npm_prefix"* ]] && hapi_source="npm"
fi
fi
[[ -z "$hapi_source" ]] && hapi_source="unknown"
fi
echo -e " ${BOLD}HAPI CLI:${NC} v$hapi_ver (来源: $hapi_source)"
echo -e " ${BOLD}CLI 路径:${NC} $hapi_path"
echo -e " ${BOLD}Hub 地址:${NC} http://${public_ip}:3006"
echo -e " ${BOLD}Token:${NC} ${YELLOW}$CLI_TOKEN${NC}"