134 lines
5.2 KiB
PowerShell
134 lines
5.2 KiB
PowerShell
# 保存 npm 包到本地
|
||
# 用于加速后续构建,避免每次从网络拉取 npm 包
|
||
# 注意:虽然已配置淘宝镜像源,但网络不稳定时仍可能失败
|
||
|
||
param(
|
||
[switch]$Force = $false # 强制重新下载,即使本地已存在
|
||
)
|
||
|
||
$ErrorActionPreference = "Stop"
|
||
|
||
$npmCacheDir = Join-Path (Split-Path $PSScriptRoot -Parent) "npm-cache"
|
||
$npmCacheDir = [System.IO.Path]::GetFullPath($npmCacheDir)
|
||
|
||
Write-Host "========================================" -ForegroundColor Cyan
|
||
Write-Host " 保存 npm 包到本地缓存" -ForegroundColor Cyan
|
||
Write-Host "========================================" -ForegroundColor Cyan
|
||
Write-Host ""
|
||
Write-Host "目标目录: $npmCacheDir" -ForegroundColor Yellow
|
||
Write-Host ""
|
||
|
||
# 创建目录
|
||
if (-not (Test-Path $npmCacheDir)) {
|
||
New-Item -ItemType Directory -Path $npmCacheDir -Force | Out-Null
|
||
Write-Host "已创建目录: $npmCacheDir" -ForegroundColor Green
|
||
}
|
||
|
||
$projectRoot = Split-Path $PSScriptRoot -Parent
|
||
$packageJsonPath = Join-Path $projectRoot "package.json"
|
||
$packageLockPath = Join-Path $projectRoot "package-lock.json"
|
||
|
||
if (-not (Test-Path $packageJsonPath)) {
|
||
Write-Host "错误: 未找到 package.json" -ForegroundColor Red
|
||
exit 1
|
||
}
|
||
|
||
Write-Host "检查 package.json..." -ForegroundColor Cyan
|
||
Write-Host ""
|
||
|
||
# 方法1: 使用 npm pack 打包所有依赖(推荐)
|
||
# 这会创建一个包含所有依赖的 tarball
|
||
$packOutput = Join-Path $npmCacheDir "packed-dependencies.tar.gz"
|
||
|
||
if ((Test-Path $packOutput) -and -not $Force) {
|
||
Write-Host "✓ 已存在打包文件: $packOutput" -ForegroundColor Gray
|
||
Write-Host "提示: 使用 -Force 参数重新打包" -ForegroundColor Gray
|
||
} else {
|
||
Write-Host "正在安装依赖并打包..." -ForegroundColor Yellow
|
||
Write-Host "注意: 这可能需要几分钟时间" -ForegroundColor Gray
|
||
Write-Host ""
|
||
|
||
# 创建临时目录
|
||
$tempDir = Join-Path $npmCacheDir "temp-install"
|
||
if (Test-Path $tempDir) {
|
||
Remove-Item -Path $tempDir -Recurse -Force
|
||
}
|
||
New-Item -ItemType Directory -Path $tempDir -Force | Out-Null
|
||
|
||
try {
|
||
# 复制 package.json 和 package-lock.json
|
||
Copy-Item $packageJsonPath (Join-Path $tempDir "package.json")
|
||
if (Test-Path $packageLockPath) {
|
||
Copy-Item $packageLockPath (Join-Path $tempDir "package-lock.json")
|
||
}
|
||
|
||
# 切换到临时目录并安装依赖
|
||
Push-Location $tempDir
|
||
Write-Host " 配置 npm 使用淘宝镜像源..." -ForegroundColor Yellow
|
||
npm config set registry https://registry.npmmirror.com
|
||
|
||
Write-Host " 正在安装依赖(这可能需要几分钟)..." -ForegroundColor Yellow
|
||
npm install --legacy-peer-deps
|
||
|
||
if ($LASTEXITCODE -ne 0) {
|
||
throw "npm install 失败"
|
||
}
|
||
|
||
# 打包 node_modules
|
||
Write-Host " 正在打包 node_modules..." -ForegroundColor Yellow
|
||
if (Test-Path $packOutput) {
|
||
Remove-Item $packOutput -Force
|
||
}
|
||
|
||
# 使用 tar 打包(Windows 上可能需要安装 tar 或使用 7zip)
|
||
$nodeModulesPath = Join-Path $tempDir "node_modules"
|
||
if (Test-Path $nodeModulesPath) {
|
||
# 尝试使用 tar(Windows 10+ 内置)
|
||
$tarCmd = Get-Command tar -ErrorAction SilentlyContinue
|
||
if ($tarCmd) {
|
||
Push-Location $tempDir
|
||
tar -czf $packOutput node_modules
|
||
Pop-Location
|
||
} else {
|
||
Write-Host " 警告: 未找到 tar 命令,跳过打包" -ForegroundColor Yellow
|
||
Write-Host " 提示: 可以直接复制 node_modules 目录" -ForegroundColor Gray
|
||
# 直接复制 node_modules 目录
|
||
$nodeModulesCache = Join-Path $npmCacheDir "node_modules"
|
||
if (Test-Path $nodeModulesCache) {
|
||
Remove-Item $nodeModulesCache -Recurse -Force
|
||
}
|
||
Copy-Item $nodeModulesPath $nodeModulesCache -Recurse
|
||
}
|
||
}
|
||
|
||
Pop-Location
|
||
|
||
if (Test-Path $packOutput) {
|
||
$fileSizeMB = (Get-Item $packOutput).Length / 1MB
|
||
$fileSizeStr = [Math]::Round($fileSizeMB, 2).ToString() + " MB"
|
||
Write-Host " ✓ 打包成功: $packOutput ($fileSizeStr)" -ForegroundColor Green
|
||
} else {
|
||
Write-Host " ✓ node_modules 已复制到缓存目录" -ForegroundColor Green
|
||
}
|
||
} catch {
|
||
Pop-Location
|
||
Write-Host " ✗ 打包失败: $_" -ForegroundColor Red
|
||
exit 1
|
||
} finally {
|
||
# 清理临时目录
|
||
if (Test-Path $tempDir) {
|
||
Remove-Item -Path $tempDir -Recurse -Force -ErrorAction SilentlyContinue
|
||
}
|
||
}
|
||
}
|
||
|
||
Write-Host ""
|
||
Write-Host "========================================" -ForegroundColor Cyan
|
||
Write-Host " 完成" -ForegroundColor Cyan
|
||
Write-Host "========================================" -ForegroundColor Cyan
|
||
Write-Host ""
|
||
Write-Host "提示: npm 包已缓存到本地" -ForegroundColor Yellow
|
||
Write-Host "提示: 在 Dockerfile 中可以使用 COPY 从缓存目录复制 node_modules" -ForegroundColor Gray
|
||
Write-Host "提示: 如需重新下载,请使用 -Force 参数" -ForegroundColor Gray
|
||
|