clean-cache.ps1 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. # Angular 项目缓存清理脚本
  2. # 用法: .\clean-cache.ps1
  3. Write-Host "================================" -ForegroundColor Cyan
  4. Write-Host " Angular 项目缓存清理工具" -ForegroundColor Cyan
  5. Write-Host "================================" -ForegroundColor Cyan
  6. Write-Host ""
  7. # 检查当前项目大小
  8. Write-Host "📊 检查当前项目大小..." -ForegroundColor Yellow
  9. $beforeSize = (Get-ChildItem -Recurse -ErrorAction SilentlyContinue | Measure-Object -Property Length -Sum).Sum / 1GB
  10. Write-Host "当前项目总大小: $([math]::Round($beforeSize, 2)) GB" -ForegroundColor White
  11. Write-Host ""
  12. # 检查各文件夹大小
  13. Write-Host "📁 各文件夹大小:" -ForegroundColor Yellow
  14. $folders = @('.angular', 'node_modules', 'dist', 'coverage')
  15. foreach ($folder in $folders) {
  16. if (Test-Path $folder) {
  17. $size = (Get-ChildItem $folder -Recurse -ErrorAction SilentlyContinue | Measure-Object -Property Length -Sum).Sum / 1GB
  18. $sizeStr = [math]::Round($size, 2)
  19. if ($size -gt 5) {
  20. Write-Host " $folder : $sizeStr GB" -ForegroundColor Red
  21. } elseif ($size -gt 1) {
  22. Write-Host " $folder : $sizeStr GB" -ForegroundColor Yellow
  23. } else {
  24. Write-Host " $folder : $sizeStr GB" -ForegroundColor Green
  25. }
  26. } else {
  27. Write-Host " $folder : 不存在" -ForegroundColor Gray
  28. }
  29. }
  30. Write-Host ""
  31. # 询问用户要清理什么
  32. Write-Host "🧹 请选择要清理的内容:" -ForegroundColor Cyan
  33. Write-Host " 1. 仅清理 .angular 缓存 (推荐)" -ForegroundColor White
  34. Write-Host " 2. 清理 .angular 和 dist" -ForegroundColor White
  35. Write-Host " 3. 清理 .angular, dist 和 coverage" -ForegroundColor White
  36. Write-Host " 4. 清理所有 (包括 node_modules)" -ForegroundColor Red
  37. Write-Host " 5. 取消" -ForegroundColor Gray
  38. Write-Host ""
  39. $choice = Read-Host "请输入选项 (1-5)"
  40. switch ($choice) {
  41. "1" {
  42. Write-Host ""
  43. Write-Host "🗑️ 正在清理 .angular 缓存..." -ForegroundColor Yellow
  44. if (Test-Path ".angular") {
  45. Remove-Item -Path ".angular" -Recurse -Force -ErrorAction SilentlyContinue
  46. Write-Host "✅ .angular 已删除" -ForegroundColor Green
  47. } else {
  48. Write-Host "ℹ️ .angular 不存在" -ForegroundColor Gray
  49. }
  50. }
  51. "2" {
  52. Write-Host ""
  53. Write-Host "🗑️ 正在清理 .angular 和 dist..." -ForegroundColor Yellow
  54. if (Test-Path ".angular") {
  55. Remove-Item -Path ".angular" -Recurse -Force -ErrorAction SilentlyContinue
  56. Write-Host "✅ .angular 已删除" -ForegroundColor Green
  57. }
  58. if (Test-Path "dist") {
  59. Remove-Item -Path "dist" -Recurse -Force -ErrorAction SilentlyContinue
  60. Write-Host "✅ dist 已删除" -ForegroundColor Green
  61. }
  62. }
  63. "3" {
  64. Write-Host ""
  65. Write-Host "🗑️ 正在清理 .angular, dist 和 coverage..." -ForegroundColor Yellow
  66. if (Test-Path ".angular") {
  67. Remove-Item -Path ".angular" -Recurse -Force -ErrorAction SilentlyContinue
  68. Write-Host "✅ .angular 已删除" -ForegroundColor Green
  69. }
  70. if (Test-Path "dist") {
  71. Remove-Item -Path "dist" -Recurse -Force -ErrorAction SilentlyContinue
  72. Write-Host "✅ dist 已删除" -ForegroundColor Green
  73. }
  74. if (Test-Path "coverage") {
  75. Remove-Item -Path "coverage" -Recurse -Force -ErrorAction SilentlyContinue
  76. Write-Host "✅ coverage 已删除" -ForegroundColor Green
  77. }
  78. }
  79. "4" {
  80. Write-Host ""
  81. Write-Host "⚠️ 警告: 这将删除所有缓存和依赖!" -ForegroundColor Red
  82. $confirm = Read-Host "确认删除? (yes/no)"
  83. if ($confirm -eq "yes") {
  84. Write-Host "🗑️ 正在清理所有文件..." -ForegroundColor Yellow
  85. Remove-Item -Path ".angular" -Recurse -Force -ErrorAction SilentlyContinue
  86. Remove-Item -Path "dist" -Recurse -Force -ErrorAction SilentlyContinue
  87. Remove-Item -Path "coverage" -Recurse -Force -ErrorAction SilentlyContinue
  88. Remove-Item -Path "node_modules" -Recurse -Force -ErrorAction SilentlyContinue
  89. Write-Host "✅ 所有文件已删除" -ForegroundColor Green
  90. Write-Host "💡 请运行 'npm install' 重新安装依赖" -ForegroundColor Cyan
  91. } else {
  92. Write-Host "❌ 已取消" -ForegroundColor Gray
  93. exit
  94. }
  95. }
  96. "5" {
  97. Write-Host "❌ 已取消" -ForegroundColor Gray
  98. exit
  99. }
  100. default {
  101. Write-Host "❌ 无效选项" -ForegroundColor Red
  102. exit
  103. }
  104. }
  105. # 显示清理后的大小
  106. Write-Host ""
  107. Write-Host "📊 清理后的项目大小..." -ForegroundColor Yellow
  108. $afterSize = (Get-ChildItem -Recurse -ErrorAction SilentlyContinue | Measure-Object -Property Length -Sum).Sum / 1GB
  109. $saved = $beforeSize - $afterSize
  110. Write-Host "清理后项目大小: $([math]::Round($afterSize, 2)) GB" -ForegroundColor Green
  111. Write-Host "节省空间: $([math]::Round($saved, 2)) GB" -ForegroundColor Green
  112. Write-Host ""
  113. Write-Host "✅ 清理完成!" -ForegroundColor Green
  114. Write-Host ""