-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.ps1
More file actions
331 lines (271 loc) · 10.7 KB
/
Copy pathbuild.ps1
File metadata and controls
331 lines (271 loc) · 10.7 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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
<#
.SYNOPSIS
OctoCAT Supply Chain Management - PowerShell task runner (Makefile equivalent).
.DESCRIPTION
Cross-platform PowerShell script that mirrors the project Makefile so Windows
users without `make` can run the same tasks. Run `./build.ps1 help` to list
available targets.
.PARAMETER Target
The task to execute (e.g. install, dev, build, test). Defaults to `help`.
.EXAMPLE
./build.ps1 install
./build.ps1 dev
./build.ps1 build-api
#>
[CmdletBinding()]
param(
[Parameter(Position = 0)]
[string]$Target = 'help'
)
$ErrorActionPreference = 'Stop'
Set-StrictMode -Version Latest
# ---- Configuration ----------------------------------------------------------
$RepoRoot = $PSScriptRoot
$ApiDir = Join-Path $RepoRoot 'api'
$FrontendDir = Join-Path $RepoRoot 'frontend'
# ---- Helpers ----------------------------------------------------------------
function Write-Section {
param([string]$Message)
Write-Host ""
Write-Host "==> $Message" -ForegroundColor Cyan
}
function Invoke-In {
<#
Runs a script block inside the given directory, restoring the original
location afterwards. Throws if the command exits with a non-zero code.
#>
param(
[Parameter(Mandatory)] [string]$Path,
[Parameter(Mandatory)] [scriptblock]$Action
)
if (-not (Test-Path $Path)) {
throw "Directory not found: $Path"
}
Push-Location $Path
try {
& $Action
if ($LASTEXITCODE -ne 0) {
throw "Command failed in '$Path' (exit code $LASTEXITCODE)."
}
}
finally {
Pop-Location
}
}
function Remove-IfExists {
param([Parameter(Mandatory)] [string[]]$Paths)
foreach ($p in $Paths) {
if (Test-Path $p) {
Write-Host " removing $p"
Remove-Item -Recurse -Force -ErrorAction SilentlyContinue -Path $p
}
}
}
function Ensure-Installed {
<#
Runs `npm install` in $Path if node_modules is missing.
Idempotent and cheap when deps are already present.
#>
param([Parameter(Mandatory)] [string]$Path)
$nodeModules = Join-Path $Path 'node_modules'
if (-not (Test-Path $nodeModules)) {
Write-Section "Installing dependencies in $([IO.Path]::GetFileName($Path)) (node_modules missing)"
Invoke-In $Path { npm install }
}
}
# ---- Targets ----------------------------------------------------------------
$Targets = [ordered]@{}
function Register-Target {
param(
[Parameter(Mandatory)] [string]$Name,
[Parameter(Mandatory)] [string]$Group,
[Parameter(Mandatory)] [string]$Description,
[Parameter(Mandatory)] [scriptblock]$Action
)
$Targets[$Name] = [pscustomobject]@{
Name = $Name
Group = $Group
Description = $Description
Action = $Action
}
}
# General -----------------------------------------------------
Register-Target -Name 'help' -Group 'General' -Description 'Display this help message' -Action {
Write-Host ""
Write-Host "Usage:" -ForegroundColor White
Write-Host " ./build.ps1 " -NoNewline
Write-Host "<target>" -ForegroundColor Cyan
Write-Host ""
$grouped = $Targets.Values | Group-Object Group
foreach ($g in $grouped) {
Write-Host $g.Name -ForegroundColor White
foreach ($t in $g.Group) {
$name = $t.Name.PadRight(16)
Write-Host (" ") -NoNewline
Write-Host $name -ForegroundColor Cyan -NoNewline
Write-Host $t.Description
}
Write-Host ""
}
}
Register-Target -Name 'info' -Group 'General' -Description 'Show detected OS and directories' -Action {
Write-Host "Detected OS: Windows (PowerShell $($PSVersionTable.PSVersion))"
Write-Host "Repository root: $RepoRoot"
Write-Host "API Directory: $ApiDir"
Write-Host "Frontend Directory: $FrontendDir"
}
# Installation ------------------------------------------------
Register-Target -Name 'install' -Group 'Installation' -Description 'Install all dependencies' -Action {
Write-Section 'Installing API dependencies'
Invoke-In $ApiDir { npm install }
Write-Section 'Installing Frontend dependencies'
Invoke-In $FrontendDir { npm install }
}
# Development -------------------------------------------------
Register-Target -Name 'dev' -Group 'Development' -Description 'Start dev servers (API + Frontend)' -Action {
Ensure-Installed -Path $ApiDir
Ensure-Installed -Path $FrontendDir
Write-Section 'Starting API + Frontend (concurrently)'
# Use concurrently to run both processes and forward Ctrl+C.
# cmd.exe `set` is used so VITE_API_URL is exported for the frontend child.
$apiCmd = "cd /d `"$ApiDir`" && npm run dev"
$frontendCmd = "cd /d `"$FrontendDir`" && set VITE_API_URL=http://localhost:3000 && npm run dev"
Invoke-In $RepoRoot {
npx --yes concurrently --kill-others --names "api,web" --prefix-colors "blue,magenta" `
$apiCmd $frontendCmd
}
}
Register-Target -Name 'dev-api' -Group 'Development' -Description 'Start only the API dev server' -Action {
Ensure-Installed -Path $ApiDir
Invoke-In $ApiDir { npm run dev }
}
Register-Target -Name 'dev-frontend' -Group 'Development' -Description 'Start only the frontend dev server' -Action {
Ensure-Installed -Path $FrontendDir
Invoke-In $FrontendDir { npm run dev }
}
# Database ----------------------------------------------------
Register-Target -Name 'db-init' -Group 'Database' -Description 'Initialize database schema' -Action {
Ensure-Installed -Path $ApiDir
Invoke-In $ApiDir { npm run db:init }
}
Register-Target -Name 'db-seed' -Group 'Database' -Description 'Initialize and seed database' -Action {
Ensure-Installed -Path $ApiDir
Invoke-In $ApiDir { npm run db:seed }
}
# Building ----------------------------------------------------
Register-Target -Name 'build' -Group 'Building' -Description 'Build all projects' -Action {
Ensure-Installed -Path $ApiDir
Ensure-Installed -Path $FrontendDir
Write-Section 'Building API'
Invoke-In $ApiDir { npm run build }
Write-Section 'Building Frontend'
Invoke-In $FrontendDir { npm run build }
}
Register-Target -Name 'build-api' -Group 'Building' -Description 'Build only the API' -Action {
Ensure-Installed -Path $ApiDir
Invoke-In $ApiDir { npm run build }
}
Register-Target -Name 'build-frontend' -Group 'Building' -Description 'Build only the frontend' -Action {
Ensure-Installed -Path $FrontendDir
Invoke-In $FrontendDir { npm run build }
}
# Testing -----------------------------------------------------
Register-Target -Name 'test' -Group 'Testing' -Description 'Run all tests' -Action {
Ensure-Installed -Path $ApiDir
Ensure-Installed -Path $FrontendDir
Write-Section 'API tests'
Invoke-In $ApiDir { npm run test }
Write-Section 'Frontend tests'
Invoke-In $FrontendDir { npm run test }
}
Register-Target -Name 'test-api' -Group 'Testing' -Description 'Run API tests' -Action {
Ensure-Installed -Path $ApiDir
Invoke-In $ApiDir { npm run test }
}
Register-Target -Name 'test-frontend' -Group 'Testing' -Description 'Run frontend tests' -Action {
Ensure-Installed -Path $FrontendDir
Invoke-In $FrontendDir { npm run test }
}
Register-Target -Name 'test-e2e' -Group 'Testing' -Description 'Run end-to-end tests' -Action {
Ensure-Installed -Path $FrontendDir
Invoke-In $FrontendDir { npm run test:e2e }
}
Register-Target -Name 'test-coverage' -Group 'Testing' -Description 'Run tests with coverage' -Action {
Ensure-Installed -Path $ApiDir
Invoke-In $ApiDir { npm run 'test:coverage' }
}
# Linting -----------------------------------------------------
Register-Target -Name 'lint' -Group 'Linting' -Description 'Lint all code' -Action {
Ensure-Installed -Path $ApiDir
Ensure-Installed -Path $FrontendDir
Invoke-In $ApiDir { npm run lint }
Invoke-In $FrontendDir { npm run lint }
}
Register-Target -Name 'lint-fix' -Group 'Linting' -Description 'Lint and auto-fix issues' -Action {
Ensure-Installed -Path $ApiDir
Ensure-Installed -Path $FrontendDir
Invoke-In $ApiDir { npm run 'lint:fix' }
Invoke-In $FrontendDir { npm run lint -- --fix }
}
Register-Target -Name 'format' -Group 'Linting' -Description 'Format code with prettier' -Action {
Invoke-In $RepoRoot {
npx --yes prettier --write "api/**/*.{ts,tsx}" "frontend/**/*.{ts,tsx}"
}
}
# Code Generation ---------------------------------------------
Register-Target -Name 'swagger' -Group 'Code Generation' -Description 'Regenerate api-swagger.json' -Action {
Ensure-Installed -Path $ApiDir
Invoke-In $ApiDir { npm run 'swagger:generate' }
}
# Production --------------------------------------------------
Register-Target -Name 'start' -Group 'Production' -Description 'Start production server' -Action {
Ensure-Installed -Path $ApiDir
Invoke-In $ApiDir { npm start }
}
Register-Target -Name 'up' -Group 'Production' -Description 'Install + build + run dev (one-shot)' -Action {
& $Targets['install'].Action
& $Targets['build'].Action
& $Targets['dev'].Action
}
# Docker ------------------------------------------------------
Register-Target -Name 'docker-build' -Group 'Docker' -Description 'Build Docker images' -Action {
Invoke-In $RepoRoot { docker compose build }
}
Register-Target -Name 'docker-up' -Group 'Docker' -Description 'Start Docker containers' -Action {
Invoke-In $RepoRoot { docker compose up }
}
Register-Target -Name 'docker-down' -Group 'Docker' -Description 'Stop Docker containers' -Action {
Invoke-In $RepoRoot { docker compose down }
}
# Cleaning ----------------------------------------------------
Register-Target -Name 'clean' -Group 'Cleaning' -Description 'Clean build artifacts and dependencies' -Action {
Write-Section 'Removing node_modules and dist directories'
Remove-IfExists -Paths @(
(Join-Path $RepoRoot 'node_modules'),
(Join-Path $ApiDir 'node_modules'),
(Join-Path $FrontendDir 'node_modules'),
(Join-Path $ApiDir 'dist'),
(Join-Path $FrontendDir 'dist')
)
Write-Section 'Removing SQLite database files'
Get-ChildItem -Path $ApiDir -Filter '*.db*' -File -ErrorAction SilentlyContinue |
ForEach-Object {
Write-Host " removing $($_.FullName)"
Remove-Item -Force $_.FullName
}
}
# ---- Dispatch ---------------------------------------------------------------
if (-not $Targets.Contains($Target)) {
Write-Host "Unknown target: $Target" -ForegroundColor Red
Write-Host ""
& $Targets['help'].Action
exit 1
}
try {
& $Targets[$Target].Action
}
catch {
Write-Host ""
Write-Host "Target '$Target' failed: $($_.Exception.Message)" -ForegroundColor Red
exit 1
}