# SPDX-License-Identifier: MPL-2.0 # Messages en ASCII : compatible Windows PowerShell 5.1 sans BOM. <# .SYNOPSIS Lance SEQUA Studio en developpement et ouvre le navigateur. .DESCRIPTION Demarre le serveur de developpement Angular (ng serve, rechargement a chaud) dans studio/ui. Installe les dependances (npm ci) au premier lancement ou si package-lock.json a change. Sans -Core ni -MockCore, Studio utilise le Core simule. Arret : Ctrl+C (le faux Core, s'il tourne, est arrete automatiquement). .PARAMETER Core URL WebSocket d'un Core reel (ws:// ou wss://). Sans ce parametre : Core simule. .PARAMETER MockCore Demarre le FAUX Core de developpement (simule) en arriere-plan sur ws://127.0.0.1:9101 et pointe Studio dessus. Pour tester le chemin WebSocket sans compiler le vrai Core (sequa-core). Exclusif avec -Core. .PARAMETER Scenario Scenario du faux Core, utilisable seulement avec -MockCore : ok, incompatible, error, silent, garbage ou close. Defaut : ok. .PARAMETER Port Port du serveur de developpement. Defaut : 5173. .PARAMETER NoOpen N'ouvre pas le navigateur. .PARAMETER Help Affiche cette aide. .EXAMPLE .\scripts\studio.ps1 .EXAMPLE .\scripts\studio.ps1 -Core ws://127.0.0.1:9100 .EXAMPLE .\scripts\studio.ps1 -MockCore .EXAMPLE .\scripts\studio.ps1 -MockCore -Scenario error #> [CmdletBinding()] param( [string]$Core, [switch]$MockCore, [ValidateSet("ok", "incompatible", "error", "silent", "garbage", "close")] [string]$Scenario = "ok", [int]$Port = 5173, [switch]$NoOpen, [Alias("h", "?")] [switch]$Help ) if ($Help) { Get-Help $PSCommandPath -Detailed exit 0 } $ErrorActionPreference = "Stop" $studio = Join-Path (Split-Path -Parent $PSScriptRoot) "studio\ui" if (-not (Get-Command npm -ErrorAction SilentlyContinue)) { throw "npm introuvable : installer Node.js 24 (https://nodejs.org)." } if ($Core -and $Core -notmatch '^wss?://') { throw "-Core doit commencer par ws:// ou wss:// (recu : $Core)." } if ($Core -and $MockCore) { throw "-Core et -MockCore sont exclusifs." } # Attend que le faux Core accepte les connexions TCP sur son port. function Wait-MockCore { param([string]$TargetHost, [int]$TargetPort, [int]$TimeoutMs = 5000) $deadline = [DateTime]::UtcNow.AddMilliseconds($TimeoutMs) while ([DateTime]::UtcNow -lt $deadline) { $client = New-Object System.Net.Sockets.TcpClient try { $client.Connect($TargetHost, $TargetPort) return $true } catch { Start-Sleep -Milliseconds 100 } finally { $client.Close() } } return $false } $mockProcess = $null $mockOut = $null $mockErr = $null Push-Location $studio try { $lock = Get-Item "package-lock.json" $installed = "node_modules\.package-lock.json" if (-not (Test-Path $installed) -or (Get-Item $installed).LastWriteTime -lt $lock.LastWriteTime) { Write-Host "==> npm ci" -ForegroundColor Cyan npm ci --no-audit --no-fund if ($LASTEXITCODE -ne 0) { throw "Echec : npm ci" } } # Sans -Core ni -MockCore : Core simule, demande explicitement (Studio ne simule jamais sans ?core=simulated). $coreUrl = "simulated" if ($Core) { $coreUrl = $Core } elseif ($MockCore) { $coreUrl = "ws://127.0.0.1:9101" Write-Host "==> FAUX CORE DE DEVELOPPEMENT (SIMULE) : $coreUrl (scenario $Scenario)" -ForegroundColor Cyan # Arriere-plan, journal dans des fichiers temporaires (affiches si le demarrage echoue). # [IO.Path]::GetTempFileName() : toujours disponible (New-TemporaryFile # peut manquer selon l'environnement PowerShell 5.1). $mockOut = [System.IO.Path]::GetTempFileName() $mockErr = [System.IO.Path]::GetTempFileName() # Le faux Core est en TypeScript (contrat genere) : construit d'abord par Vite (SSR). Push-Location $studio try { npm run mock-core:build --silent; if ($LASTEXITCODE -ne 0) { throw "Echec : construction du faux Core" } } finally { Pop-Location } $mockProcess = Start-Process -FilePath "node" -ArgumentList @( "tools/.mock-core/mock-core.js", "--scenario", $Scenario, "--port", "9101", "--host", "127.0.0.1" ) -WorkingDirectory $studio -WindowStyle Hidden -PassThru ` -RedirectStandardOutput $mockOut -RedirectStandardError $mockErr if (-not (Wait-MockCore -TargetHost "127.0.0.1" -TargetPort 9101)) { $outText = "" $errText = "" if (Test-Path $mockOut) { $outText = [string](Get-Content $mockOut -Raw) } if (Test-Path $mockErr) { $errText = [string](Get-Content $mockErr -Raw) } throw "Le faux Core n'a pas demarre sur le port 9101. Journal : $outText $errText" } } $path = "/" if ($coreUrl) { $path = "/?core=" + [uri]::EscapeDataString($coreUrl) } $mode = if ($Core) { "Core reel $coreUrl" } elseif ($MockCore) { "Faux Core (simule) $coreUrl" } else { "Core simule" } Write-Host "==> SEQUA Studio : http://localhost:$Port$path ($mode) - Ctrl+C pour arreter" -ForegroundColor Cyan # ng serve ouvre seulement la racine : le navigateur est ouvert ici, sur l'URL complete # (parametre ?core=), des que le port repond. Processus cache, independant de Studio. if (-not $NoOpen) { $url = "http://localhost:$Port$path" $opener = "for (`$i = 0; `$i -lt 240; `$i++) { try { `$c = [Net.Sockets.TcpClient]::new('127.0.0.1', $Port); `$c.Dispose(); Start-Process '$url'; break } catch { Start-Sleep -Milliseconds 500 } }" Start-Process -FilePath "powershell" -ArgumentList @("-NoProfile", "-Command", $opener) -WindowStyle Hidden | Out-Null } npm run dev -- --port "$Port" } finally { # Arret de l'arbre de processus du faux Core (taskkill /T), s'il vit encore. if ($null -ne $mockProcess) { if (-not $mockProcess.HasExited) { taskkill /PID $mockProcess.Id /T /F 2>$null | Out-Null } $mockProcess.Dispose() } foreach ($temp in @($mockOut, $mockErr)) { if ($null -ne $temp -and (Test-Path $temp)) { Remove-Item $temp -ErrorAction SilentlyContinue } } Pop-Location }