-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit.ps1
More file actions
139 lines (121 loc) · 6.02 KB
/
Copy pathinit.ps1
File metadata and controls
139 lines (121 loc) · 6.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# OPPE 环境初始化脚本 (Windows PowerShell)
# 用途:每次 Builder/QA 会话开始时运行,确认环境可用
# 用法:.\init.ps1
$ErrorActionPreference = "Stop"
Write-Host "===============================" -ForegroundColor Cyan
Write-Host " OPPE 环境初始化" -ForegroundColor Cyan
Write-Host "===============================" -ForegroundColor Cyan
# ── 1. 检查 Node.js 版本 ──────────────────────────────
$REQUIRED_NODE = 18
try {
$nodeVersion = node -v 2>$null
$currentNode = [int]($nodeVersion -replace 'v(\d+)\..*', '$1')
} catch {
Write-Host "❌ 未检测到 Node.js,请先安装 Node.js v${REQUIRED_NODE}+" -ForegroundColor Red
exit 1
}
if ($currentNode -lt $REQUIRED_NODE) {
Write-Host "❌ Node.js 版本过低(当前:$nodeVersion,需要 v${REQUIRED_NODE}+)" -ForegroundColor Red
exit 1
}
$npmVersion = npm -v
Write-Host "✅ Node.js $nodeVersion | npm $npmVersion" -ForegroundColor Green
# ── 2. 安装依赖 ───────────────────────────────────────
if (-not (Test-Path "node_modules")) {
Write-Host "📦 node_modules 不存在,正在安装依赖..." -ForegroundColor Yellow
npm install
Write-Host "✅ 依赖安装完成" -ForegroundColor Green
} else {
Write-Host "✅ 依赖已安装(node_modules 存在)" -ForegroundColor Green
}
# ── 3. 检查 .env ──────────────────────────────────────
if (-not (Test-Path ".env")) {
if (-not (Test-Path ".env.example")) {
Write-Host "❌ .env.example 不存在,请先创建再运行" -ForegroundColor Red
exit 1
}
Copy-Item ".env.example" ".env"
# 自动生成 NEXTAUTH_SECRET(API key 加密必填)
$secretBytes = New-Object byte[] 32
[Security.Cryptography.RandomNumberGenerator]::Create().GetBytes($secretBytes)
$secret = [Convert]::ToBase64String($secretBytes)
(Get-Content ".env") -replace 'NEXTAUTH_SECRET=<auto-generated>', "NEXTAUTH_SECRET=$secret" | Set-Content ".env"
Write-Host "✅ .env 已创建(NEXTAUTH_SECRET 自动生成)" -ForegroundColor Green
Write-Host " API Key 请在登录后进入 /settings 页面配置" -ForegroundColor Yellow
} else {
Write-Host "✅ .env 存在" -ForegroundColor Green
}
# ── 4. 创建数据库目录 ─────────────────────────────────
if (-not (Test-Path "db")) {
New-Item -ItemType Directory -Path "db" | Out-Null
}
Write-Host "✅ db/ 目录就绪" -ForegroundColor Green
# ── 5. 生成 Prisma Client ─────────────────────────────
Write-Host "🔧 生成 Prisma Client..." -ForegroundColor Cyan
npx prisma generate
Write-Host "✅ Prisma Client 生成完成" -ForegroundColor Green
# ── 6. 运行数据库迁移 ─────────────────────────────────
Write-Host "🗄️ 同步数据库..." -ForegroundColor Cyan
try {
npx prisma migrate deploy 2>$null
Write-Host "✅ 数据库迁移完成(deploy 模式)" -ForegroundColor Green
} catch {
try {
npx prisma db push 2>$null
Write-Host "✅ 数据库已同步(db push 模式,适用于首次运行)" -ForegroundColor Green
} catch {
Write-Host "⚠️ 数据库迁移跳过(首次运行或无迁移文件,属于正常情况)" -ForegroundColor Yellow
}
}
# ── 7. TypeScript 类型检查 ────────────────────────────
Write-Host "🔍 TypeScript 类型检查..." -ForegroundColor Cyan
try {
npx tsc --noEmit
Write-Host "✅ TypeScript 无类型错误" -ForegroundColor Green
} catch {
Write-Host "⚠️ TypeScript 有类型错误,Builder 需要修复后才能继续" -ForegroundColor Yellow
Write-Host " 运行 npx tsc --noEmit 查看详情" -ForegroundColor Yellow
}
# ── 8. 启动开发服务器并健康检查 ───────────────────────
Write-Host "🚀 启动开发服务器..." -ForegroundColor Cyan
$logFile = "$env:TEMP\oppe-dev.log"
try {
$response = Invoke-WebRequest -Uri "http://localhost:3000" -TimeoutSec 2 -UseBasicParsing -ErrorAction Stop
Write-Host "✅ 服务器已在运行:http://localhost:3000" -ForegroundColor Green
} catch {
# 后台启动
$devProcess = Start-Process -FilePath "npm" -ArgumentList "run","dev" `
-RedirectStandardOutput $logFile -RedirectStandardError $logFile `
-NoNewWindow -PassThru
Write-Host " 服务器 PID:$($devProcess.Id)(日志:$logFile)" -ForegroundColor Gray
Write-Host "⏳ 等待服务器就绪..." -ForegroundColor Cyan
$maxAttempts = 60
$attempt = 0
$ready = $false
while ($attempt -lt $maxAttempts) {
Start-Sleep -Seconds 1
$attempt++
try {
Invoke-WebRequest -Uri "http://localhost:3000" -TimeoutSec 2 -UseBasicParsing -ErrorAction Stop | Out-Null
Write-Host "✅ 服务器就绪:http://localhost:3000" -ForegroundColor Green
$ready = $true
break
} catch { }
}
if (-not $ready) {
Write-Host "❌ 服务器启动超时(60 秒),请查看日志:" -ForegroundColor Red
Write-Host " Get-Content $logFile" -ForegroundColor Red
Stop-Process -Id $devProcess.Id -Force -ErrorAction SilentlyContinue
exit 1
}
}
# ── 完成 ──────────────────────────────────────────────
Write-Host ""
Write-Host "===============================" -ForegroundColor Cyan
Write-Host " ✅ 环境初始化完成" -ForegroundColor Green
Write-Host "===============================" -ForegroundColor Cyan
Write-Host " 访问地址:http://localhost:3000" -ForegroundColor White
Write-Host " 数据库 :.\db\dev.db" -ForegroundColor White
Write-Host " 日志 :$logFile" -ForegroundColor White
Write-Host ""
Write-Host "Agent 可以开始工作了。" -ForegroundColor Green