feat: add npm timeout, mirror support, and interactive fallback
- Add --npm-registry param for custom npm mirrors - Add --github-mirror param for GitHub API/download proxy - npm install now has 120s timeout with interactive fallback menu - On timeout/failure, prompt user to switch source or use mirror - Update docs with mirror usage examples via [HAPI](https://hapi.run) Co-Authored-By: HAPI <[email protected]>
This commit is contained in:
+119
-4
@@ -33,6 +33,8 @@
|
||||
.\install.ps1 -Force
|
||||
.\install.ps1 -Upgrade
|
||||
.\install.ps1 -SkipHub
|
||||
.\install.ps1 -NpmRegistry https://registry.npmmirror.com
|
||||
.\install.ps1 -GithubMirror https://ghproxy.com -Source github-official
|
||||
#>
|
||||
|
||||
[CmdletBinding()]
|
||||
@@ -42,7 +44,10 @@ param(
|
||||
|
||||
[switch]$Force,
|
||||
[switch]$Upgrade,
|
||||
[switch]$SkipHub
|
||||
[switch]$SkipHub,
|
||||
|
||||
[string]$NpmRegistry = "",
|
||||
[string]$GithubMirror = ""
|
||||
)
|
||||
|
||||
$ErrorActionPreference = "Stop"
|
||||
@@ -59,8 +64,10 @@ $SOURCE_FILE = "$INSTALL_DIR\.hapi-source"
|
||||
$NODE_MIN_MAJOR = 22
|
||||
|
||||
$NPM_PACKAGE = "@twsxtd/hapi"
|
||||
$NPM_REGISTRY = if ($NpmRegistry) { $NpmRegistry } else { "https://registry.npmjs.org" }
|
||||
$GITHUB_OFFICIAL_REPO = "tiann/hapi"
|
||||
$GITHUB_FORK_REPO = "arkylin/Hapi-Docker"
|
||||
$GITHUB_MIRROR = $GithubMirror
|
||||
|
||||
# ─────────────────────────────────────────
|
||||
# 输出函数
|
||||
@@ -71,6 +78,17 @@ function Write-Ok { param($msg) Write-Host "[OK] $msg" -ForegroundColor Gree
|
||||
function Write-Warn { param($msg) Write-Host "[WARN] $msg" -ForegroundColor Yellow; "[WARN] $msg" | Out-File $LOG_FILE -Append }
|
||||
function Write-Err { param($msg) Write-Host "[ERR] $msg" -ForegroundColor Red; "[ERR] $msg" | Out-File $LOG_FILE -Append }
|
||||
|
||||
# ─────────────────────────────────────────
|
||||
# GitHub 镜像 URL 替换
|
||||
# ─────────────────────────────────────────
|
||||
function Github-Url {
|
||||
param([string]$url)
|
||||
if ($script:GITHUB_MIRROR) {
|
||||
return "$script:GITHUB_MIRROR/$url"
|
||||
}
|
||||
return $url
|
||||
}
|
||||
|
||||
# ─────────────────────────────────────────
|
||||
# 刷新环境变量 PATH
|
||||
# ─────────────────────────────────────────
|
||||
@@ -170,17 +188,23 @@ function Get-RemoteNpmVersion {
|
||||
|
||||
function Get-RemoteGithubVersion {
|
||||
param([string]$repo)
|
||||
$apiUrl = "https://api.github.com/repos/$repo/releases/latest"
|
||||
$apiUrl = Github-Url -url "https://api.github.com/repos/$repo/releases/latest"
|
||||
Write-Info "查询 GitHub Release: $repo ..."
|
||||
if ($script:GITHUB_MIRROR) { Write-Info "使用镜像: $script:GITHUB_MIRROR" }
|
||||
try {
|
||||
$response = Invoke-RestMethod -Uri $apiUrl -TimeoutSec 10 -ErrorAction Stop
|
||||
$ver = $response.tag_name -replace '^v',''
|
||||
Write-Ok "GitHub Release 最新版本: v$ver"
|
||||
return $ver
|
||||
} catch {
|
||||
if ($_.Exception.Response.StatusCode -eq 403) {
|
||||
if ($_.Exception.Response -and $_.Exception.Response.StatusCode -eq 403) {
|
||||
Write-Warn "GitHub API rate limit exceeded,请设置 GITHUB_TOKEN 或稍后重试"
|
||||
} else {
|
||||
Write-Warn "无法连接 GitHub API: $_"
|
||||
if (-not $script:GITHUB_MIRROR) {
|
||||
Write-Info "提示: 如果 GitHub 访问困难,可使用 -GithubMirror 参数指定镜像"
|
||||
Write-Info " 例如: -GithubMirror https://ghproxy.com"
|
||||
}
|
||||
}
|
||||
return $null
|
||||
}
|
||||
@@ -251,6 +275,8 @@ function Match-GithubAsset {
|
||||
}
|
||||
|
||||
Write-Ok "匹配到 asset: $($bestAsset.name)"
|
||||
# 应用镜像到下载 URL
|
||||
$bestAsset.browser_download_url = Github-Url -url $bestAsset.browser_download_url
|
||||
return $bestAsset
|
||||
}
|
||||
|
||||
@@ -343,8 +369,31 @@ function Install-FromGithub {
|
||||
# ─────────────────────────────────────────
|
||||
function Install-FromNpm {
|
||||
Write-Info "从 npm registry 安装 HAPI CLI..."
|
||||
Write-Info "Registry: $script:NPM_REGISTRY"
|
||||
|
||||
npm install -g $script:NPM_PACKAGE --registry=https://registry.npmjs.org | Out-File $LOG_FILE -Append
|
||||
# npm install 带 120 秒超时
|
||||
$npmTimeout = 120
|
||||
Write-Info "npm install 超时设置: ${npmTimeout}s"
|
||||
|
||||
$proc = Start-Process -FilePath "npm" -ArgumentList "install", "-g", $script:NPM_PACKAGE, "--registry=$script:NPM_REGISTRY" -PassThru -WindowStyle Hidden -RedirectStandardOutput $LOG_FILE -RedirectStandardError $LOG_FILE
|
||||
|
||||
$timedOut = $null
|
||||
try {
|
||||
$timedOut = -not $proc.WaitForExit($npmTimeout * 1000)
|
||||
} catch {}
|
||||
|
||||
if ($timedOut) {
|
||||
Stop-Process -Id $proc.Id -Force -ErrorAction SilentlyContinue
|
||||
Write-Warn "npm install 超时 (${npmTimeout}秒),网络可能较慢"
|
||||
Handle-NpmTimeout
|
||||
return $?
|
||||
}
|
||||
|
||||
if ($proc.ExitCode -ne 0) {
|
||||
Write-Warn "npm install 失败 (退出码: $($proc.ExitCode))"
|
||||
Handle-NpmTimeout
|
||||
return $?
|
||||
}
|
||||
|
||||
Update-EnvPath
|
||||
$hapi = Get-Command "hapi" -ErrorAction SilentlyContinue
|
||||
@@ -371,6 +420,71 @@ function Install-FromNpm {
|
||||
return $true
|
||||
}
|
||||
|
||||
# ─────────────────────────────────────────
|
||||
# npm 超时/失败后的处理
|
||||
# ─────────────────────────────────────────
|
||||
function Handle-NpmTimeout {
|
||||
Write-Host ""
|
||||
Write-Warn "npm 安装遇到问题,可能的原因:"
|
||||
Write-Host " 1. 网络较慢或连接不稳定"
|
||||
Write-Host " 2. npm registry 被墙或访问受限"
|
||||
Write-Host ""
|
||||
Write-Info "请选择操作:"
|
||||
Write-Host " 1) 切换到 GitHub Release 安装(无需 npm,推荐国内环境)"
|
||||
Write-Host " 2) 使用国内 npm 镜像重试(如淘宝/中科大镜像)"
|
||||
Write-Host " 3) 跳过 HAPI CLI 安装,继续其他步骤"
|
||||
Write-Host " 4) 退出"
|
||||
Write-Host ""
|
||||
|
||||
do {
|
||||
$choice = Read-Host "请输入选项 [1-4]"
|
||||
switch ($choice) {
|
||||
"1" {
|
||||
Write-Info "切换到 GitHub Release 官方来源..."
|
||||
$script:SELECTED_SOURCE = "github-official"
|
||||
$script:REMOTE_VERSION = Get-RemoteGithubVersion -repo $GITHUB_OFFICIAL_REPO
|
||||
if ($script:REMOTE_VERSION) {
|
||||
Install-FromGithub -repo $GITHUB_OFFICIAL_REPO
|
||||
return $?
|
||||
}
|
||||
return $false
|
||||
}
|
||||
"2" {
|
||||
Write-Host ""
|
||||
Write-Info "可选的国内 npm 镜像:"
|
||||
Write-Host " 1) 淘宝镜像 (registry.npmmirror.com)"
|
||||
Write-Host " 2) 中科大镜像 (mirrors.ustc.edu.cn/npm)"
|
||||
Write-Host " 3) 腾讯云镜像 (mirrors.cloud.tencent.com/npm)"
|
||||
Write-Host " 4) 自定义"
|
||||
Write-Host ""
|
||||
$mirrorChoice = Read-Host "请选择镜像 [1-4]"
|
||||
switch ($mirrorChoice) {
|
||||
"1" { $script:NPM_REGISTRY = "https://registry.npmmirror.com" }
|
||||
"2" { $script:NPM_REGISTRY = "https://mirrors.ustc.edu.cn/npm" }
|
||||
"3" { $script:NPM_REGISTRY = "https://mirrors.cloud.tencent.com/npm" }
|
||||
"4" {
|
||||
$customRegistry = Read-Host "请输入 npm registry 地址"
|
||||
$script:NPM_REGISTRY = $customRegistry
|
||||
}
|
||||
default { $script:NPM_REGISTRY = "https://registry.npmmirror.com" }
|
||||
}
|
||||
Write-Info "使用镜像: $script:NPM_REGISTRY"
|
||||
Install-FromNpm
|
||||
return $?
|
||||
}
|
||||
"3" {
|
||||
Write-Warn "跳过 HAPI CLI 安装"
|
||||
return $true
|
||||
}
|
||||
"4" {
|
||||
Write-Err "安装中止"
|
||||
return $false
|
||||
}
|
||||
default { Write-Warn "无效选项" }
|
||||
}
|
||||
} while ($true)
|
||||
}
|
||||
|
||||
# ─────────────────────────────────────────
|
||||
# 选择安装来源
|
||||
# ─────────────────────────────────────────
|
||||
@@ -765,6 +879,7 @@ function Print-Result {
|
||||
Write-Host " 升级命令:"
|
||||
Write-Host " .\install.ps1 -Upgrade"
|
||||
Write-Host " .\install.ps1 -Source github-official -Upgrade"
|
||||
Write-Host " .\install.ps1 -GithubMirror https://ghproxy.com -Upgrade"
|
||||
Write-Host ""
|
||||
Write-Host " 日志文件: $LOG_FILE"
|
||||
Write-Host ""
|
||||
|
||||
Reference in New Issue
Block a user