-
-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathdev.ps1
More file actions
239 lines (213 loc) · 6.72 KB
/
dev.ps1
File metadata and controls
239 lines (213 loc) · 6.72 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
#Requires -Version 5.1
<#
.SYNOPSIS
ServerKit development launcher and validation tool.
.DESCRIPTION
Start backend, frontend, or both. Run validation checks.
.PARAMETER Mode
Operation mode: start (default), backend, frontend, validate
.EXAMPLE
.\dev.ps1 # Start backend + frontend
.\dev.ps1 backend # Backend only
.\dev.ps1 frontend # Frontend only
.\dev.ps1 validate # Run all linters/checks
#>
param(
[Parameter(Position = 0)]
[ValidateSet('start', 'backend', 'frontend', 'validate')]
[string]$Mode = 'start'
)
$ProjectRoot = $PSScriptRoot
$BackendDir = Join-Path $ProjectRoot 'backend'
$FrontendDir = Join-Path $ProjectRoot 'frontend'
function Write-Header {
param([string]$Text)
Write-Host ""
Write-Host "=== $Text ===" -ForegroundColor Cyan
Write-Host ""
}
function Write-Pass {
param([string]$Text)
Write-Host " PASS " -ForegroundColor Green -NoNewline
Write-Host $Text
}
function Write-Fail {
param([string]$Text)
Write-Host " FAIL " -ForegroundColor Red -NoNewline
Write-Host $Text
}
function Start-Backend {
Write-Header "Starting Backend (http://localhost:5000)"
Push-Location $BackendDir
try {
if (Test-Path 'venv\Scripts\Activate.ps1') {
& 'venv\Scripts\Activate.ps1'
}
python run.py
}
finally {
Pop-Location
}
}
function Start-Frontend {
Write-Header "Starting Frontend (http://localhost:5173)"
Push-Location $FrontendDir
try {
npm run dev
}
finally {
Pop-Location
}
}
function Start-Both {
Write-Host ""
Write-Host "ServerKit Dev Server" -ForegroundColor Cyan
Write-Host " Backend: http://localhost:5000"
Write-Host " Frontend: http://localhost:5173"
Write-Host ""
$backendJob = Start-Job -ScriptBlock {
param($dir)
Set-Location $dir
if (Test-Path 'venv\Scripts\Activate.ps1') {
& 'venv\Scripts\Activate.ps1'
}
python run.py
} -ArgumentList $BackendDir
Start-Sleep -Seconds 2
$frontendJob = Start-Job -ScriptBlock {
param($dir)
Set-Location $dir
npm run dev
} -ArgumentList $FrontendDir
try {
Write-Host "Press Ctrl+C to stop..." -ForegroundColor DarkGray
while ($true) {
# Stream output from both jobs
Receive-Job $backendJob -ErrorAction SilentlyContinue
Receive-Job $frontendJob -ErrorAction SilentlyContinue
if ($backendJob.State -eq 'Failed') {
Write-Host "Backend crashed!" -ForegroundColor Red
Receive-Job $backendJob
break
}
if ($frontendJob.State -eq 'Failed') {
Write-Host "Frontend crashed!" -ForegroundColor Red
Receive-Job $frontendJob
break
}
Start-Sleep -Seconds 1
}
}
finally {
Stop-Job $backendJob -ErrorAction SilentlyContinue
Stop-Job $frontendJob -ErrorAction SilentlyContinue
Remove-Job $backendJob -Force -ErrorAction SilentlyContinue
Remove-Job $frontendJob -Force -ErrorAction SilentlyContinue
Write-Host "`nStopped." -ForegroundColor Yellow
}
}
function Invoke-Check {
param(
[string]$Name,
[string]$WorkDir,
[scriptblock]$Command
)
Write-Host "Running $Name..." -ForegroundColor Yellow
$prev = $PWD
if ($WorkDir) { Set-Location $WorkDir }
$ErrorActionPreference = 'Continue'
& $Command 2>&1 | Tee-Object -Variable output | Out-Null
$exitCode = $LASTEXITCODE
if ($WorkDir) { Set-Location $prev }
if ($exitCode -eq 0) {
Write-Pass $Name
return $true
}
else {
Write-Fail $Name
$output | ForEach-Object { Write-Host " $_" }
return $false
}
}
function Run-Validate {
Write-Header "ServerKit Validation Suite"
$failed = 0
$passed = 0
# --- ESLint (warn-only, does not block) ---
Write-Host "Running ESLint..." -ForegroundColor Yellow
Push-Location $FrontendDir
$ErrorActionPreference = 'Continue'
npm run lint 2>&1 | Out-Null
$eslintExit = $LASTEXITCODE
Pop-Location
if ($eslintExit -eq 0) {
Write-Pass "ESLint"
$passed++
}
else {
Write-Host " WARN " -ForegroundColor Yellow -NoNewline
Write-Host "ESLint (has warnings/errors - run 'cd frontend && npm run lint' for details)"
$passed++ # count as pass — pre-existing issues should not block
}
# --- Bandit ---
if (Get-Command bandit -ErrorAction SilentlyContinue) {
if (Invoke-Check "Bandit (security scan)" "" { bandit -r "$BackendDir\app" --ini "$BackendDir\.bandit" --severity-level medium }) { $passed++ } else { $failed++ }
}
else {
Write-Fail "Bandit (not installed - pip install bandit)"
$failed++
}
# --- Pytest ---
if (Test-Path "$BackendDir\venv\Scripts\Activate.ps1") {
& "$BackendDir\venv\Scripts\Activate.ps1"
}
if (Invoke-Check "Pytest" $BackendDir { pytest --tb=short -q }) { $passed++ } else { $failed++ }
# --- Frontend build ---
if (Invoke-Check "Frontend build" $FrontendDir { npm run build }) { $passed++ } else { $failed++ }
# --- Summary ---
Write-Header "Results"
Write-Host " Passed: $passed" -ForegroundColor Green
if ($failed -gt 0) {
Write-Host " Failed: $failed" -ForegroundColor Red
exit 1
}
else {
Write-Host " All checks passed!" -ForegroundColor Green
}
}
function Run-ValidateWatch {
Write-Host "Watching for changes... (Ctrl+C to stop)" -ForegroundColor DarkGray
Run-Validate
$watcher = [System.IO.FileSystemWatcher]::new()
$watcher.Path = $ProjectRoot
$watcher.IncludeSubdirectories = $true
$watcher.Filter = '*.*'
$watcher.EnableRaisingEvents = $true
$lastRun = [DateTime]::MinValue
try {
while ($true) {
$result = $watcher.WaitForChanged([System.IO.WatcherChangeTypes]::All, 2000)
if (-not $result.TimedOut) {
$ext = [System.IO.Path]::GetExtension($result.Name)
if ($ext -in '.py', '.js', '.jsx', '.ts', '.tsx') {
$now = [DateTime]::Now
if (($now - $lastRun).TotalSeconds -gt 3) {
$lastRun = $now
Write-Host "`nChange detected: $($result.Name)" -ForegroundColor Yellow
Run-Validate
}
}
}
}
}
finally {
$watcher.Dispose()
}
}
# --- Main ---
switch ($Mode) {
'backend' { Start-Backend }
'frontend' { Start-Frontend }
'validate' { Run-ValidateWatch }
default { Start-Both }
}