param( [ValidateSet("sqlite", "postgres")] [string]$DbMode = "sqlite", [string]$DatabaseUrl = "", [switch]$SkipInstall ) $ErrorActionPreference = "Stop" function Test-CommandExists { param([string]$Name) return $null -ne (Get-Command $Name -ErrorAction SilentlyContinue) } function Get-PythonCommand { $candidates = @( "py -3.12", "py -3.11", "py -3", "python" ) foreach ($candidate in $candidates) { try { $parts = $candidate.Split(" ", 2) $exe = $parts[0] $args = if ($parts.Count -gt 1) { $parts[1] } else { "" } if (-not (Test-CommandExists $exe)) { continue } if ([string]::IsNullOrWhiteSpace($args)) { & $exe --version | Out-Null } else { & $exe $args --version | Out-Null } return $candidate } catch { continue } } throw "Python was not found. Please install Python 3.11+." } function Ensure-Backend { param( [string]$BackendPath, [string]$PyCmd, [string]$Mode, [string]$DbUrl, [bool]$DoInstall ) Write-Host "[backend] preparing..." -ForegroundColor Cyan Set-Location $BackendPath if (-not (Test-Path ".\venv\Scripts\python.exe")) { Write-Host "[backend] creating virtualenv..." -ForegroundColor Yellow if ($PyCmd -like "py *") { $parts = $PyCmd.Split(" ", 2) & $parts[0] $parts[1] -m venv venv } else { & $PyCmd -m venv venv } } $venvPy = Join-Path $BackendPath "venv\Scripts\python.exe" if ($DoInstall) { Write-Host "[backend] installing python dependencies..." -ForegroundColor Yellow & $venvPy -m pip install --upgrade pip & $venvPy -m pip install -r requirements.txt } if ($Mode -eq "sqlite") { Write-Host "[backend] initializing sqlite data..." -ForegroundColor Yellow & $venvPy .\init_local_db.py } else { if (-not [string]::IsNullOrWhiteSpace($DbUrl)) { $env:DATABASE_URL = $DbUrl Write-Host "[backend] DATABASE_URL set from parameter." -ForegroundColor Yellow } elseif (-not $env:DATABASE_URL) { Write-Warning "DbMode=postgres but DATABASE_URL is not set. Using app default from backend/app/config.py." } Write-Host "[backend] initializing postgres data..." -ForegroundColor Yellow & $venvPy .\init_db.py & $venvPy .\fix_admin.py } } function Ensure-Frontend { param( [string]$FrontendPath, [bool]$DoInstall ) if (-not (Test-CommandExists "npm")) { throw "npm was not found. Please install Node.js (with npm)." } Write-Host "[frontend] preparing..." -ForegroundColor Cyan Set-Location $FrontendPath if ($DoInstall) { Write-Host "[frontend] installing npm dependencies..." -ForegroundColor Yellow npm install } } function Start-BackendWindow { param( [string]$BackendPath, [string]$Mode, [string]$DbUrl ) $dbExport = "" if ($Mode -eq "postgres" -and -not [string]::IsNullOrWhiteSpace($DbUrl)) { $escaped = $DbUrl.Replace("'", "''") $dbExport = "`$env:DATABASE_URL='$escaped'; " } $cmd = "$dbExport Set-Location '$BackendPath'; .\venv\Scripts\python.exe .\run.py" Start-Process powershell -ArgumentList @("-NoExit", "-ExecutionPolicy", "Bypass", "-Command", $cmd) | Out-Null } function Start-FrontendWindow { param([string]$FrontendPath) $cmd = "Set-Location '$FrontendPath'; npm run dev" Start-Process powershell -ArgumentList @("-NoExit", "-ExecutionPolicy", "Bypass", "-Command", $cmd) | Out-Null } $repoRoot = Split-Path -Parent $PSScriptRoot $backendDir = Join-Path $repoRoot "backend" $frontendDir = Join-Path $repoRoot "frontend" if (-not (Test-Path $backendDir)) { throw "backend directory not found: $backendDir" } if (-not (Test-Path $frontendDir)) { throw "frontend directory not found: $frontendDir" } $pythonCmd = Get-PythonCommand $doInstall = -not $SkipInstall.IsPresent Write-Host "== HXOPS local startup ==" -ForegroundColor Green Write-Host "mode: $DbMode" -ForegroundColor Green Ensure-Backend -BackendPath $backendDir -PyCmd $pythonCmd -Mode $DbMode -DbUrl $DatabaseUrl -DoInstall $doInstall Ensure-Frontend -FrontendPath $frontendDir -DoInstall $doInstall Start-BackendWindow -BackendPath $backendDir -Mode $DbMode -DbUrl $DatabaseUrl Start-FrontendWindow -FrontendPath $frontendDir Write-Host "" Write-Host "Started in two new PowerShell windows." -ForegroundColor Green Write-Host "Frontend URL: http://127.0.0.1:8881" -ForegroundColor Green Write-Host "Default local admin (sqlite/init scripts): admin / Hxit@54321" -ForegroundColor Green