cmvr-wms/mobile-app/scripts/Start-DevelopmentUpdates.ps1

37 lines
2.2 KiB
PowerShell
Raw Normal View History

$ErrorActionPreference = 'Stop'
$taskApp = Split-Path $PSScriptRoot -Parent
$taskRepo = Split-Path $taskApp -Parent
$taskDir = Join-Path $taskRepo '.codex-tmp/app-dev-ota'
$taskServer = Join-Path $PSScriptRoot 'serve-development.mjs'
$taskConfig = Get-Content -LiteralPath (Join-Path $taskApp 'config/development.json') -Raw | ConvertFrom-Json
$taskOrigin = $taskConfig.updateOrigin
New-Item -ItemType Directory -Path $taskDir -Force | Out-Null
$taskRecord = Join-Path $taskDir 'server.json'
if (Test-Path -LiteralPath $taskRecord) {
$taskPrevious = Get-Content -LiteralPath $taskRecord -Raw | ConvertFrom-Json
$taskExisting = Get-CimInstance Win32_Process -Filter "ProcessId=$($taskPrevious.pid)"
if ($taskExisting -and $taskExisting.Name -eq 'node.exe' -and $taskExisting.CommandLine.Contains($taskServer) -and $taskPrevious.origin -eq $taskOrigin) {
if ((Invoke-WebRequest -Uri ($taskOrigin + '/health') -TimeoutSec 5 -UseBasicParsing).Content.Trim() -eq 'ok') {
Write-Output ('Development updates already running: ' + $taskOrigin)
exit 0
}
}
if ($taskExisting) { throw 'Recorded process does not match a healthy development update server; no process stopped.' }
}
$taskNode = (Get-Command node.exe).Source
$taskProcess = Start-Process -FilePath $taskNode -ArgumentList ('"' + $taskServer + '"') -WorkingDirectory $taskApp -WindowStyle Hidden -PassThru `
-RedirectStandardOutput (Join-Path $taskDir 'server.out.log') -RedirectStandardError (Join-Path $taskDir 'server.err.log')
for ($taskAttempt = 0; $taskAttempt -lt 20; $taskAttempt++) {
Start-Sleep -Milliseconds 250
$taskProcess.Refresh()
if ($taskProcess.HasExited) { throw (Get-Content -LiteralPath (Join-Path $taskDir 'server.err.log') -Raw) }
try {
if ((Invoke-WebRequest -Uri ($taskOrigin + '/health') -TimeoutSec 2 -UseBasicParsing).Content.Trim() -eq 'ok') {
[IO.File]::WriteAllText($taskRecord, (@{pid=$taskProcess.Id;origin=$taskOrigin;script=$taskServer} | ConvertTo-Json))
Write-Output ('Development updates running: ' + $taskOrigin)
exit 0
}
} catch { }
}
throw 'Development update service did not become ready; inspect .codex-tmp/app-dev-ota/server.err.log'