param(
[Parameter(Position=0)]
[string]$Command = 'build'
)
$ErrorActionPreference = "Stop"
# 验证命令
$validCommands = @('build', 'dev', 'dev-all', 'init', 'help')
if ($Command -and $Command -ne '' -and $validCommands -notcontains $Command) {
Write-Host ""
Write-Host "❌ 未知命令: $Command" -ForegroundColor Red
Write-Host ""
Write-Host "有效命令: build, dev, dev-all, init, help" -ForegroundColor Yellow
Write-Host "使用 '.\build.ps1 help' 查看详细帮助" -ForegroundColor Yellow
Write-Host ""
exit 1
}
#region 帮助函数
function Show-Help {
Write-Host ""
Write-Host "Dpz.Core.Web 构建工具" -ForegroundColor Cyan
Write-Host "===================" -ForegroundColor Cyan
Write-Host ""
Write-Host "用法:" -ForegroundColor Yellow
Write-Host " .\build.ps1 [command]" -ForegroundColor White
Write-Host ""
Write-Host "命令:" -ForegroundColor Yellow
Write-Host " build 生产构建(压缩、哈希、生成清单)[默认]" -ForegroundColor White
Write-Host " dev 启动主应用 watch 模式(开发环境实时编译)" -ForegroundColor White
Write-Host " dev-all 启动主应用 watch 模式(兼容旧命令)" -ForegroundColor White
Write-Host " init 初始化环境(安装依赖,验证配置)" -ForegroundColor White
Write-Host " help 显示此帮助信息" -ForegroundColor White
Write-Host ""
Write-Host "示例:" -ForegroundColor Yellow
Write-Host " .\build.ps1 # 生产构建" -ForegroundColor Gray
Write-Host " .\build.ps1 build # 生产构建" -ForegroundColor Gray
Write-Host " .\build.ps1 dev # 开发 watch" -ForegroundColor Gray
Write-Host " .\build.ps1 init # 初始化环境" -ForegroundColor Gray
Write-Host ""
Write-Host "前端技术栈:" -ForegroundColor Yellow
Write-Host " - TypeScript (可选,支持 JS/TS 混用)" -ForegroundColor Gray
Write-Host " - esbuild(超快速打包,< 50ms)" -ForegroundColor Gray
Write-Host " - clean-css(CSS 压缩)" -ForegroundColor Gray
Write-Host ""
}
function Write-Step {
param([string]$Message)
Write-Host ""
Write-Host "--------------------------------" -ForegroundColor Yellow
Write-Host $Message -ForegroundColor Yellow
Write-Host "--------------------------------" -ForegroundColor Yellow
}
function Write-Success {
param([string]$Message)
Write-Host "✅ $Message" -ForegroundColor Green
}
function Write-Info {
param([string]$Message)
Write-Host "ℹ️ $Message" -ForegroundColor Cyan
}
function Get-LocalCleanCssPath {
$binDirectory = [System.IO.Path]::Combine((Get-Location).Path, "node_modules", ".bin")
$commandPath = [System.IO.Path]::Combine($binDirectory, "cleancss.cmd")
if (-not [System.IO.File]::Exists($commandPath)) {
$commandPath = [System.IO.Path]::Combine($binDirectory, "cleancss")
}
if (-not [System.IO.File]::Exists($commandPath)) {
Write-Host "❌ 未找到本地 clean-css-cli,请先运行 npm install" -ForegroundColor Red
exit 1
}
return $commandPath
}
#endregion
#region 初始化环境
function Initialize-Environment {
Write-Host ""
Write-Host "========================================" -ForegroundColor Cyan
Write-Host "初始化 TypeScript 开发环境" -ForegroundColor Cyan
Write-Host "========================================" -ForegroundColor Cyan
Write-Step "步骤 1/3: 检查并安装依赖"
# 安装项目依赖
Write-Info "安装项目依赖..."
if (Test-Path "node_modules") {
Write-Info "node_modules 已存在,更新依赖..."
} else {
Write-Info "首次安装依赖..."
}
npm install
if ($LASTEXITCODE -ne 0) {
exit $LASTEXITCODE
}
Write-Success "项目依赖安装完成"
$cleanCssPath = Get-LocalCleanCssPath
Write-Success "找到本地 clean-css-cli: $cleanCssPath"
Write-Step "步骤 2/3: 验证配置文件"
$configFiles = @(
"tsconfig.json",
"package.json"
)
foreach ($file in $configFiles) {
if (Test-Path $file) {
Write-Success "找到配置文件: $file"
} else {
Write-Host "⚠️ 缺少配置文件: $file" -ForegroundColor Yellow
}
}
Write-Step "步骤 3/3: 测试构建"
Write-Info "执行一次完整构建验证配置..."
Invoke-ProductionBuild
Write-Host ""
Write-Host "========================================" -ForegroundColor Green
Write-Host "🎉 环境初始化完成!" -ForegroundColor Green
Write-Host "========================================" -ForegroundColor Green
Write-Host ""
Write-Host "下一步:" -ForegroundColor Yellow
Write-Host " 开发模式: .\build.ps1 dev" -ForegroundColor White
Write-Host " 生产构建: .\build.ps1 build" -ForegroundColor White
Write-Host ""
}
#endregion
#region 开发模式
function Start-DevWatch {
param([bool]$AllModules = $false)
Write-Host ""
Write-Host "========================================" -ForegroundColor Cyan
Write-Host "🚀 启动主应用 Watch 模式" -ForegroundColor Cyan
Write-Host "========================================" -ForegroundColor Cyan
Write-Host ""
Write-Info "监听文件变化,自动编译 TypeScript/JavaScript"
Write-Info "修改代码后浏览器刷新即可看到效果"
Write-Host ""
Write-Host "按 Ctrl+C 停止" -ForegroundColor Yellow
Write-Host ""
# 检查依赖
if (-not (Test-Path "node_modules")) {
Write-Host "⚠️ 未发现 node_modules,正在安装依赖..." -ForegroundColor Yellow
npm install
if ($LASTEXITCODE -ne 0) {
exit $LASTEXITCODE
}
Write-Host ""
}
if ($AllModules) {
Write-Info "dev-all 已保留为兼容命令,当前只启动主应用 watch"
}
# 启动 watch
Write-Success "开始监听..."
npm run dev:app
if ($LASTEXITCODE -ne 0) {
exit $LASTEXITCODE
}
}
#endregion
#region 生产构建
function Invoke-ProductionBuild {
npm run build
if ($LASTEXITCODE -ne 0) {
exit $LASTEXITCODE
}
}
#endregion
#region 主程序
# 处理空参数
if ([string]::IsNullOrWhiteSpace($Command)) {
$Command = 'build'
}
switch ($Command) {
'help' {
Show-Help
}
'init' {
Initialize-Environment
}
'dev' {
Start-DevWatch -AllModules $false
}
'dev-all' {
Start-DevWatch -AllModules $true
}
'build' {
Invoke-ProductionBuild
}
}
#endregion
AI 正在分析代码…
评论加载中...