bookworm-boot/stop.ps1

146 lines
5.5 KiB
PowerShell
Raw Normal View History

<#
2026-04-01 12:47:44 +08:00
.SYNOPSIS
Bookworm Portable - 清理/卸载脚本
.DESCRIPTION
清除环境变量, 恢复原始 .claude 目录, 清理痕迹.
.USAGE
.\stop.ps1 # 标准清理 (保留 Bookworm 配置)
.\stop.ps1 -Restore # 完整恢复 (删除 Bookworm, 恢复备份)
.\stop.ps1 -Deep # 深度清理 (含 PS 历史)
#>
param(
[switch]$Restore,
[switch]$Deep
)
$ClaudeTarget = Join-Path $env:USERPROFILE ".claude"
$BackupPath = Join-Path $env:USERPROFILE ".claude.bw-backup"
Write-Host ""
Write-Host " Bookworm Portable - 清理" -ForegroundColor Cyan
Write-Host " ========================" -ForegroundColor Cyan
Write-Host ""
# 1. 终止 Claude Code 及 Node.js 子进程
$claudeProcs = Get-Process | Where-Object { $_.ProcessName -in @("claude", "claude-code") } -ErrorAction SilentlyContinue
if ($claudeProcs) {
Write-Host "[1/5] 终止 Claude Code 进程..." -ForegroundColor Yellow
$claudeProcs | Stop-Process -Force
Start-Sleep -Seconds 3
# 清理残留 node 子进程 (hooks)
Get-Process node -ErrorAction SilentlyContinue | Where-Object {
$_.CommandLine -match '\.claude[\\/]hooks'
} | Stop-Process -Force -ErrorAction SilentlyContinue
Write-Host " [OK] 进程已终止" -ForegroundColor Green
}
else {
Write-Host "[1/5] 无 Claude Code 进程运行" -ForegroundColor Gray
}
# 2. 清除进程级环境变量
Write-Host "[2/4] 清除环境变量..." -ForegroundColor White
$envVars = @(
"CLAUDE_HOME", "CLAUDE_ROOT", "ANTHROPIC_API_KEY",
"ANTHROPIC_BASE_URL", "SUPABASE_ACCESS_TOKEN",
"GITHUB_PERSONAL_ACCESS_TOKEN", "SLACK_BOT_TOKEN",
"ATLASSIAN_API_TOKEN", "BROWSERBASE_API_KEY",
"FIRECRAWL_API_KEY"
)
foreach ($v in $envVars) {
if ([System.Environment]::GetEnvironmentVariable($v, "Process")) {
[System.Environment]::SetEnvironmentVariable($v, $null, "Process")
Write-Host " 清除: $v" -ForegroundColor Gray
}
}
Write-Host " [OK] 环境变量已清除" -ForegroundColor Green
# 3. 清除 Git 凭证缓存
Write-Host "[3/5] 清除 Git 凭证缓存..." -ForegroundColor White
$gitCredTargets = @("git:https://code.letcareme.com", "git:http://8.138.11.105", "git:https://8.138.11.105")
foreach ($target in $gitCredTargets) {
$exists = cmdkey /list 2>$null | Select-String $target
if ($exists) {
cmdkey /delete:$target 2>$null
Write-Host " 清除: $target" -ForegroundColor Gray
}
}
# 通过 git credential reject 清除缓存
@("code.letcareme.com", "8.138.11.105") | ForEach-Object {
@"
protocol=https
host=$_
"@ | git credential reject 2>$null
}
Write-Host " [OK] Git 凭证已清除" -ForegroundColor Green
# 4. 恢复备份 (如果请求)
if ($Restore) {
Write-Host "[4/5] 恢复原始 .claude 目录..." -ForegroundColor White
if (Test-Path $BackupPath) {
if (Test-Path $ClaudeTarget) {
$retries = 3
while ($retries -gt 0) {
try {
Remove-Item $ClaudeTarget -Recurse -Force -ErrorAction Stop
break
} catch {
$retries--
if ($retries -gt 0) {
Write-Host " 文件占用,等待重试... ($retries)" -ForegroundColor Yellow
Start-Sleep 3
} else {
Write-Host " [ERROR] 无法删除 .claude可能有文件被占用" -ForegroundColor Red
Write-Host " 请关闭所有 Node.js/Claude 进程后重试" -ForegroundColor Yellow
exit 1
}
}
}
Write-Host " 已删除 Bookworm 配置" -ForegroundColor Gray
}
Rename-Item $BackupPath $ClaudeTarget
Write-Host " [OK] 原始 .claude 已恢复" -ForegroundColor Green
}
else {
Write-Host " [!] 无备份可恢复 (.claude.bw-backup 不存在)" -ForegroundColor Yellow
}
}
else {
Write-Host "[4/5] 保留 Bookworm 配置 (使用 -Restore 可恢复原始)" -ForegroundColor Gray
}
# 5. 深度清理 (可选)
if ($Deep) {
Write-Host "[5/5] 深度清理..." -ForegroundColor White
# 清除 PowerShell 历史
$histFile = Join-Path $env:APPDATA "Microsoft\Windows\PowerShell\PSReadLine\ConsoleHost_history.txt"
if (Test-Path $histFile) {
# 仅删除含 bookworm/secrets/api_key 的行
$lines = Get-Content $histFile
$cleaned = $lines | Where-Object {
$_ -notmatch 'secrets\.enc|ANTHROPIC_API_KEY|api[_-]?key|bookworm-portable'
}
Set-Content $histFile -Value $cleaned
Write-Host " [OK] PS 历史已清理敏感条目" -ForegroundColor Green
}
# 清除 Claude Code 本地缓存
$cacheDir = Join-Path $env:LOCALAPPDATA "claude-code"
if (Test-Path $cacheDir) {
Write-Host " 发现本地缓存: $cacheDir" -ForegroundColor Yellow
Write-Host " (手动决定是否删除)" -ForegroundColor Yellow
}
Write-Host " [OK] 深度清理完成" -ForegroundColor Green
}
else {
Write-Host "[5/5] 跳过深度清理 (使用 -Deep 可清理 PS 历史)" -ForegroundColor Gray
}
Write-Host ""
Write-Host " ╔══════════════════════════════════════════╗" -ForegroundColor Green
Write-Host " ║ Bookworm 已清理完毕 ║" -ForegroundColor Green
Write-Host " ╚══════════════════════════════════════════╝" -ForegroundColor Green
Write-Host ""