175 lines
4.6 KiB
PowerShell
175 lines
4.6 KiB
PowerShell
# 药箱启动脚本 (PowerShell)
|
|
# 用法: .\start.ps1 [命令]
|
|
|
|
param(
|
|
[string]$Command = "help"
|
|
)
|
|
|
|
$ErrorActionPreference = "Stop"
|
|
|
|
# 颜色函数
|
|
function Write-Color {
|
|
param([string]$Text, [string]$Color = "White")
|
|
Write-Host $Text -ForegroundColor $Color
|
|
}
|
|
|
|
function Write-Success { param([string]$Text) Write-Color $Text "Green" }
|
|
function Write-Error { param([string]$Text) Write-Color $Text "Red" }
|
|
function Write-Info { param([string]$Text) Write-Color $Text "Cyan" }
|
|
function Write-Warning { param([string]$Text) Write-Color $Text "Yellow" }
|
|
|
|
# 检查 Node.js
|
|
function Test-Node {
|
|
try {
|
|
$version = node --version
|
|
Write-Success "Node.js $version 已安装"
|
|
return $true
|
|
} catch {
|
|
Write-Error "未安装 Node.js,请先安装"
|
|
return $false
|
|
}
|
|
}
|
|
|
|
# 检查 Python
|
|
function Test-Python {
|
|
try {
|
|
$version = python --version
|
|
Write-Success "Python $version 已安装"
|
|
return $true
|
|
} catch {
|
|
Write-Error "未安装 Python,请先安装"
|
|
return $false
|
|
}
|
|
}
|
|
|
|
# 安装前端依赖
|
|
function Install-Frontend {
|
|
Write-Info "安装前端依赖..."
|
|
Set-Location frontend
|
|
npm install
|
|
Set-Location ..
|
|
Write-Success "前端依赖安装完成"
|
|
}
|
|
|
|
# 安装后端依赖
|
|
function Install-Backend {
|
|
Write-Info "安装后端依赖..."
|
|
Set-Location backend
|
|
|
|
if (-not (Test-Path "venv")) {
|
|
Write-Info "创建虚拟环境..."
|
|
python -m venv venv
|
|
}
|
|
|
|
& ".\venv\Scripts\Activate.ps1"
|
|
pip install -r requirements.txt
|
|
Set-Location ..
|
|
Write-Success "后端依赖安装完成"
|
|
}
|
|
|
|
# 安装所有依赖
|
|
function Install-All {
|
|
Install-Frontend
|
|
Install-Backend
|
|
}
|
|
|
|
# 启动前端
|
|
function Start-Frontend {
|
|
Write-Info "启动前端开发服务器..."
|
|
Set-Location frontend
|
|
Start-Process npm -ArgumentList "run dev" -NoNewWindow
|
|
Set-Location ..
|
|
Write-Success "前端服务器启动中: http://localhost:5173"
|
|
}
|
|
|
|
# 启动后端
|
|
function Start-Backend {
|
|
Write-Info "启动后端开发服务器..."
|
|
Set-Location backend
|
|
|
|
if (-not (Test-Path "venv")) {
|
|
Install-Backend
|
|
}
|
|
|
|
& ".\venv\Scripts\Activate.ps1"
|
|
Start-Process uvicorn -ArgumentList "app.main:app --reload --host 0.0.0.0 --port 8000" -NoNewWindow
|
|
Set-Location ..
|
|
Write-Success "后端服务器启动中: http://localhost:8000"
|
|
}
|
|
|
|
# 启动所有服务
|
|
function Start-All {
|
|
Start-Backend
|
|
Start-Sleep -Seconds 2
|
|
Start-Frontend
|
|
Write-Success "所有服务已启动"
|
|
Write-Info "前端: http://localhost:5173"
|
|
Write-Info "后端: http://localhost:8000"
|
|
Write-Info "API 文档: http://localhost:8000/docs"
|
|
}
|
|
|
|
# 构建前端
|
|
function Build-Frontend {
|
|
Write-Info "构建前端生产版本..."
|
|
Set-Location frontend
|
|
npm run build
|
|
Set-Location ..
|
|
Write-Success "构建完成,输出目录: frontend/dist"
|
|
}
|
|
|
|
# 清理缓存
|
|
function Clear-Cache {
|
|
Write-Info "清理缓存..."
|
|
|
|
if (Test-Path "frontend/node_modules/.cache") {
|
|
Remove-Item -Recurse -Force "frontend/node_modules/.cache"
|
|
}
|
|
|
|
Write-Success "缓存清理完成"
|
|
}
|
|
|
|
# 显示帮助
|
|
function Show-Help {
|
|
Write-Color "药箱启动脚本" "Cyan"
|
|
Write-Color "========================" "Cyan"
|
|
Write-Host ""
|
|
Write-Info "可用命令:"
|
|
Write-Host " install - 安装所有依赖"
|
|
Write-Host " install:f - 安装前端依赖"
|
|
Write-Host " install:b - 安装后端依赖"
|
|
Write-Host " start - 启动所有服务"
|
|
Write-Host " start:f - 启动前端服务"
|
|
Write-Host " start:b - 启动后端服务"
|
|
Write-Host " build - 构建前端生产版本"
|
|
Write-Host " clean - 清理缓存"
|
|
Write-Host " help - 显示帮助"
|
|
Write-Host ""
|
|
Write-Info "示例:"
|
|
Write-Host " .\start.ps1 install # 安装所有依赖"
|
|
Write-Host " .\start.ps1 start # 启动所有服务"
|
|
Write-Host " .\start.ps1 build # 构建生产版本"
|
|
}
|
|
|
|
# 主逻辑
|
|
switch ($Command.ToLower()) {
|
|
"install" { Install-All }
|
|
"install:f" { Install-Frontend }
|
|
"install:frontend" { Install-Frontend }
|
|
"install:b" { Install-Backend }
|
|
"install:backend" { Install-Backend }
|
|
"start" { Start-All }
|
|
"start:f" { Start-Frontend }
|
|
"start:frontend" { Start-Frontend }
|
|
"start:b" { Start-Backend }
|
|
"start:backend" { Start-Backend }
|
|
"build" { Build-Frontend }
|
|
"build:f" { Build-Frontend }
|
|
"build:frontend" { Build-Frontend }
|
|
"clean" { Clear-Cache }
|
|
"help" { Show-Help }
|
|
"?" { Show-Help }
|
|
default {
|
|
Write-Error "未知命令: $Command"
|
|
Show-Help
|
|
}
|
|
} |