-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.ps1
More file actions
421 lines (367 loc) · 16.6 KB
/
Copy pathinstall.ps1
File metadata and controls
421 lines (367 loc) · 16.6 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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
<#
.SYNOPSIS
OpenProcure — One-command Windows installer
.DESCRIPTION
Installs all dependencies, starts Docker services, configures environment,
and launches OpenProcure on your machine.
.PARAMETER ProjectDir
Where to install the project (default: current directory\openprocure)
.PARAMETER SkipDocker
Skip Docker service setup (if you already have Postgres/Redis running)
.PARAMETER SkipBuild
Skip the build step after installing dependencies
#>
param(
[string]$ProjectDir = (Join-Path (Get-Location) "openprocure"),
[switch]$SkipDocker,
[switch]$SkipBuild
)
#━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
# Helpers
#━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
$Host.UI.RawUI.ForegroundColor = "White"
function Write-Title($Text) {
$Host.UI.RawUI.ForegroundColor = "Cyan"
Write-Host ""
Write-Host "╔══════════════════════════════════════════════════════╗"
Write-Host "║ $($Text.PadRight(52))║"
Write-Host "╚══════════════════════════════════════════════════════╝"
Write-Host ""
$Host.UI.RawUI.ForegroundColor = "White"
}
function Write-Step($Text) {
$Host.UI.RawUI.ForegroundColor = "Yellow"
Write-Host " → $Text" -NoNewline
$Host.UI.RawUI.ForegroundColor = "White"
}
function Write-Ok {
$Host.UI.RawUI.ForegroundColor = "Green"
Write-Host " ✓"
$Host.UI.RawUI.ForegroundColor = "White"
}
function Write-Done($Text) {
$Host.UI.RawUI.ForegroundColor = "Green"
Write-Host " ✓ $Text"
$Host.UI.RawUI.ForegroundColor = "White"
}
function Write-Error($Text) {
$Host.UI.RawUI.ForegroundColor = "Red"
Write-Host " ✗ $Text"
$Host.UI.RawUI.ForegroundColor = "White"
}
function Write-Info($Text) {
$Host.UI.RawUI.ForegroundColor = "DarkGray"
Write-Host " $Text"
$Host.UI.RawUI.ForegroundColor = "White"
}
function Test-Command($Name) {
$null -ne (Get-Command $Name -ErrorAction SilentlyContinue)
}
function Get-RandomHex($Bytes = 32) {
$rng = [System.Security.Cryptography.RandomNumberGenerator]::Create()
$buf = [byte[]]::new($Bytes)
$rng.GetBytes($buf)
return -join ($buf | ForEach-Object { '{0:x2}' -f $_ })
}
function Read-EnvVar($Prompt, $Default, $Secret = $false) {
$defaultStr = if ($Default) { " [$Default]" } else { "" }
Write-Host " $Prompt$defaultStr" -NoNewline -ForegroundColor White
if ($Secret) {
$val = Read-Host -AsSecureString
$bstr = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($val)
$val = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($bstr)
if ([string]::IsNullOrWhiteSpace($val)) { return $Default }
return $val
} else {
$val = Read-Host
if ([string]::IsNullOrWhiteSpace($val)) { return $Default }
return $val
}
}
#━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
# Welcome
#━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
Clear-Host
Write-Host @"
╔════════════════════════════════════════════════╗
║ ║
║ 🚀 OpenProcure Installer ║
║ Spend Intelligence & Procurement OS ║
║ ║
╚════════════════════════════════════════════════╝
"@ -ForegroundColor Cyan
Write-Host " This will set up OpenProcure on your Windows machine." -ForegroundColor White
Write-Host " It will install dependencies, start services, and configure the project.`n" -ForegroundColor DarkGray
#━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
# 1. Prerequisites
#━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
Write-Title "Step 1: Checking prerequisites"
$missing = @()
# Node.js
Write-Step "Node.js (>= 20)..."
if (Test-Command node) {
$ver = node -v
Write-Host " $ver" -ForegroundColor Green
if ([version]($ver -replace 'v','') -lt [version]"20.0.0") {
Write-Error "Node.js >= 20 required, found $ver"
$missing += "Node.js >= 20"
}
} else {
Write-Error "Not found"
$missing += "Node.js >= 20"
}
# npm
Write-Step "npm..."
if (Test-Command npm) {
$ver = npm -v
Write-Host " v$ver" -ForegroundColor Green
} else {
Write-Error "Not found"
$missing += "npm"
}
# Git
Write-Step "Git..."
if (Test-Command git) {
Write-Host " $(git --version)" -ForegroundColor Green
} else {
Write-Error "Not found"
$missing += "Git"
}
# Docker
if (-not $SkipDocker) {
Write-Step "Docker Desktop..."
$hasDocker = $false
if (Test-Command docker) {
$hasDocker = $true
Write-Host " $(docker --version)" -ForegroundColor Green
} else {
Write-Error "Not found"
}
}
# If missing essentials, offer to install
if ($missing.Count -gt 0) {
Write-Host ""
Write-Error "Missing prerequisites: $($missing -join ', ')"
Write-Host ""
$install = Read-Host " Would you like to install them now? (y/N)"
if ($install -eq 'y') {
foreach ($item in $missing) {
switch ($item) {
"Node.js >= 20" {
Write-Step "Installing Node.js via winget..."
try {
winget install OpenJS.NodeJS.LTS --silent --accept-package-agreements > $null 2>&1
Write-Ok
} catch {
Write-Error "Failed. Download from https://nodejs.org"
}
}
"Git" {
Write-Step "Installing Git via winget..."
try {
winget install Git.Git --silent --accept-package-agreements > $null 2>&1
Write-Ok
} catch {
Write-Error "Failed. Download from https://git-scm.com"
}
}
}
}
# Refresh PATH for this session
$env:Path = [Environment]::GetEnvironmentVariable("Path", "Machine") + ";" + [Environment]::GetEnvironmentVariable("Path", "User")
} else {
Write-Error "Please install missing prerequisites and re-run this installer."
exit 1
}
}
if ($hasDocker -and -not $SkipDocker) {
# Check Docker is running
Write-Step "Docker Desktop is running..."
try {
$dockerInfo = docker info --format '{{.ServerVersion}}' 2>$null
if ($dockerInfo) {
Write-Host " yes (engine v$dockerInfo)" -ForegroundColor Green
} else {
Write-Host " no" -ForegroundColor Red
Write-Info "Start Docker Desktop manually, then re-run or use -SkipDocker"
$dockerRunning = $false
}
} catch {
Write-Host " no" -ForegroundColor Red
Write-Info "Start Docker Desktop manually, then re-run or use -SkipDocker"
$dockerRunning = $false
}
}
#━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
# 2. Project setup
#━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
Write-Title "Step 2: Setting up project"
if (Test-Path $ProjectDir) {
Write-Step "Project directory exists: $ProjectDir"
Write-Host "" -ForegroundColor Green
Set-Location $ProjectDir
} else {
Write-Step "Cloning repository..."
$repo = Read-EnvVar "Repository URL" "https://github.com/your-org/openprocure.git"
try {
git clone $repo $ProjectDir 2>&1 | Out-Null
Write-Ok
Set-Location $ProjectDir
} catch {
Write-Error "Clone failed: $_"
exit 1
}
}
#━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
# 3. Environment configuration
#━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
Write-Title "Step 3: Configuring environment"
# Root .env
$rootEnv = Join-Path $ProjectDir ".env"
if (Test-Path $rootEnv) {
Write-Step ".env exists, backing up to .env.bak..."
Copy-Item $rootEnv "$rootEnv.bak" -Force
Write-Ok
}
Write-Host "`n Enter your Clerk authentication credentials (required)." -ForegroundColor DarkGray
Write-Host " Sign up free at https://dashboard.clerk.com`n" -ForegroundColor DarkGray
$clerkSecret = Read-EnvVar "CLERK_SECRET_KEY" "" $true
$clerkPub = Read-EnvVar "CLERK_PUBLISHABLE_KEY" "" $false
$clerkIssuer = Read-EnvVar "CLERK_ISSUER (your-domain.clerk.accounts.dev)" "" $false
$frontendUrl = Read-EnvVar "FRONTEND_URL" "http://localhost:3000"
$internalToken = Read-EnvVar "INTERNAL_API_TOKEN" (Get-RandomHex 16)
$encryptionKey = if ((Read-Host "`n Generate a secure BYOK_ENCRYPTION_KEY? (Y/n)") -ne 'n') { Get-RandomHex 32 } else { Read-EnvVar "BYOK_ENCRYPTION_KEY" "" $true }
Write-Step "Writing root .env..."
@"
# OpenProcure — Local Environment (generated by installer)
NODE_ENV=development
PORT=4000
FRONTEND_URL=$frontendUrl
ADDITIONAL_ORIGINS=https://openprocure.pages.dev
DATABASE_URL=postgresql://openprocure:openprocure_dev@localhost:5432/openprocure
CLERK_SECRET_KEY=$clerkSecret
CLERK_PUBLISHABLE_KEY=$clerkPub
CLERK_WEBHOOK_SECRET=whsec_$(Get-RandomHex 16)
CLERK_ISSUER=$clerkIssuer
INTERNAL_API_TOKEN=$internalToken
BYOK_ENCRYPTION_KEY=$encryptionKey
UPSTASH_REDIS_REST_URL=
UPSTASH_REDIS_REST_TOKEN=
"@ | Set-Content $rootEnv -Encoding UTF8
Write-Ok
# Web .env.local
$webEnvDir = Join-Path $ProjectDir "apps\web"
$webEnv = Join-Path $webEnvDir ".env.local"
Write-Step "Writing web .env.local..."
@"
NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY=$clerkPub
CLERK_SECRET_KEY=$clerkSecret
NEXT_PUBLIC_CLERK_SIGN_IN_URL=/sign-in
NEXT_PUBLIC_CLERK_SIGN_UP_URL=/sign-up
NEXT_PUBLIC_CLERK_AFTER_SIGN_IN_URL=/dashboard
NEXT_PUBLIC_CLERK_AFTER_SIGN_UP_URL=/dashboard
NEXT_PUBLIC_API_URL=http://localhost:4000/api/v1
"@ | Set-Content $webEnv -Encoding UTF8
Write-Ok
#━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
# 4. Docker services
#━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
if (-not $SkipDocker -and $dockerRunning) {
Write-Title "Step 4: Starting Docker services (PostgreSQL, Redis, AI)"
Write-Step "Starting containers..."
try {
docker compose up -d 2>&1 | Out-Null
Write-Ok
Write-Step "Waiting for PostgreSQL to be ready..."
$ready = $false
for ($i = 0; $i -lt 30; $i++) {
$check = docker exec openprocure-postgres pg_isready -U openprocure 2>$null
if ($check -match "accepting connections") { $ready = $true; break }
Start-Sleep -Seconds 2
}
if ($ready) { Write-Done "PostgreSQL is ready" } else { Write-Error "Timed out waiting for PostgreSQL" }
} catch {
Write-Error "Docker compose failed: $_"
}
} elseif (-not $SkipDocker) {
Write-Info "Skipping Docker — start PostgreSQL and Redis manually, or run: docker compose up -d"
}
#━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
# 5. Install dependencies
#━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
Write-Title "Step 5: Installing dependencies"
Write-Step "npm install..."
try {
npm install --loglevel=error 2>&1 | ForEach-Object {
if ($_ -match "error") { Write-Error $_ }
}
if ($LASTEXITCODE -eq 0) { Write-Ok } else { throw "npm install failed" }
} catch {
Write-Error "npm install failed: $_"
exit 1
}
#━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
# 6. Database
#━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
Write-Title "Step 6: Setting up database"
Write-Step "Generating Prisma client..."
try {
npm run db:generate 2>&1 | Out-Null
Write-Ok
} catch {
Write-Error "Prisma generate failed: $_"
}
Write-Step "Pushing database schema..."
try {
npm run db:push 2>&1 | Out-Null
Write-Ok
} catch {
Write-Error "Prisma push failed — ensure PostgreSQL is running: $_"
}
#━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
# 7. Build (optional)
#━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
if (-not $SkipBuild) {
Write-Title "Step 7: Building project"
Write-Step "turbo build..."
try {
npm run build 2>&1 | ForEach-Object {
if ($_ -match "error") { Write-Host " $_" -ForegroundColor Red }
}
if ($LASTEXITCODE -eq 0) { Write-Ok } else { throw "Build failed" }
} catch {
Write-Error "Build failed: $_"
Write-Info "You can still run 'npm run dev' to start in development mode"
}
}
#━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
# Done!
#━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
Write-Title "✅ Installation Complete!"
$Host.UI.RawUI.ForegroundColor = "Green"
Write-Host @"
┌─────────────────────────────────────────────────────┐
│ │
│ OpenProcure is ready to go! │
│ │
│ Start development: │
│ cd "$ProjectDir" │
│ npm run dev │
│ │
│ Web app: http://localhost:3000 │
│ API: http://localhost:4000/api/v1 │
│ API docs: http://localhost:4000/docs │
│ DB Studio: npm run db:studio │
│ │
│ Need help? Check DEPLOY.md │
│ │
└─────────────────────────────────────────────────────┘
"@
$Host.UI.RawUI.ForegroundColor = "White"
# Quick-start prompt
$startNow = Read-Host "`n Start dev servers now? (y/N)"
if ($startNow -eq 'y') {
Write-Title "Starting development servers"
Write-Info "Press Ctrl+C to stop both servers"
npm run dev
}