Files

90 lines
3.0 KiB
PowerShell

$ErrorActionPreference = 'Stop'
$ProjectRoot = Split-Path -Parent $MyInvocation.MyCommand.Path
if (-not $ProjectRoot) { $ProjectRoot = $PSScriptRoot }
Write-Host '=== AI 发布平台 打包工具 ===' -ForegroundColor Cyan
Write-Host ''
# 1. 检查环境
Write-Host '[1/5] 检查环境...' -ForegroundColor Yellow
$node = Get-Command node -ErrorAction SilentlyContinue
if (-not $node) {
Write-Host 'ERROR: Node.js 未安装' -ForegroundColor Red
exit 1
}
Write-Host (' Node: ' + $node.Version)
Write-Host (' npm: ' + (npm --version))
# 2. 构建前端
Write-Host '[2/5] 构建前端 (npm run build)...' -ForegroundColor Yellow
Push-Location $ProjectRoot
try {
npm run build 2>&1 | Out-Null
if ($LASTEXITCODE -ne 0) {
Write-Host 'ERROR: 前端构建失败' -ForegroundColor Red
exit 1
}
} finally {
Pop-Location
}
Write-Host ' 前端构建完成'
# 3. 构建后端 JAR
Write-Host '[3/5] 构建后端 (mvn package)...' -ForegroundColor Yellow
Push-Location $ProjectRoot
try {
if (-not $env:JAVA_HOME) {
$javaDirs = Get-ChildItem 'C:\Program Files\Java' -ErrorAction SilentlyContinue
$java = $javaDirs | Sort-Object LastWriteTime -Descending | Select-Object -First 1
if ($java) {
$env:JAVA_HOME = $java.FullName
$env:PATH = $env:JAVA_HOME + '\bin;' + $env:PATH
Write-Host (' JAVA_HOME: ' + $env:JAVA_HOME)
}
}
.\mvnw.cmd package -DskipTests -q 2>&1 | Out-Null
if ($LASTEXITCODE -ne 0) {
Write-Host 'ERROR: 后端构建失败' -ForegroundColor Red
exit 1
}
} finally {
Pop-Location
}
Write-Host ' 后端构建完成'
# 4. 准备打包目录
Write-Host '[4/5] 准备打包文件...' -ForegroundColor Yellow
$tmpDir = $env:TEMP + '\publish_deploy_' + (Get-Date -Format 'yyyyMMdd_HHmmss')
$frontendDir = $tmpDir + '\frontend'
New-Item -ItemType Directory -Path $frontendDir -Force | Out-Null
Copy-Item ($ProjectRoot + '\dist\index.html') $frontendDir -Force
Copy-Item ($ProjectRoot + '\dist\assets') $frontendDir -Recurse -Force
Copy-Item ($ProjectRoot + '\server.js') $frontendDir -Force
Copy-Item ($ProjectRoot + '\target\daily-report-distribution-1.0.0.jar') $tmpDir -Force
Write-Host ' 打包结构:'
Write-Host ' frontend/'
Write-Host ' index.html'
Write-Host ' server.js'
Write-Host ' assets/'
Write-Host ' app.jar'
# 5. 打包
Write-Host '[5/5] 打包 zip...' -ForegroundColor Yellow
$outputZip = $ProjectRoot + '\deploy\publish_deploy.zip'
Compress-Archive -Path ($tmpDir + '\*') -DestinationPath $outputZip -Force
$zipSize = [math]::Round((Get-Item $outputZip).Length / 1MB, 1)
Write-Host (' 打包完成: ' + $outputZip + ' (' + $zipSize + ' MB)')
Remove-Item $tmpDir -Recurse -Force -ErrorAction SilentlyContinue
Write-Host ''
Write-Host '=== 打包成功 ===' -ForegroundColor Green
Write-Host ''
Write-Host '部署步骤:' -ForegroundColor Cyan
Write-Host ' 1. 上传 deploy\publish_deploy.zip 到 NAS: /usr/local/publish_dishboard/'
Write-Host ' 2. 解压覆盖'
Write-Host ' 3. 重启前后端服务'
Write-Host ''