<# scripts/_ide_start_wrapper.ps1 Purpose: One-shot wrapper for IDE-initiated "clean + migrate + hot-deploy full stack" under Windows PowerShell 5.x. Key notes (avoid PowerShell 5 CP936 / UTF-8-no-BOM parser bugs): 1) NO non-ASCII arrows, emojis, or Unicode punctuation anywhere in this file. All symbols are pure ASCII. PS5 (powershell.exe) parses .ps1 without BOM as ANSI (CP936/GBK), and UTF-8 multi-byte chars like U+2192 RIGHTWARDS ARROW can be mis-decoded as a byte that looks like a quote/brace -> ParserError chain. 2) Spawns start_local.ps1 via Start-Process independent powershell.exe so the wrapper itself exits ASAP and the IDE controlled terminal never sees "spawning multiple windows" (which gets Cancel-gated). 3) Logs are timestamped + PID-suffixed to avoid "file in use by another process" when IDE watcher or previous child still holds handles. 4) Enforces: concurrency=1 (single instance) / no RPA worker / no hot reload by default. #> param( [string]$DatabaseUrl = "postgresql+psycopg2://postgres:postgres@127.0.0.1:5432/hxops_db", [int]$KbConcurrency = 1, [int]$ShippingConcurrency = 1, [switch]$IncludeRpaWorker = $false, [switch]$HotReload = $false ) $ErrorActionPreference = "Stop" function Remove-FileWithRetry { param( [string]$Path, [int]$MaxRetries = 5, [int]$SleepMs = 300 ) if (-not (Test-Path $Path)) { return } for ($i = 1; $i -le $MaxRetries; $i++) { try { Remove-Item -Path $Path -Force -ErrorAction Stop return } catch { if ($i -eq $MaxRetries) { throw } Start-Sleep -Milliseconds $SleepMs } } } function Write-LogLineShared { param( [string]$Path, [string[]]$Lines, [switch]$Append ) $mode = if ($Append) { [System.IO.FileMode]::Append } else { [System.IO.FileMode]::Create } for ($i = 1; $i -le 5; $i++) { try { $fs = [System.IO.FileStream]::new( $Path, $mode, [System.IO.FileAccess]::Write, [System.IO.FileShare]::ReadWrite ) try { $sw = [System.IO.StreamWriter]::new($fs, [System.Text.UTF8Encoding]::new($false)) foreach ($l in $Lines) { $sw.WriteLine($l) } $sw.Flush() return } finally { if ($sw) { $sw.Dispose() } $fs.Dispose() } } catch { if ($i -eq 5) { throw } Start-Sleep -Milliseconds 300 $mode = [System.IO.FileMode]::Append } } } $RepoRoot = Split-Path -Parent $PSScriptRoot $LogsDir = Join-Path $RepoRoot ".local-logs" if (-not (Test-Path $LogsDir)) { New-Item -ItemType Directory -Path $LogsDir | Out-Null } $StartScript = Join-Path $PSScriptRoot "start_local.ps1" # Log file naming: ide_start__.log/.err.log -> never collides with previous runs. $ts = Get-Date -Format "yyyyMMddHHmmss" $LogOut = Join-Path $LogsDir ("ide_start_{0}_{1}.log" -f $PID, $ts) $LogErr = Join-Path $LogsDir ("ide_start_{0}_{1}.err.log" -f $PID, $ts) # Rotate: keep latest 8 of each pattern; remove older ones (with retry for watcher handles). Get-ChildItem -Path $LogsDir -Filter "ide_start_*.log" -ErrorAction SilentlyContinue | Sort-Object LastWriteTime -Descending | Select-Object -Skip 8 | ForEach-Object { Remove-FileWithRetry -Path $_.FullName } Get-ChildItem -Path $LogsDir -Filter "ide_start_*.err.log" -ErrorAction SilentlyContinue | Sort-Object LastWriteTime -Descending | Select-Object -Skip 8 | ForEach-Object { Remove-FileWithRetry -Path $_.FullName } $now = Get-Date -Format "yyyy-MM-dd HH:mm:ss" $tsLine = "[$now]" Write-LogLineShared -Path $LogOut -Lines @( "$tsLine wrapper: starting start_local.ps1 ...", "$tsLine wrapper: DatabaseUrl = $DatabaseUrl", "$tsLine wrapper: KbConcurrency=$KbConcurrency ShippingConcurrency=$ShippingConcurrency", "$tsLine wrapper: IncludeRpaWorker=$IncludeRpaWorker HotReload=$HotReload" ) $argList = @( "-NoProfile", "-ExecutionPolicy", "Bypass", "-File", "`"$StartScript`"", "-DbMode", "postgres", "-DatabaseUrl", "`"$DatabaseUrl`"", "-KbConcurrency", "$KbConcurrency", "-ShippingConcurrency", "$ShippingConcurrency" ) if ($IncludeRpaWorker) { $argList += @("-IncludeRpaWorker") } if (-not $HotReload) { $argList += @("-NoHotReload") } $spArgs = @{ FilePath = "powershell.exe" ArgumentList = $argList RedirectStandardOutput = $LogOut RedirectStandardError = $LogErr WindowStyle = "Normal" UseNewEnvironment = $false PassThru = $true } $proc = Start-Process @spArgs Write-LogLineShared -Append -Path $LogOut -Lines @( "$tsLine wrapper: start_local.ps1 launched (child PID=$($proc.Id)).", "$tsLine wrapper: stdout -> $LogOut", "$tsLine wrapper: stderr -> $LogErr", "$tsLine wrapper: wrapper exit=0 (child continues independently)." ) Write-Host "" Write-Host "=== HXOPS Start OK ===" -ForegroundColor Green Write-Host (" start_local child PID : " + $proc.Id) -ForegroundColor Cyan Write-Host (" stdout log : " + $LogOut) -ForegroundColor Cyan Write-Host (" stderr log : " + $LogErr) -ForegroundColor Cyan Write-Host "" Write-Host " Open the above log file(s) in the IDE to watch progress." Write-Host " Phase order: cleanup -> db-migrate -> backend 5052 -> frontend 8881 -> kb-worker -> shipping-worker." Write-Host ""