refactor: unified status + menu flow, removed run_quick_mode, cleaner status page and menus for both scripts

This commit is contained in:
i
2026-06-02 00:58:09 +08:00
parent 01792bbac4
commit 329f7722b3
2 changed files with 386 additions and 720 deletions
+200 -346
View File
@@ -726,54 +726,41 @@ function Collect-Status {
# 打印状态面板 # 打印状态面板
# ───────────────────────────────────────── # ─────────────────────────────────────────
function Print-Status { function Print-Status {
$remoteHint = ""
if ($script:STATUS_REMOTE_VERSION) {
$remoteHint = " (最新: v$($script:STATUS_REMOTE_VERSION))"
}
$cliStatus = "未安装" $cliStatus = "未安装"
$cliColor = "Red" $cliDetail = ""
if ($script:STATUS_CLI_INSTALLED) {
$cliStatus = "已安装 v$($script:STATUS_CLI_VERSION)"
$cliColor = "Green"
}
$autoHub = "未设置"
$autoRunner = "未设置"
if ($script:STATUS_AUTOSTART_HUB) {
$autoHub = $script:STATUS_AUTOSTART_HUB
}
if ($script:STATUS_AUTOSTART_RUNNER) {
$autoRunner = $script:STATUS_AUTOSTART_RUNNER
}
Write-Host ""
Write-Host "╔══════════════════════════════════════════════════════════════════╗" -ForegroundColor White
Write-Host "║ HAPI 系统状态 ║" -ForegroundColor White
Write-Host "╠══════════════════════════════════════════════════════════════════╣" -ForegroundColor White
Write-Host "║ HAPI CLI ║" -ForegroundColor White
$cliLine = "║ 状态: $cliStatus$remoteHint"
$cliLine = $cliLine.PadRight(67) + "║"
Write-Host $cliLine -ForegroundColor White
if ($script:STATUS_CLI_INSTALLED) { if ($script:STATUS_CLI_INSTALLED) {
$srcDisplay = $script:STATUS_CLI_SOURCE $srcDisplay = $script:STATUS_CLI_SOURCE
if ($srcDisplay -like "github:*") { $srcDisplay = $srcDisplay.Substring(7) } if ($srcDisplay -like "github:*") { $srcDisplay = $srcDisplay.Substring(7) }
$srcLine = "║ 来源: $srcDisplay" $cliStatus = "已安装 v$($script:STATUS_CLI_VERSION)"
$srcLine = $srcLine.PadRight(67) + "║" $cliDetail = " 来源: $srcDisplay`n 路径: $($script:STATUS_CLI_PATH)"
Write-Host $srcLine -ForegroundColor White if ($script:STATUS_REMOTE_VERSION) {
$pathLine = "║ 路径: $($script:STATUS_CLI_PATH)" $cmp = Compare-Versions -v1 $script:STATUS_CLI_VERSION -v2 $script:STATUS_REMOTE_VERSION
$pathLine = $pathLine.PadRight(67) + "║" if ($cmp -eq 2) {
Write-Host $pathLine -ForegroundColor White $cliStatus = "已安装 v$($script:STATUS_CLI_VERSION) (可升级: v$($script:STATUS_REMOTE_VERSION))"
} elseif ($cmp -eq 0) {
$cliStatus = "已安装 v$($script:STATUS_CLI_VERSION) (最新)"
}
}
} }
Write-Host "║ ║" -ForegroundColor White
Write-Host "║ 开机自启 ║" -ForegroundColor White $autoStatus = "未设置"
$ahLine = "║ Hub: $autoHub" if ($script:STATUS_AUTOSTART_HUB -or $script:STATUS_AUTOSTART_RUNNER) {
$ahLine = $ahLine.PadRight(67) + "║" $hubState = if ($script:STATUS_AUTOSTART_HUB) { $script:STATUS_AUTOSTART_HUB } else { "None" }
Write-Host $ahLine -ForegroundColor White $runnerState = if ($script:STATUS_AUTOSTART_RUNNER) { $script:STATUS_AUTOSTART_RUNNER } else { "None" }
$arLine = "║ Runner: $autoRunner" $autoStatus = "已配置 (hub: $hubState, runner: $runnerState)"
$arLine = $arLine.PadRight(67) + "║" }
Write-Host $arLine -ForegroundColor White
Write-Host "╚══════════════════════════════════════════════════════════════════╝" -ForegroundColor White Write-Host ""
Write-Host "━━━ HAPI 系统状态 ─────────────────────────────────" -ForegroundColor White
Write-Host ""
Write-Host " HAPI CLI" -ForegroundColor White
Write-Host " 状态: $cliStatus"
if ($cliDetail) { Write-Host "$cliDetail" }
Write-Host ""
Write-Host " 开机自启" -ForegroundColor White
Write-Host " 状态: $autoStatus"
Write-Host ""
Write-Host "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━" -ForegroundColor White
} }
# ───────────────────────────────────────── # ─────────────────────────────────────────
@@ -808,10 +795,6 @@ function Main-Menu {
$i++ $i++
} }
Write-Host ""
Write-Host " $i) 配置 HAPI Hub" -ForegroundColor Yellow
$options += "hub"
$i++
Write-Host "" Write-Host ""
if ($script:STATUS_AUTOSTART_HUB -or $script:STATUS_AUTOSTART_RUNNER) { if ($script:STATUS_AUTOSTART_HUB -or $script:STATUS_AUTOSTART_RUNNER) {
@@ -819,8 +802,8 @@ function Main-Menu {
$options += "disable_autostart" $options += "disable_autostart"
$i++ $i++
} else { } else {
Write-Host " $i) 设置开机自启" Write-Host " $i) 启用开机自启"
$options += "setup_autostart" $options += "enable_autostart"
$i++ $i++
} }
@@ -833,182 +816,55 @@ function Main-Menu {
do { do {
$choice = Read-Host "请输入选项 [0-$($i-1)]" $choice = Read-Host "请输入选项 [0-$($i-1)]"
if ($choice -eq "0") { Write-Host ""; Write-Info "退出"; exit 0 }
if ($choice -eq "0") { if (-not ($choice -match '^\d+$')) { Write-Warn "无效选项"; continue }
Write-Host ""
Write-Info "退出"
exit 0
}
if (-not ($choice -match '^\d+$')) {
Write-Warn "无效选项"
continue
}
$idx = [int]$choice - 1 $idx = [int]$choice - 1
if ($idx -lt 0 -or $idx -ge $options.Count) { if ($idx -lt 0 -or $idx -ge $options.Count) { Write-Warn "无效选项"; continue }
Write-Warn "无效选项"
continue
}
$action = $options[$idx] $action = $options[$idx]
break break
} while ($true) } while ($true)
Write-Host "" Write-Host ""
switch ($action) { switch ($action) {
"install" { Execute-Install } "install" {
"reinstall" { Execute-Install } Select-InstallSource
"upgrade" { Execute-Upgrade } $script:Source = $script:SELECTED_SOURCE
"uninstall" { Execute-Uninstall } Invoke-Install | Out-Null
"hub" { Execute-HubSetup }
"setup_autostart" { Execute-SetupAutoStart }
"disable_autostart" { Execute-DisableAutoStart }
"refresh" {
Collect-Status Collect-Status
}
}
}
# ─────────────────────────────────────────
# 菜单操作:安装
# ─────────────────────────────────────────
function Execute-Install {
Select-InstallSource
Write-Info "安装来源: $SELECTED_SOURCE"
$script:REMOTE_VERSION = Get-RemoteVersion -source $SELECTED_SOURCE
if (-not $script:REMOTE_VERSION) {
Write-Err "无法获取远程版本"
Read-Host "按 Enter 继续..."
return
}
$script:STATUS_REMOTE_VERSION = $script:REMOTE_VERSION
if ($SELECTED_SOURCE -eq "npm") {
Install-NodeJS -forSource "npm"
}
$ok = $false
switch ($SELECTED_SOURCE) {
"npm" { $ok = Install-FromNpm }
"github-official" {
$ok = Install-FromGithub -repo $GITHUB_OFFICIAL_REPO
if (-not $ok) {
Write-Warn "官方仓库无匹配 asset,尝试本仓库..."
$ok = Install-FromGithub -repo $GITHUB_FORK_REPO
}
}
"github-fork" { $ok = Install-FromGithub -repo $GITHUB_FORK_REPO }
}
if (-not $ok) {
Write-Err "HAPI CLI 安装失败"
}
Collect-Status
Read-Host "按 Enter 继续..."
}
# ─────────────────────────────────────────
# 菜单操作:升级
# ─────────────────────────────────────────
function Execute-Upgrade {
if (-not $script:STATUS_REMOTE_VERSION) {
$upgradeSource = $script:STATUS_CLI_SOURCE
if ($upgradeSource -like "github:*") { $upgradeSource = $upgradeSource.Substring(7) }
switch ($upgradeSource) {
"npm" { $script:SELECTED_SOURCE = "npm" }
"tiann/hapi" { $script:SELECTED_SOURCE = "github-official" }
"arkylin/Hapi-Docker" { $script:SELECTED_SOURCE = "github-fork" }
default { $script:SELECTED_SOURCE = "npm" }
}
$script:REMOTE_VERSION = Get-RemoteVersion -source $script:SELECTED_SOURCE
if (-not $script:REMOTE_VERSION) {
Write-Err "无法获取远程版本"
Read-Host "按 Enter 继续..." Read-Host "按 Enter 继续..."
return
} }
$script:STATUS_REMOTE_VERSION = $script:REMOTE_VERSION "reinstall" {
} Select-InstallSource
$script:Source = $script:SELECTED_SOURCE
$cmp = Compare-Versions -v1 $script:STATUS_CLI_VERSION -v2 $script:STATUS_REMOTE_VERSION Invoke-Install | Out-Null
if ($cmp -ne 2) { Collect-Status
Write-Ok "当前已是最新版本 (v$($script:STATUS_CLI_VERSION))" Read-Host "按 Enter 继续..."
Read-Host "按 Enter 继续..."
return
}
Write-Info "升级: v$($script:STATUS_CLI_VERSION) -> v$($script:STATUS_REMOTE_VERSION)"
if ($script:SELECTED_SOURCE -eq "npm") {
Install-NodeJS -forSource "npm"
}
$ok = $false
switch ($script:SELECTED_SOURCE) {
"npm" { $ok = Install-FromNpm }
"github-official" {
$ok = Install-FromGithub -repo $GITHUB_OFFICIAL_REPO
if (-not $ok) {
Write-Warn "官方仓库无匹配 asset,尝试本仓库..."
$ok = Install-FromGithub -repo $GITHUB_FORK_REPO
}
} }
"github-fork" { $ok = Install-FromGithub -repo $GITHUB_FORK_REPO } "upgrade" {
Invoke-Upgrade | Out-Null
Collect-Status
Read-Host "按 Enter 继续..."
}
"uninstall" {
Write-Warn "警告: 这将删除 HAPI CLI"
$confirm = Read-Host "确认卸载? [y/N]"
if ($confirm -match '^[Yy]$') { Uninstall-Hapi } else { Write-Info "已取消" }
Collect-Status
Read-Host "按 Enter 继续..."
}
"enable_autostart" {
if (-not $script:STATUS_HUB_CONFIGURED) { Write-Warn "Hub 未配置,先执行配置"; Setup-Hub }
Setup-AutoStart
Collect-Status
Read-Host "按 Enter 继续..."
}
"disable_autostart" {
Disable-AutoStart
Collect-Status
Read-Host "按 Enter 继续..."
}
"refresh" { Collect-Status }
} }
if (-not $ok) {
Write-Err "HAPI CLI 升级失败"
}
Collect-Status
Read-Host "按 Enter 继续..."
}
# ─────────────────────────────────────────
# 菜单操作:卸载
# ─────────────────────────────────────────
function Execute-Uninstall {
Write-Warn "警告: 这将删除 HAPI CLI"
$confirm = Read-Host "确认卸载? [y/N]"
if ($confirm -match '^[Yy]$') {
Uninstall-Hapi
} else {
Write-Info "已取消"
}
Collect-Status
Read-Host "按 Enter 继续..."
}
# ─────────────────────────────────────────
# 菜单操作:Hub 相关
# ─────────────────────────────────────────
function Execute-HubSetup {
Setup-Hub
Collect-Status
Read-Host "按 Enter 继续..."
}
# ─────────────────────────────────────────
# 菜单操作:开机自启
# ─────────────────────────────────────────
function Execute-SetupAutoStart {
if (-not $script:STATUS_HUB_CONFIGURED) {
Write-Warn "Hub 未配置,先执行配置"
Setup-Hub
}
Setup-AutoStart
Collect-Status
Read-Host "按 Enter 继续..."
}
function Execute-DisableAutoStart {
Disable-AutoStart
Collect-Status
Read-Host "按 Enter 继续..."
} }
# ───────────────────────────────────────── # ─────────────────────────────────────────
@@ -1030,7 +886,7 @@ function Check-Prerequisites {
# ───────────────────────────────────────── # ─────────────────────────────────────────
# 快速模式:向后兼容的命令行参数 # 快速模式:向后兼容的命令行参数
# ───────────────────────────────────────── # ─────────────────────────────────────────
function Run-QuickMode { function Resolve-InstallSource {
if ($Source) { if ($Source) {
$script:SELECTED_SOURCE = $Source $script:SELECTED_SOURCE = $Source
} else { } else {
@@ -1040,109 +896,77 @@ function Run-QuickMode {
default { "npm" } default { "npm" }
} }
} }
}
$script:REMOTE_VERSION = Get-RemoteVersion -source $script:SELECTED_SOURCE function Resolve-RemoteVersion {
while (-not $script:REMOTE_VERSION) { while (-not $script:REMOTE_VERSION) {
Write-Warn "无法从 $($script:SELECTED_SOURCE) 获取远程版本" Write-Warn "无法从 $($script:SELECTED_SOURCE) 获取远程版本"
Write-Host "" if (-not $host.UI.RawUI.KeyAvailable -and -not $isIexExecution) {
Write-Host "请选择操作:" -ForegroundColor Yellow # 交互式
Write-Host " 1) 配置镜像重试 ($($script:SELECTED_SOURCE))" Write-Host ""
Write-Host " 2) 切换其他来源" Write-Host "请选择操作:" -ForegroundColor Yellow
Write-Host " 3) 跳过版本检查,直接安装" Write-Host " 1) 配置镜像重试 ($($script:SELECTED_SOURCE))"
Write-Host " 0) 退出" Write-Host " 2) 切换其他来源"
Write-Host "" Write-Host " 0) 退出"
$choice = Read-Host "请输入选项 [0-3]" Write-Host ""
switch ($choice) { $choice = Read-Host "请输入选项 [0-2]"
"1" { switch ($choice) {
if ($script:SELECTED_SOURCE -eq "npm") { "1" {
$reg = Read-Host "请输入 npm registry URL (Enter 使用默认 https://registry.npmmirror.com)" if ($script:SELECTED_SOURCE -eq "npm") {
$script:NPM_REGISTRY = if ($reg) { $reg } else { "https://registry.npmmirror.com" } $reg = Read-Host "请输入 npm registry URL (Enter 使用默认 https://registry.npmmirror.com)"
Write-Ok "npm registry 已设置为: $script:NPM_REGISTRY" $script:NPM_REGISTRY = if ($reg) { $reg } else { "https://registry.npmmirror.com" }
} else { Write-Ok "npm registry 已设置为: $script:NPM_REGISTRY"
$mirror = Read-Host "请输入 GitHub 镜像 URL (Enter 使用默认 https://ghproxy.com)" } else {
$script:GITHUB_MIRROR = if ($mirror) { $mirror } else { "https://ghproxy.com" } $mirror = Read-Host "请输入 GitHub 镜像 URL (Enter 使用默认 https://ghproxy.com)"
Write-Ok "GitHub 镜像已设置为: $script:GITHUB_MIRROR" $script:GITHUB_MIRROR = if ($mirror) { $mirror } else { "https://ghproxy.com" }
} Write-Ok "GitHub 镜像已设置为: $script:GITHUB_MIRROR"
$script:REMOTE_VERSION = Get-RemoteVersion -source $script:SELECTED_SOURCE
if (-not $script:REMOTE_VERSION) {
Write-Warn "使用镜像后仍无法获取版本,请检查 URL 是否正确"
}
}
"2" {
$allSources = @("npm", "github-official", "github-fork")
$found = $false
foreach ($fs in $allSources) {
if ($fs -eq $script:SELECTED_SOURCE) { continue }
Write-Info "尝试从 $fs 获取..."
$ver = Get-RemoteVersion -source $fs
if ($ver) {
$script:SELECTED_SOURCE = $fs
$script:REMOTE_VERSION = $ver
Write-Ok "从 $fs 获取版本成功: v$($script:REMOTE_VERSION)"
$found = $true
break
} }
} }
if (-not $found) { "2" {
Write-Warn "所有来源均无法获取版本" $found = $false
foreach ($fs in @("npm", "github-official", "github-fork")) {
if ($fs -eq $script:SELECTED_SOURCE) { continue }
Write-Info "尝试从 $fs 获取..."
$ver = Get-RemoteVersion -source $fs
if ($ver) {
$script:SELECTED_SOURCE = $fs
$script:REMOTE_VERSION = $ver
Write-Ok "从 $fs 获取版本成功: v$($script:REMOTE_VERSION)"
$found = $true
break
}
}
if (-not $found) { Write-Warn "所有来源均无法获取版本" }
}
"0" { exit 1 }
default { Write-Warn "无效选项,请重新输入" }
}
} else {
# 非交互式 - 自动尝试其他来源
$found = $false
foreach ($fs in @("npm", "github-official", "github-fork")) {
if ($fs -eq $script:SELECTED_SOURCE) { continue }
Write-Info "尝试 $fs..."
$ver = Get-RemoteVersion -source $fs
if ($ver) {
$script:SELECTED_SOURCE = $fs
$script:REMOTE_VERSION = $ver
$found = $true
break
} }
} }
"3" { break } if (-not $found) {
"0" { exit 1 } Write-Err "所有来源均无法获取版本,退出"
default { Write-Warn "无效选项,请重新输入" } exit 1
} }
Write-Host ""
}
Detect-Installed
if ($Upgrade) {
if (-not $script:INSTALLED_VERSION -or $script:INSTALLED_VERSION -eq "unknown") {
Write-Err "未检测到已安装的 HAPI CLI,无法升级"
exit 1
}
$cmp = Compare-Versions -v1 $script:INSTALLED_VERSION -v2 $script:REMOTE_VERSION
if ($cmp -ne 2) {
Write-Ok "当前已是最新版本 (v$($script:INSTALLED_VERSION))"
exit 0
}
Write-Info "升级: v$($script:INSTALLED_VERSION) -> v$($script:REMOTE_VERSION)"
}
if ($script:INSTALLED_VERSION -and $script:INSTALLED_VERSION -ne "unknown" -and -not $Force -and -not $Upgrade) {
$cmp = Compare-Versions -v1 $script:INSTALLED_VERSION -v2 $script:REMOTE_VERSION
if ($cmp -eq 0) {
Write-Ok "当前已是最新版本 (v$($script:INSTALLED_VERSION))"
exit 0
} }
} }
$script:STATUS_REMOTE_VERSION = $script:REMOTE_VERSION
}
if (-not $Force -and -not $Upgrade) { function Invoke-Install {
$action = if ($script:INSTALLED_VERSION) { "覆盖安装" } else { "安装" } Resolve-InstallSource
$srcLabel = switch ($script:SELECTED_SOURCE) { Resolve-RemoteVersion
"npm" { "npm registry" }
"github-official" { "GitHub (tiann/hapi)" }
"github-fork" { "GitHub (arkylin/Hapi-Docker)" }
}
Write-Host ""
Write-Host "即将${action} HAPI CLI:" -ForegroundColor White
Write-Host " 版本: v$($script:REMOTE_VERSION)" -ForegroundColor Cyan
Write-Host " 来源: $srcLabel" -ForegroundColor Cyan
$installDirDisplay = if ($script:SELECTED_SOURCE -eq "npm" -and $script:INSTALLED_SOURCE -eq "npm") {
$npmRoot = & npm root -g 2>$null
if (-not $npmRoot) { $npmRoot = if ($IsWindows) { "$env:APPDATA\npm\node_modules" } else { "/usr/lib/node_modules" } }
Join-Path $npmRoot $NPM_PACKAGE
} else { $script:INSTALL_DIR }
Write-Host " 目录: $installDirDisplay" -ForegroundColor Cyan
Write-Host ""
$confirm = Read-Host "确认${action}? [Y/n]"
if ($confirm -match '^[Nn]') {
Write-Info "已取消"
exit 0
}
Write-Host ""
}
if ($script:SELECTED_SOURCE -eq "npm") { if ($script:SELECTED_SOURCE -eq "npm") {
Install-NodeJS -forSource "npm" Install-NodeJS -forSource "npm"
@@ -1163,13 +987,55 @@ function Run-QuickMode {
if (-not $ok) { if (-not $ok) {
Write-Err "HAPI CLI 安装失败" Write-Err "HAPI CLI 安装失败"
exit 1 return $false
} }
if (-not $SkipHub) { if (-not $SkipHub) {
Setup-Hub Setup-Hub
Setup-AutoStart Setup-AutoStart
} }
return $true
}
function Invoke-Upgrade {
Resolve-InstallSource
Resolve-RemoteVersion
$cmp = Compare-Versions -v1 $script:INSTALLED_VERSION -v2 $script:REMOTE_VERSION
if ($cmp -ne 2) {
Write-Ok "当前已是最新版本 (v$($script:INSTALLED_VERSION))"
return $true
}
Write-Info "升级: v$($script:INSTALLED_VERSION) -> v$($script:REMOTE_VERSION)"
if ($script:SELECTED_SOURCE -eq "npm") {
Install-NodeJS -forSource "npm"
}
$ok = $false
switch ($script:SELECTED_SOURCE) {
"npm" { $ok = Install-FromNpm }
"github-official" {
$ok = Install-FromGithub -repo $GITHUB_OFFICIAL_REPO
if (-not $ok) {
Write-Warn "官方仓库无匹配 asset,尝试本仓库..."
$ok = Install-FromGithub -repo $GITHUB_FORK_REPO
}
}
"github-fork" { $ok = Install-FromGithub -repo $GITHUB_FORK_REPO }
}
if (-not $ok) {
Write-Err "HAPI CLI 升级失败"
return $false
}
if (-not $SkipHub) {
Setup-Hub
Setup-AutoStart
}
return $true
} }
# ───────────────────────────────────────── # ─────────────────────────────────────────
@@ -1180,48 +1046,36 @@ function Run-QuickMode {
# 检测是否是 iex / 管道执行(如 irm | iex) # 检测是否是 iex / 管道执行(如 irm | iex)
$isIexExecution = -not $PSCommandPath $isIexExecution = -not $PSCommandPath
# 如果带有旧版参数,或是 iex 执行,进入快速模式
if ($Upgrade -or $Force -or $Source -or $SkipHub -or $InstallDir -or $isIexExecution) {
Write-Host ""
Write-Host "╔══════════════════════════════════════════════════════════════════╗" -ForegroundColor White
Write-Host "║ HAPI 快速模式 ║" -ForegroundColor White
Write-Host "╚══════════════════════════════════════════════════════════════════╝" -ForegroundColor White
Write-Host ""
Setup-InstallDir
Detect-Installed
if ($script:INSTALLED_VERSION) {
if ($script:INSTALLED_SOURCE -eq "npm") {
$npmRoot = & npm root -g 2>$null
if (-not $npmRoot) { $npmRoot = if ($IsWindows) { "$env:APPDATA\npm\node_modules" } else { "/usr/lib/node_modules" } }
$npmPkgDir = Join-Path $npmRoot $NPM_PACKAGE
Write-Ok "HAPI CLI v$($script:INSTALLED_VERSION) (npm: $npmPkgDir)"
} else {
$srcDisplay = $script:INSTALLED_SOURCE
if ($srcDisplay -like "github:*") { $srcDisplay = $srcDisplay.Substring(7) }
$hapiPath = (Get-Command hapi -ErrorAction SilentlyContinue).Source
Write-Ok "HAPI CLI v$($script:INSTALLED_VERSION) (来源: $srcDisplay, 目录: $($script:INSTALL_DIR))"
}
} else {
Write-Ok "安装目录: $($script:INSTALL_DIR)"
}
Check-Prerequisites
Run-QuickMode
exit 0
}
Write-Host ""
Write-Host "╔══════════════════════════════════════════════════════════════════╗" -ForegroundColor White
Write-Host "║ HAPI 管理脚本 (Windows) ║" -ForegroundColor White
Write-Host "║ 状态查看 | 安装升级 | 开机自启 | Hub 管理 ║" -ForegroundColor White
Write-Host "╚══════════════════════════════════════════════════════════════════╝" -ForegroundColor White
Write-Host ""
Setup-InstallDir Setup-InstallDir
Write-Ok "安装目录: $($script:INSTALL_DIR)"
Check-Prerequisites Check-Prerequisites
Collect-Status Collect-Status
Write-Host ""
Write-Host "╔══════════════════════════════════════════════════════════════════╗" -ForegroundColor White
Write-Host "║ HAPI 系统状态 ║" -ForegroundColor White
Write-Host "╚══════════════════════════════════════════════════════════════════╝" -ForegroundColor White
Write-Host ""
Print-Status
# 非交互模式(irm | iex)
if ($isIexExecution) {
if ($Upgrade) {
Invoke-Upgrade
exit
}
if ($Force -or $Source -or $SkipHub -or $InstallDir) {
Invoke-Install
exit
}
if (-not $script:STATUS_CLI_INSTALLED) {
Write-Host ""
Write-Warn "HAPI CLI 未安装"
Write-Warn "请使用 -Source / -Force 等参数指定操作,或直接运行 install.ps1 进入交互模式"
}
exit
}
# 交互菜单
while ($true) { while ($true) {
Print-Status
Main-Menu Main-Menu
} }
+186 -374
View File
@@ -1012,57 +1012,51 @@ collect_status() {
# 打印状态面板 # 打印状态面板
# ───────────────────────────────────────── # ─────────────────────────────────────────
print_status() { print_status() {
local remote_hint="" local cli_status="${RED}未安装${NC}"
if [[ -n "$STATUS_REMOTE_VERSION" ]]; then local cli_detail=""
remote_hint=" (最新: v$STATUS_REMOTE_VERSION)"
fi
local cli_status="未安装"
local cli_color=$RED
if [[ "$STATUS_CLI_INSTALLED" == true ]]; then
cli_status="已安装 v$STATUS_CLI_VERSION"
cli_color=$GREEN
fi
local autostart_hub="未设置"
local autostart_runner="未设置"
if [[ -n "$STATUS_AUTOSTART_HUB_ENABLED" ]]; then
local hub_active_color=$GREEN
[[ "$STATUS_AUTOSTART_HUB_ACTIVE" != "active" ]] && hub_active_color=$RED
autostart_hub="${STATUS_AUTOSTART_HUB_ENABLED} (${hub_active_color}${STATUS_AUTOSTART_HUB_ACTIVE}${NC})"
fi
if [[ -n "$STATUS_AUTOSTART_RUNNER_ENABLED" ]]; then
local runner_active_color=$GREEN
[[ "$STATUS_AUTOSTART_RUNNER_ACTIVE" != "active" ]] && runner_active_color=$RED
autostart_runner="${STATUS_AUTOSTART_RUNNER_ENABLED} (${runner_active_color}${STATUS_AUTOSTART_RUNNER_ACTIVE}${NC})"
fi
local user_info=""
if [[ "$IS_ROOT" == true ]]; then
user_info="root"
else
user_info="$(whoami)"
fi
echo ""
echo -e "${BOLD}╔══════════════════════════════════════════════════════════════════╗${NC}"
echo -e "${BOLD}║${NC} ${BOLD}HAPI 系统状态${NC} ${BOLD}║${NC}"
echo -e "${BOLD}╠══════════════════════════════════════════════════════════════════╣${NC}"
echo -e "${BOLD}║${NC} ${DIM}用户: $user_info | 目录: $INSTALL_DIR${NC} ${BOLD}║${NC}"
echo -e "${BOLD}║${NC} ${BOLD}║${NC}"
echo -e "${BOLD}║${NC} ${BOLD}HAPI CLI${NC} ${BOLD}║${NC}"
echo -e "${BOLD}║${NC} 状态: ${cli_color}${cli_status}${NC}${remote_hint} ${BOLD}║${NC}"
if [[ "$STATUS_CLI_INSTALLED" == true ]]; then if [[ "$STATUS_CLI_INSTALLED" == true ]]; then
local src_display="$STATUS_CLI_SOURCE" local src_display="$STATUS_CLI_SOURCE"
[[ "$src_display" == github:* ]] && src_display="${src_display#github:}" [[ "$src_display" == github:* ]] && src_display="${src_display#github:}"
echo -e "${BOLD}║${NC} 来源: ${src_display} ${BOLD}║${NC}" cli_status="${GREEN}已安装 v$STATUS_CLI_VERSION${NC}"
echo -e "${BOLD}║${NC} 路径: ${STATUS_CLI_PATH} ${BOLD}║${NC}" cli_detail=" 来源: $src_display
路径: $STATUS_CLI_PATH"
if [[ -n "$STATUS_REMOTE_VERSION" ]]; then
local cmp
cmp=$(compare_versions "$STATUS_CLI_VERSION" "$STATUS_REMOTE_VERSION")
if [[ "$cmp" == "2" ]]; then
cli_status="${GREEN}已安装 v$STATUS_CLI_VERSION${NC} ${YELLOW}(可升级: v$STATUS_REMOTE_VERSION)${NC}"
elif [[ "$cmp" == "0" ]]; then
cli_status="${GREEN}已安装 v$STATUS_CLI_VERSION${NC} ${CYAN}(最新)${NC}"
fi
fi
fi fi
echo -e "${BOLD}║${NC} ${BOLD}║${NC}"
echo -e "${BOLD}║${NC} ${BOLD}开机自启${NC} ${BOLD}║${NC}" local autostart_status="未设置"
echo -e "${BOLD}║${NC} Hub: ${autostart_hub} ${BOLD}║${NC}" if [[ "$IS_ROOT" == true ]]; then
echo -e "${BOLD}║${NC} Runner: ${autostart_runner} ${BOLD}║${NC}" if [[ "$STATUS_AUTOSTART_HUB_ENABLED" == "enabled" || "$STATUS_AUTOSTART_RUNNER_ENABLED" == "enabled" ]]; then
echo -e "${BOLD}╚══════════════════════════════════════════════════════════════════╝${NC}" local hub_st="${STATUS_AUTOSTART_HUB_ACTIVE:-unknown}"
local runner_st="${STATUS_AUTOSTART_RUNNER_ACTIVE:-unknown}"
[[ "$hub_st" == "active" ]] && hub_st="${GREEN}active${NC}" || hub_st="${RED}$hub_st${NC}"
[[ "$runner_st" == "active" ]] && runner_st="${GREEN}active${NC}" || runner_st="${RED}$runner_st${NC}"
autostart_status="${GREEN}已启用${NC} (hub: $hub_st, runner: $runner_st)"
else
autostart_status="${YELLOW}未启用${NC}"
fi
fi
echo ""
echo -e "${BOLD}━━━ HAPI 系统状态 ─────────────────────────────────${NC}"
echo ""
echo -e " ${BOLD}HAPI CLI${NC}"
echo -e " 状态: $cli_status"
if [[ -n "$cli_detail" ]]; then
echo -e "$cli_detail"
fi
echo ""
echo -e " ${BOLD}开机自启${NC}"
echo -e " 状态: $autostart_status"
echo ""
echo -e "${BOLD}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
} }
# ───────────────────────────────────────── # ─────────────────────────────────────────
@@ -1099,22 +1093,15 @@ main_menu() {
fi fi
echo "" echo ""
if [[ "$IS_ROOT" == true ]]; then if [[ "$IS_ROOT" == true ]]; then
if [[ -n "$STATUS_AUTOSTART_HUB_ENABLED" || -n "$STATUS_AUTOSTART_RUNNER_ENABLED" ]]; then if [[ "$STATUS_AUTOSTART_HUB_ENABLED" == "enabled" || "$STATUS_AUTOSTART_RUNNER_ENABLED" == "enabled" ]]; then
if [[ "$STATUS_AUTOSTART_HUB_ENABLED" == "enabled" || "$STATUS_AUTOSTART_RUNNER_ENABLED" == "enabled" ]]; then echo " $i) 禁用开机自启"
echo " $i) 禁用开机自启" options+=("disable_autostart")
options+=("disable_autostart")
else
echo " $i) 启用开机自启"
options+=("enable_autostart")
fi
((i++))
else else
echo " $i) 设置开机自启" echo " $i) 启用开机自启"
options+=("setup_autostart") options+=("enable_autostart")
((i++))
fi fi
((i++))
else else
echo -e " ${DIM}开机自启 (需要 root)${NC}" echo -e " ${DIM}开机自启 (需要 root)${NC}"
fi fi
@@ -1128,205 +1115,38 @@ main_menu() {
while true; do while true; do
read -rp "请输入选项 [0-$((i-1))]: " choice read -rp "请输入选项 [0-$((i-1))]: " choice
if [[ "$choice" == "0" ]]; then if [[ "$choice" == "0" ]]; then
echo "" echo ""
info "退出" info "退出"
exit 0 exit 0
fi fi
if [[ -z "$choice" ]] || ! [[ "$choice" =~ ^[0-9]+$ ]]; then if [[ -z "$choice" ]] || ! [[ "$choice" =~ ^[0-9]+$ ]]; then
warn "无效选项" warn "无效选项"
continue continue
fi fi
local idx=$((choice - 1)) local idx=$((choice - 1))
if [[ $idx -lt 0 || $idx -ge ${#options[@]} ]]; then if [[ $idx -lt 0 || $idx -ge ${#options[@]} ]]; then
warn "无效选项" warn "无效选项"
continue continue
fi fi
local action="${options[$idx]}" local action="${options[$idx]}"
break break
done done
echo "" echo ""
case "$action" in case "$action" in
install|reinstall) install|reinstall)
select_source select_source
info "安装来源: $SELECTED_SOURCE" SOURCE="$SELECTED_SOURCE"
perform_install || true
while ! get_remote_version "$SELECTED_SOURCE"; do
warn "无法从 $SELECTED_SOURCE 获取远程版本"
echo ""
echo -e "${YELLOW}请选择操作:${NC}"
echo " 1) 配置镜像重试 ($SELECTED_SOURCE)"
echo " 2) 切换其他来源"
echo " 3) 跳过版本检查,直接安装"
echo " 0) 退出"
echo ""
read -rp "请输入选项 [0-3]: " choice
case "$choice" in
1)
if [[ "$SELECTED_SOURCE" == "npm" ]]; then
read -rp "请输入 npm registry URL (Enter 使用默认 https://registry.npmmirror.com): " reg
NPM_REGISTRY="${reg:-https://registry.npmmirror.com}"
ok "npm registry 已设置为: $NPM_REGISTRY"
else
read -rp "请输入 GitHub 镜像 URL (Enter 使用默认 https://ghproxy.com): " mirror
GITHUB_MIRROR="${mirror:-https://ghproxy.com}"
ok "GitHub 镜像已设置为: $GITHUB_MIRROR"
fi
;;
2)
local found=false
for fs in npm github-official github-fork; do
[[ "$fs" == "$SELECTED_SOURCE" ]] && continue
info "尝试从 $fs 获取..."
if get_remote_version "$fs"; then
SELECTED_SOURCE="$fs"
ok "从 $fs 获取版本成功: v$REMOTE_VERSION"
found=true
break
fi
done
if [[ "$found" != true ]]; then
warn "所有来源均无法获取版本"
fi
;;
3) break ;;
0) exit 1 ;;
*) warn "无效选项,请重新输入" ;;
esac
echo ""
done
STATUS_REMOTE_VERSION="$REMOTE_VERSION"
if [[ "$SELECTED_SOURCE" == "npm" ]]; then
install_nodejs
fi
local ok=false
case "$SELECTED_SOURCE" in
npm)
install_from_npm && ok=true || ok=false
;;
github-official)
if install_from_github "$GITHUB_OFFICIAL_REPO"; then
ok=true
else
warn "官方仓库无匹配 asset,尝试本仓库..."
install_from_github "$GITHUB_FORK_REPO" && ok=true || ok=false
fi
;;
github-fork)
install_from_github "$GITHUB_FORK_REPO" && ok=true || ok=false
;;
esac
if [[ "$ok" != true ]]; then
err "HAPI CLI 安装失败"
fi
collect_status collect_status
read -rp "按 Enter 继续..." read -rp "按 Enter 继续..."
;; ;;
upgrade) upgrade)
if [[ -z "$STATUS_REMOTE_VERSION" ]]; then perform_upgrade || true
local upgrade_source="$STATUS_CLI_SOURCE"
[[ "$upgrade_source" == github:* ]] && upgrade_source="${upgrade_source#github:}"
[[ "$upgrade_source" == "npm" ]] && SELECTED_SOURCE="npm"
[[ "$upgrade_source" == "tiann/hapi" ]] && SELECTED_SOURCE="github-official"
[[ "$upgrade_source" == "arkylin/Hapi-Docker" ]] && SELECTED_SOURCE="github-fork"
[[ -z "$SELECTED_SOURCE" ]] && SELECTED_SOURCE="npm"
while ! get_remote_version "$SELECTED_SOURCE"; do
warn "无法从 $SELECTED_SOURCE 获取远程版本"
echo ""
echo -e "${YELLOW}请选择操作:${NC}"
echo " 1) 配置镜像重试 ($SELECTED_SOURCE)"
echo " 2) 切换其他来源"
echo " 0) 退出"
echo ""
read -rp "请输入选项 [0-2]: " choice
case "$choice" in
1)
if [[ "$SELECTED_SOURCE" == "npm" ]]; then
read -rp "请输入 npm registry URL (Enter 使用默认 https://registry.npmmirror.com): " reg
NPM_REGISTRY="${reg:-https://registry.npmmirror.com}"
ok "npm registry 已设置为: $NPM_REGISTRY"
else
read -rp "请输入 GitHub 镜像 URL (Enter 使用默认 https://ghproxy.com): " mirror
GITHUB_MIRROR="${mirror:-https://ghproxy.com}"
ok "GitHub 镜像已设置为: $GITHUB_MIRROR"
fi
;;
2)
local found=false
for fs in npm github-official github-fork; do
[[ "$fs" == "$SELECTED_SOURCE" ]] && continue
info "尝试从 $fs 获取..."
if get_remote_version "$fs"; then
SELECTED_SOURCE="$fs"
ok "从 $fs 获取版本成功: v$REMOTE_VERSION"
found=true
break
fi
done
if [[ "$found" != true ]]; then
warn "所有来源均无法获取版本"
fi
;;
0) exit 1 ;;
*) warn "无效选项,请重新输入" ;;
esac
echo ""
done
STATUS_REMOTE_VERSION="$REMOTE_VERSION"
fi
local cmp
cmp=$(compare_versions "$STATUS_CLI_VERSION" "$STATUS_REMOTE_VERSION")
if [[ "$cmp" != "2" ]]; then
ok "当前已是最新版本 (v$STATUS_CLI_VERSION)"
read -rp "按 Enter 继续..."
return
fi
info "升级: v$STATUS_CLI_VERSION -> v$STATUS_REMOTE_VERSION"
if [[ "$SELECTED_SOURCE" == "npm" ]]; then
install_nodejs
fi
local ok=false
case "$SELECTED_SOURCE" in
npm)
install_from_npm && ok=true || ok=false
;;
github-official)
if install_from_github "$GITHUB_OFFICIAL_REPO"; then
ok=true
else
warn "官方仓库无匹配 asset,尝试本仓库..."
install_from_github "$GITHUB_FORK_REPO" && ok=true || ok=false
fi
;;
github-fork)
install_from_github "$GITHUB_FORK_REPO" && ok=true || ok=false
;;
esac
if [[ "$ok" != true ]]; then
err "HAPI CLI 升级失败"
fi
collect_status collect_status
read -rp "按 Enter 继续..." read -rp "按 Enter 继续..."
;; ;;
uninstall) uninstall)
echo -e "${YELLOW}警告: 这将删除 HAPI CLI${NC}" echo -e "${YELLOW}警告: 这将删除 HAPI CLI${NC}"
read -rp "确认卸载? [y/N]: " confirm read -rp "确认卸载? [y/N]: " confirm
@@ -1338,14 +1158,7 @@ main_menu() {
collect_status collect_status
read -rp "按 Enter 继续..." read -rp "按 Enter 继续..."
;; ;;
enable_autostart)
hub)
setup_hub
collect_status
read -rp "按 Enter 继续..."
;;
setup_autostart|enable_autostart)
if [[ "$STATUS_HUB_CONFIGURED" == false ]]; then if [[ "$STATUS_HUB_CONFIGURED" == false ]]; then
warn "Hub 未配置,先执行配置" warn "Hub 未配置,先执行配置"
setup_hub || true setup_hub || true
@@ -1354,13 +1167,11 @@ main_menu() {
collect_status collect_status
read -rp "按 Enter 继续..." read -rp "按 Enter 继续..."
;; ;;
disable_autostart) disable_autostart)
disable_autostart disable_autostart
collect_status collect_status
read -rp "按 Enter 继续..." read -rp "按 Enter 继续..."
;; ;;
refresh) refresh)
collect_status collect_status
;; ;;
@@ -1387,31 +1198,10 @@ check_prerequisites() {
} }
# ───────────────────────────────────────── # ─────────────────────────────────────────
# 快速模式:向后兼容的命令行参数
# ───────────────────────────────────────── # ─────────────────────────────────────────
run_quick_mode() { # 选择安装来源
setup_install_dir # ─────────────────────────────────────────
detect_installed resolve_install_source() {
if [[ -n "$INSTALLED_VERSION" ]]; then
if [[ "$INSTALLED_SOURCE" == "npm" ]]; then
local npm_root npm_pkg_dir
npm_root=$(npm root -g 2>/dev/null || echo "/usr/lib/node_modules")
npm_pkg_dir="$npm_root/$NPM_PACKAGE"
ok "HAPI CLI v$INSTALLED_VERSION (npm: $npm_pkg_dir)"
else
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:}"
ok "HAPI CLI v$INSTALLED_VERSION (来源: $src_display, 目录: $INSTALL_DIR)"
fi
else
ok "安装目录: $INSTALL_DIR"
fi
check_prerequisites
SELECTED_SOURCE="" SELECTED_SOURCE=""
if [[ -n "$SOURCE" ]]; then if [[ -n "$SOURCE" ]]; then
case "$SOURCE" in case "$SOURCE" in
@@ -1427,103 +1217,73 @@ run_quick_mode() {
*) SELECTED_SOURCE="npm" ;; *) SELECTED_SOURCE="npm" ;;
esac esac
fi fi
}
resolve_remote_version() {
while ! get_remote_version "$SELECTED_SOURCE"; do while ! get_remote_version "$SELECTED_SOURCE"; do
warn "无法从 $SELECTED_SOURCE 获取远程版本" warn "无法从 $SELECTED_SOURCE 获取远程版本"
echo "" if [[ ! -t 0 ]]; then
echo -e "${YELLOW}请选择操作:${NC}" local found=false
echo " 1) 配置镜像重试 ($SELECTED_SOURCE)" for fs in npm github-official github-fork; do
echo " 2) 切换其他来源" [[ "$fs" == "$SELECTED_SOURCE" ]] && continue
echo " 0) 退出" info "尝试 $fs..."
echo "" if get_remote_version "$fs"; then
read -rp "请输入选项 [0-2]: " choice SELECTED_SOURCE="$fs"
case "$choice" in found=true
1) break
if [[ "$SELECTED_SOURCE" == "npm" ]]; then
read -rp "请输入 npm registry URL (Enter 使用默认 https://registry.npmmirror.com): " reg
NPM_REGISTRY="${reg:-https://registry.npmmirror.com}"
ok "npm registry 已设置为: $NPM_REGISTRY"
else
read -rp "请输入 GitHub 镜像 URL (Enter 使用默认 https://ghproxy.com): " mirror
GITHUB_MIRROR="${mirror:-https://ghproxy.com}"
ok "GitHub 镜像已设置为: $GITHUB_MIRROR"
fi fi
;; done
2) if [[ "$found" != true ]]; then
local found=false err "所有来源均无法获取版本,退出"
for fs in npm github-official github-fork; do exit 1
[[ "$fs" == "$SELECTED_SOURCE" ]] && continue fi
info "尝试从 $fs 获取..." else
if get_remote_version "$fs"; then echo ""
SELECTED_SOURCE="$fs" echo -e "${YELLOW}请选择操作:${NC}"
ok "从 $fs 获取版本成功: v$REMOTE_VERSION" echo " 1) 配置镜像重试 ($SELECTED_SOURCE)"
found=true echo " 2) 切换其他来源"
break echo " 0) 退出"
echo ""
read -rp "请输入选项 [0-2]: " choice
case "$choice" in
1)
if [[ "$SELECTED_SOURCE" == "npm" ]]; then
read -rp "请输入 npm registry URL (Enter 使用默认 https://registry.npmmirror.com): " reg
NPM_REGISTRY="${reg:-https://registry.npmmirror.com}"
ok "npm registry 已设置为: $NPM_REGISTRY"
else
read -rp "请输入 GitHub 镜像 URL (Enter 使用默认 https://ghproxy.com): " mirror
GITHUB_MIRROR="${mirror:-https://ghproxy.com}"
ok "GitHub 镜像已设置为: $GITHUB_MIRROR"
fi fi
done ;;
if [[ "$found" != true ]]; then 2)
warn "所有来源均无法获取版本" local found=false
fi for fs in npm github-official github-fork; do
;; [[ "$fs" == "$SELECTED_SOURCE" ]] && continue
0) exit 1 ;; info "尝试从 $fs 获取..."
*) warn "无效选项,请重新输入" ;; if get_remote_version "$fs"; then
esac SELECTED_SOURCE="$fs"
found=true
break
fi
done
if [[ "$found" != true ]]; then
warn "所有来源均无法获取版本"
fi
;;
0) exit 1 ;;
*) warn "无效选项,请重新输入" ;;
esac
fi
echo "" echo ""
done done
STATUS_REMOTE_VERSION="$REMOTE_VERSION"
}
if [[ "$UPGRADE_ONLY" == true ]]; then perform_install() {
if [[ -z "$INSTALLED_VERSION" || "$INSTALLED_VERSION" == "unknown" ]]; then resolve_install_source
err "未检测到已安装的 HAPI CLI,无法升级" resolve_remote_version
exit 1
fi
local cmp
cmp=$(compare_versions "$INSTALLED_VERSION" "$REMOTE_VERSION")
if [[ "$cmp" != "2" ]]; then
ok "当前已是最新版本 (v$INSTALLED_VERSION)"
exit 0
fi
info "升级: v$INSTALLED_VERSION -> v$REMOTE_VERSION"
fi
if [[ -n "$INSTALLED_VERSION" && "$INSTALLED_VERSION" != "unknown" && "$FORCE" != true && "$UPGRADE_ONLY" != true ]]; then
local cmp
cmp=$(compare_versions "$INSTALLED_VERSION" "$REMOTE_VERSION")
if [[ "$cmp" == "0" ]]; then
ok "当前已是最新版本 (v$INSTALLED_VERSION)"
exit 0
fi
fi
if [[ "$FORCE" != true && "$UPGRADE_ONLY" != true ]]; then
local action="安装"
[[ -n "$INSTALLED_VERSION" ]] && action="覆盖安装"
local src_label=""
case "$SELECTED_SOURCE" in
npm) src_label="npm registry" ;;
github-official) src_label="GitHub (tiann/hapi)" ;;
github-fork) src_label="GitHub (arkylin/Hapi-Docker)" ;;
esac
echo ""
echo -e "${BOLD}即将${action} HAPI CLI:${NC}"
echo -e " 版本: ${CYAN}v$REMOTE_VERSION${NC}"
echo -e " 来源: ${CYAN}$src_label${NC}"
local dir_display
if [[ "$SELECTED_SOURCE" == "npm" && "$INSTALLED_SOURCE" == "npm" ]]; then
local npm_root
npm_root=$(npm root -g 2>/dev/null || echo "/usr/lib/node_modules")
dir_display="$npm_root/$NPM_PACKAGE"
else
dir_display="$INSTALL_DIR"
fi
echo -e " 目录: ${CYAN}$dir_display${NC}"
echo ""
read -rp "确认${action}? [Y/n]: " confirm
if [[ "$confirm" =~ ^[Nn] ]]; then
info "已取消"
exit 0
fi
echo ""
fi
if [[ "$SELECTED_SOURCE" == "npm" ]]; then if [[ "$SELECTED_SOURCE" == "npm" ]]; then
install_nodejs || true install_nodejs || true
@@ -1549,7 +1309,7 @@ run_quick_mode() {
if [[ "$ok" != true ]]; then if [[ "$ok" != true ]]; then
err "HAPI CLI 安装失败" err "HAPI CLI 安装失败"
exit 1 return 1
fi fi
if [[ "$SKIP_HUB" != true ]]; then if [[ "$SKIP_HUB" != true ]]; then
@@ -1558,6 +1318,52 @@ run_quick_mode() {
setup_systemd setup_systemd
fi fi
fi fi
return 0
}
perform_upgrade() {
resolve_install_source
resolve_remote_version
local cmp
cmp=$(compare_versions "$INSTALLED_VERSION" "$REMOTE_VERSION")
if [[ "$cmp" != "2" ]]; then
ok "当前已是最新版本 (v$INSTALLED_VERSION)"
return 0
fi
info "升级: v$INSTALLED_VERSION -> v$REMOTE_VERSION"
if [[ "$SELECTED_SOURCE" == "npm" ]]; then
install_nodejs || true
fi
local ok=false
case "$SELECTED_SOURCE" in
npm) install_from_npm && ok=true || ok=false ;;
github-official)
if install_from_github "$GITHUB_OFFICIAL_REPO"; then
ok=true
else
warn "官方仓库无匹配 asset,尝试本仓库..."
install_from_github "$GITHUB_FORK_REPO" && ok=true || ok=false
fi
;;
github-fork) install_from_github "$GITHUB_FORK_REPO" && ok=true || ok=false ;;
esac
if [[ "$ok" != true ]]; then
err "HAPI CLI 升级失败"
return 1
fi
if [[ "$SKIP_HUB" != true ]]; then
setup_hub
if [[ "$IS_ROOT" == true ]]; then
setup_systemd
fi
fi
return 0
} }
# ───────────────────────────────────────── # ─────────────────────────────────────────
@@ -1569,31 +1375,37 @@ main() {
LOG_FILE="${XDG_CACHE_HOME:-$HOME/.cache}/hapi-install.log" LOG_FILE="${XDG_CACHE_HOME:-$HOME/.cache}/hapi-install.log"
mkdir -p "$(dirname "$LOG_FILE")" 2>/dev/null || true mkdir -p "$(dirname "$LOG_FILE")" 2>/dev/null || true
# 如果带有旧版参数,或是管道执行(curl | bash),进入快速模式
if [[ "$UPGRADE_ONLY" == true || "$FORCE" == true || -n "$SOURCE" || "$SKIP_HUB" == true || -n "$INSTALL_DIR_ARG" || ! -t 0 ]]; then
echo ""
echo -e "${BOLD}╔══════════════════════════════════════════════════════════════════╗${NC}"
echo -e "${BOLD}║${NC} HAPI 快速模式 ${BOLD}║${NC}"
echo -e "${BOLD}╚══════════════════════════════════════════════════════════════════╝${NC}"
echo ""
run_quick_mode
exit 0
fi
echo ""
echo -e "${BOLD}╔══════════════════════════════════════════════════════════════════╗${NC}"
echo -e "${BOLD}║${NC} HAPI 管理脚本 (Linux) ${BOLD}║${NC}"
echo -e "${BOLD}║${NC} 状态查看 | 安装升级 | 开机自启 | Hub 管理 ${BOLD}║${NC}"
echo -e "${BOLD}╚══════════════════════════════════════════════════════════════════╝${NC}"
echo ""
setup_install_dir setup_install_dir
ok "安装目录: $INSTALL_DIR"
check_prerequisites check_prerequisites
collect_status collect_status
echo ""
echo -e "${BOLD}╔══════════════════════════════════════════════════════════════════╗${NC}"
echo -e "${BOLD}║${NC} HAPI 系统状态 ${BOLD}║${NC}"
echo -e "${BOLD}╚══════════════════════════════════════════════════════════════════╝${NC}"
echo ""
print_status
# 非交互模式(curl | bash)
if [[ ! -t 0 ]]; then
if [[ "$UPGRADE_ONLY" == true ]]; then
perform_upgrade
exit $?
fi
if [[ "$FORCE" == true || -n "$SOURCE" || "$SKIP_HUB" == true || -n "$INSTALL_DIR_ARG" ]]; then
perform_install
exit $?
fi
if [[ "$STATUS_CLI_INSTALLED" != true ]]; then
echo ""
warn "HAPI CLI 未安装"
warn "请使用 --source / --force 等参数指定操作,或直接运行 bash install.sh 进入交互模式"
fi
exit 0
fi
# 交互菜单
while true; do while true; do
print_status
main_menu main_menu
done done
} }