# ============================================ # 本地缓存管理脚本 # 用于管理Docker镜像、npm包等构建资源的本地缓存 # ============================================ param( [Parameter(Mandatory=$true)] [ValidateSet("setup", "load-images", "save-images", "copy-deps", "clean")] [string]$Action, [string]$SourcePath = "..\flnxi_sucai", [switch]$Force ) # 设置错误处理 $ErrorActionPreference = "Stop" # 颜色输出函数 function Write-ColorOutput { param([string]$Message, [string]$Color = "White") $colors = @{ "Red" = [ConsoleColor]::Red "Green" = [ConsoleColor]::Green "Yellow" = [ConsoleColor]::Yellow "Blue" = [ConsoleColor]::Blue "Cyan" = [ConsoleColor]::Cyan "White" = [ConsoleColor]::White } Write-Host $Message -ForegroundColor $colors[$Color] } # 检查Docker是否运行 function Test-DockerRunning { try { docker version | Out-Null return $true } catch { return $false } } # 加载Docker镜像 function Load-DockerImages { Write-ColorOutput "🐳 加载Docker基础镜像..." "Blue" $sourceImagesPath = Join-Path $SourcePath "docker-images" $localCachePath = "cache\docker-images" if (-not (Test-Path $sourceImagesPath)) { Write-ColorOutput "❌ 源镜像路径不存在: $sourceImagesPath" "Red" return } # 创建本地缓存目录 if (-not (Test-Path $localCachePath)) { New-Item -ItemType Directory -Path $localCachePath -Force | Out-Null } # 检查并加载Node.js镜像 $nodeImagePath = Join-Path $sourceImagesPath "node\node-18-alpine.tar" if (Test-Path $nodeImagePath) { Write-ColorOutput "📦 发现Node.js 18 Alpine镜像,正在加载..." "Cyan" try { docker load -i $nodeImagePath Copy-Item $nodeImagePath $localCachePath -Force Write-ColorOutput "✅ Node.js镜像加载成功" "Green" } catch { Write-ColorOutput "⚠️ Node.js镜像加载失败: $($_.Exception.Message)" "Yellow" } } # 检查并加载MinIO MC镜像 $minioMcPath = Join-Path $sourceImagesPath "minio-mc\minio-mc-latest.tar" if (Test-Path $minioMcPath) { Write-ColorOutput "📦 发现MinIO MC镜像,正在加载..." "Cyan" try { docker load -i $minioMcPath Copy-Item $minioMcPath $localCachePath -Force Write-ColorOutput "✅ MinIO MC镜像加载成功" "Green" } catch { Write-ColorOutput "⚠️ MinIO MC镜像加载失败: $($_.Exception.Message)" "Yellow" } } # 检查其他可能的镜像 $otherImages = @("postgres", "minio") foreach ($imageName in $otherImages) { $imagePath = Join-Path $sourceImagesPath "$imageName" if (Test-Path $imagePath) { Get-ChildItem "$imagePath\*.tar" | ForEach-Object { Write-ColorOutput "📦 发现$imageName镜像: $($_.Name),正在加载..." "Cyan" try { docker load -i $_.FullName Copy-Item $_.FullName $localCachePath -Force Write-ColorOutput "✅ $imageName镜像加载成功" "Green" } catch { Write-ColorOutput "⚠️ $imageName镜像加载失败: $($_.Exception.Message)" "Yellow" } } } } } # 复制npm依赖包 function Copy-NpmDependencies { Write-ColorOutput "📦 复制npm依赖包..." "Blue" $sourceNodeModules = Join-Path $SourcePath "node_modules" $localCachePath = "cache\node_modules" if (-not (Test-Path $sourceNodeModules)) { Write-ColorOutput "❌ 源node_modules路径不存在: $sourceNodeModules" "Red" return } # 创建本地缓存目录 if (-not (Test-Path $localCachePath)) { New-Item -ItemType Directory -Path $localCachePath -Force | Out-Null } # 复制关键依赖包(避免复制整个node_modules,太大了) $keyPackages = @( "@prisma", ".prisma", "prisma", "sharp", "ag-psd", "next", "react", "react-dom", "@aws-sdk", "three", "gsap" ) foreach ($package in $keyPackages) { $sourcePath = Join-Path $sourceNodeModules $package $targetPath = Join-Path $localCachePath $package if (Test-Path $sourcePath) { Write-ColorOutput "📋 复制包: $package" "Cyan" try { if (Test-Path $targetPath) { Remove-Item $targetPath -Recurse -Force } Copy-Item $sourcePath $targetPath -Recurse -Force Write-ColorOutput "✅ $package 复制成功" "Green" } catch { Write-ColorOutput "⚠️ $package 复制失败: $($_.Exception.Message)" "Yellow" } } } # 复制package-lock.json作为参考 $sourceLockFile = Join-Path $SourcePath "package-lock.json" if (Test-Path $sourceLockFile) { Copy-Item $sourceLockFile "cache\" -Force Write-ColorOutput "✅ package-lock.json已复制到缓存" "Green" } } # 保存当前项目的镜像 function Save-DockerImages { Write-ColorOutput "💾 保存当前Docker镜像..." "Blue" $localCachePath = "cache\docker-images" if (-not (Test-Path $localCachePath)) { New-Item -ItemType Directory -Path $localCachePath -Force | Out-Null } # 保存常用镜像 $imagesToSave = @( @{Name="node:18-alpine"; File="node-18-alpine.tar"}, @{Name="postgres:15-alpine"; File="postgres-15-alpine.tar"}, @{Name="minio/minio:latest"; File="minio-latest.tar"}, @{Name="minio/mc:latest"; File="minio-mc-latest.tar"} ) foreach ($image in $imagesToSave) { try { # 检查镜像是否存在 docker inspect $image.Name | Out-Null $outputPath = Join-Path $localCachePath $image.File Write-ColorOutput "💾 保存镜像: $($image.Name)" "Cyan" docker save $image.Name -o $outputPath Write-ColorOutput "✅ $($image.Name) 保存成功" "Green" } catch { Write-ColorOutput "⚠️ 镜像 $($image.Name) 不存在或保存失败" "Yellow" } } } # 设置缓存环境 function Setup-Cache { Write-ColorOutput "🚀 设置本地缓存环境..." "Blue" # 创建缓存目录结构 $cacheDirectories = @( "cache", "cache\docker-images", "cache\node_modules", "cache\npm-cache", "cache\prisma-engines" ) foreach ($dir in $cacheDirectories) { if (-not (Test-Path $dir)) { New-Item -ItemType Directory -Path $dir -Force | Out-Null Write-ColorOutput "📁 创建目录: $dir" "Green" } } # 创建缓存配置文件 $cacheConfig = @{ version = "1.0" created = Get-Date -Format "yyyy-MM-dd HH:mm:ss" source = $SourcePath description = "3D家具安装展示系统本地缓存" } $cacheConfig | ConvertTo-Json | Set-Content "cache\cache-config.json" Write-ColorOutput "📝 缓存配置文件已创建" "Green" # 复制Prisma引擎文件 $sourcePrismaEngines = Join-Path $SourcePath "prisma\engine-cache" if (Test-Path $sourcePrismaEngines) { Copy-Item "$sourcePrismaEngines\*" "cache\prisma-engines\" -Force Write-ColorOutput "🔧 Prisma引擎文件已复制" "Green" } Write-ColorOutput "✅ 缓存环境设置完成" "Green" } # 清理缓存 function Clean-Cache { Write-ColorOutput "🧹 清理本地缓存..." "Blue" if (Test-Path "cache") { if ($Force -or (Read-Host "确定要删除所有缓存吗?(y/N)") -eq "y") { Remove-Item "cache" -Recurse -Force Write-ColorOutput "✅ 缓存已清理" "Green" } else { Write-ColorOutput "❌ 取消清理操作" "Yellow" } } else { Write-ColorOutput "ℹ️ 没有找到缓存目录" "Cyan" } } # 主逻辑 try { Write-ColorOutput "🎯 3D家具安装展示系统 - 缓存管理器" "Blue" Write-ColorOutput "操作: $Action" "Cyan" # 检查源路径 if (-not (Test-Path $SourcePath) -and $Action -ne "clean") { Write-ColorOutput "❌ 源路径不存在: $SourcePath" "Red" Write-ColorOutput "请确保flnxi_sucai项目在正确的位置" "Yellow" exit 1 } # 检查Docker(除了clean操作) if ($Action -in @("load-images", "save-images") -and -not (Test-DockerRunning)) { Write-ColorOutput "❌ Docker未运行,请先启动Docker" "Red" exit 1 } switch ($Action) { "setup" { Setup-Cache Load-DockerImages Copy-NpmDependencies } "load-images" { Load-DockerImages } "save-images" { Save-DockerImages } "copy-deps" { Copy-NpmDependencies } "clean" { Clean-Cache } } Write-ColorOutput "🎉 操作完成!" "Green" } catch { Write-ColorOutput "❌ 操作失败: $($_.Exception.Message)" "Red" exit 1 }