sojorn/run_web_chrome.ps1

89 lines
2.6 KiB
PowerShell

param(
[string]$EnvPath = (Join-Path $PSScriptRoot ".env"),
[int]$Port = 8002,
[string]$Renderer = "auto", # Options: auto, canvaskit, html
[switch]$NoWasmDryRun
)
function Parse-Env($path) {
$vals = @{}
if (-not (Test-Path $path)) {
Write-Host "No .env file found at ${path}. Using defaults." -ForegroundColor Yellow
# Set default API_BASE_URL since no .env exists
$vals['API_BASE_URL'] = 'https://api.sojorn.net/api/v1'
return $vals
}
Get-Content $path | ForEach-Object {
$line = $_.Trim()
if ($line.Length -eq 0 -or $line.StartsWith('#')) { return }
$parts = $line -split '=', 2
if ($parts.Count -lt 2) { return }
$key = $parts[0].Trim()
$value = $parts[1].Trim()
if ($value.StartsWith('"') -and $value.EndsWith('"')) {
$value = $value.Substring(1, $value.Length - 2)
}
$vals[$key] = $value
}
return $vals
}
$values = Parse-Env $EnvPath
# Collect dart-defines we actually use on web.
$defineArgs = @()
$keysOfInterest = @('API_BASE_URL', 'FIREBASE_WEB_VAPID_KEY')
foreach ($k in $keysOfInterest) {
if ($values.ContainsKey($k) -and -not [string]::IsNullOrWhiteSpace($values[$k])) {
$defineArgs += "--dart-define=$k=$($values[$k])"
}
}
# Ensure API_BASE_URL is set
if (-not $values.ContainsKey('API_BASE_URL') -or [string]::IsNullOrWhiteSpace($values['API_BASE_URL'])) {
$currentApi = 'https://api.sojorn.net/api/v1'
$defineArgs += "--dart-define=API_BASE_URL=$currentApi"
}
else {
$currentApi = $values['API_BASE_URL']
# Always ensure we're using the HTTPS endpoint
if ($currentApi.StartsWith('http://api.sojorn.net:8080')) {
$currentApi = $currentApi.Replace('http://api.sojorn.net:8080', 'https://api.sojorn.net')
$defineArgs = $defineArgs | Where-Object { -not ($_ -like '--dart-define=API_BASE_URL=*') }
$defineArgs += "--dart-define=API_BASE_URL=$currentApi"
}
elseif ($currentApi.StartsWith('http://localhost:')) {
# For local development, keep localhost but warn
Write-Host "Using local API: $currentApi" -ForegroundColor Yellow
}
}
Write-Host "Launching Sojorn Web (Chrome)..." -ForegroundColor Cyan
Write-Host "Port: $Port"
Write-Host "Renderer: $Renderer"
Write-Host "API: $currentApi"
Push-Location (Join-Path $PSScriptRoot -ChildPath "sojorn_app")
try {
$cmdArgs = @(
'run',
'-d',
'chrome',
'--web-hostname',
'localhost',
'--web-port',
"$Port"
)
$cmdArgs += $defineArgs
if ($NoWasmDryRun) {
$cmdArgs += '--no-wasm-dry-run'
}
Write-Host "Running: flutter $($cmdArgs -join ' ')" -ForegroundColor DarkGray
& flutter $cmdArgs
}
finally {
Pop-Location
}