-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdev.ps1
More file actions
154 lines (130 loc) · 4.52 KB
/
Copy pathdev.ps1
File metadata and controls
154 lines (130 loc) · 4.52 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
# dev.ps1 - Idempotent local dev setup and start for terrymurray.com
# Usage: .\dev.ps1
Set-StrictMode -Version Latest
$ErrorActionPreference = "Stop"
$root = $PSScriptRoot
Push-Location $root
function Write-Step($msg) { Write-Host "`n>> $msg" -ForegroundColor Cyan }
function Write-Ok($msg) { Write-Host " $msg" -ForegroundColor Green }
function Write-Skip($msg) { Write-Host " $msg (skipped)" -ForegroundColor DarkGray }
function Write-Warn($msg) { Write-Host " $msg" -ForegroundColor Yellow }
try {
# -- 1. Check prerequisites
Write-Step "Checking prerequisites"
if (-not (Get-Command node -ErrorAction SilentlyContinue)) {
throw "Node.js is not installed. Install from https://nodejs.org"
}
$nodeVersion = node --version
Write-Ok "Node.js $nodeVersion"
if (-not (Get-Command docker -ErrorAction SilentlyContinue)) {
throw "Docker is not installed. Install from https://docker.com"
}
Write-Ok "Docker available"
# -- 2. Install dependencies
Write-Step "Installing dependencies"
if (Test-Path "node_modules/.package-lock.json") {
$pkgTime = (Get-Item "package.json").LastWriteTime
$lockTime = (Get-Item "node_modules/.package-lock.json").LastWriteTime
if ($pkgTime -gt $lockTime) {
npm install
Write-Ok "Dependencies updated"
}
else {
Write-Skip "node_modules up to date"
}
}
else {
npm install
Write-Ok "Dependencies installed"
}
# -- 3. Check .env.local
Write-Step "Checking environment"
if (-not (Test-Path ".env.local")) {
throw ".env.local not found. Copy from the README template and fill in your values."
}
$envContent = Get-Content ".env.local" -Raw
$placeholders = @("your-google-client-id", "your-google-client-secret", "your-email@gmail.com")
$warnings = @()
foreach ($ph in $placeholders) {
if ($envContent -match [regex]::Escape($ph)) {
$warnings += $ph
}
}
if ($warnings.Count -gt 0) {
Write-Warn "Placeholder values detected in .env.local:"
foreach ($w in $warnings) { Write-Warn " - $w" }
Write-Warn "The app may not fully work until these are replaced."
}
else {
Write-Ok ".env.local configured"
}
# -- 4. Start PostgreSQL via Docker
Write-Step "Starting PostgreSQL (Docker)"
$ErrorActionPreference = "Continue"
$containerStatus = docker inspect -f "{{.State.Running}}" terrymurray-db 2>&1
$ErrorActionPreference = "Stop"
if ("$containerStatus" -eq "true") {
Write-Skip "terrymurray-db already running"
}
else {
docker compose up -d db
Write-Ok "terrymurray-db started"
# Wait for PostgreSQL to accept connections
Write-Host " Waiting for PostgreSQL to be ready..." -ForegroundColor White
$ready = $false
for ($i = 0; $i -lt 30; $i++) {
$ErrorActionPreference = "Continue"
$null = docker exec terrymurray-db pg_isready -U tmdotcom 2>&1
$ErrorActionPreference = "Stop"
if ($LASTEXITCODE -eq 0) {
$ready = $true
break
}
Start-Sleep -Seconds 1
}
if ($ready) {
Write-Ok "PostgreSQL is ready"
}
else {
throw "PostgreSQL did not become ready within 30 seconds"
}
}
# -- 5. Generate Prisma client + run migrations
Write-Step "Running Prisma migrations"
$ErrorActionPreference = "Continue"
$null = npx prisma generate 2>&1
if ($LASTEXITCODE -eq 0) {
Write-Ok "Prisma client generated"
}
else {
Write-Warn "Prisma generate failed - check DATABASE_URL in .env.local"
}
npx prisma migrate deploy 2>&1
if ($LASTEXITCODE -eq 0) {
Write-Ok "Migrations applied"
}
else {
Write-Warn "Migration failed - is DATABASE_URL correct in .env.local?"
}
$ErrorActionPreference = "Stop"
# -- 6. Ensure uploads directory exists
Write-Step "Checking uploads directory"
if (-not (Test-Path "uploads/images")) {
New-Item -ItemType Directory -Path "uploads/images" -Force | Out-Null
Write-Ok "Created uploads/images/"
}
else {
Write-Skip "uploads/images/ exists"
}
# -- 7. Start dev server
Write-Step "Starting dev server"
Write-Host " http://localhost:3000`n" -ForegroundColor White
npm run dev
}
catch {
Write-Host "`nERROR: $_" -ForegroundColor Red
exit 1
}
finally {
Pop-Location
}