# 更新 Docker 构建缓存 # 用途:更新 npm-packages.tgz 缓存,加速后续构建 Write-Host "🔄 更新 Docker 构建缓存" -ForegroundColor Cyan Write-Host "" # 检查 node_modules 是否存在 if (-not (Test-Path "node_modules")) { Write-Host "❌ node_modules 不存在,请先运行 npm install" -ForegroundColor Red exit 1 } # 创建缓存目录 if (-not (Test-Path ".docker-cache")) { New-Item -ItemType Directory -Path ".docker-cache" | Out-Null } # 打包 node_modules Write-Host "📦 正在打包 node_modules..." -ForegroundColor Yellow $tarFile = ".docker-cache/npm-packages.tgz" # 使用 tar 命令打包(Windows 10+ 自带) tar -czf $tarFile node_modules if (Test-Path $tarFile) { $fileSize = (Get-Item $tarFile).Length / 1MB Write-Host "✅ 缓存已更新:$([Math]::Round($fileSize, 2)) MB" -ForegroundColor Green # 更新缓存信息 $cacheInfo = @{ "created" = (Get-Date).ToString("yyyy-MM-dd HH:mm:ss") "size_mb" = [Math]::Round($fileSize, 2) "node_version" = (node -v) "npm_version" = (npm -v) } $cacheInfo | ConvertTo-Json | Out-File ".docker-cache/cache-info.json" -Encoding UTF8 Write-Host "" Write-Host "💡 提示:下次构建镜像时会使用此缓存,速度会快很多" -ForegroundColor Cyan } else { Write-Host "❌ 打包失败" -ForegroundColor Red exit 1 }