param( [string]$HostName = "christunscripted", [string]$RemoteRoot = "/sites/libre-study", [string]$RemoteBibleDataRoot = "/sites/libre-bible-data/packages/json", [int]$Port = 9174 ) $ErrorActionPreference = "Stop" $repoRoot = Split-Path -Parent $PSScriptRoot $dataRepo = Resolve-Path (Join-Path $repoRoot "..\libre-bible-data") $repoRootWsl = (wsl wslpath -a "$repoRoot").Trim() $dataRepoWsl = (wsl wslpath -a "$dataRepo").Trim() $commit = (git -C $repoRoot rev-parse HEAD).Trim() $deployedAt = (Get-Date).ToUniversalTime().ToString("o") Push-Location $repoRoot try { $env:VITE_REMOTE_WORKSPACE = "true" $env:VITE_API_BASE_URL = "/study/api" $env:VITE_LIBREBIBLE_BASE_URL = "/study/librebible" npm.cmd run build:web:hosted } finally { Remove-Item Env:VITE_REMOTE_WORKSPACE -ErrorAction SilentlyContinue Remove-Item Env:VITE_API_BASE_URL -ErrorAction SilentlyContinue Remove-Item Env:VITE_LIBREBIBLE_BASE_URL -ErrorAction SilentlyContinue Pop-Location } $deployInfo = @{ commit = $commit deployedAt = $deployedAt source = "libre-study" } | ConvertTo-Json -Compress $escapedDeployInfo = $deployInfo.Replace("'", "'\''") wsl ssh $HostName "mkdir -p '$RemoteRoot/app' '$RemoteRoot/server' '$RemoteRoot/state' '$RemoteBibleDataRoot' && printf '%s' '$escapedDeployInfo' > '$RemoteRoot/deploy-info.json'" wsl rsync -az --delete "$repoRootWsl/dist-hosted/" "${HostName}:$RemoteRoot/app/" wsl rsync -az --delete "$repoRootWsl/server/" "${HostName}:$RemoteRoot/server/" wsl rsync -az --delete "$dataRepoWsl/packages/json/" "${HostName}:$RemoteBibleDataRoot/" $pm2Command = @" cd '$RemoteRoot' && if pm2 describe libre-study-web >/dev/null 2>&1; then PORT=$Port PUBLIC_DIR='$RemoteRoot/app' LIBREBIBLE_DATA_DIR='$RemoteBibleDataRoot' STATE_DIR='$RemoteRoot/state' DB_PATH='$RemoteRoot/state/libre-study-web.sqlite3' DEPLOY_INFO_PATH='$RemoteRoot/deploy-info.json' pm2 restart libre-study-web --update-env; else PORT=$Port PUBLIC_DIR='$RemoteRoot/app' LIBREBIBLE_DATA_DIR='$RemoteBibleDataRoot' STATE_DIR='$RemoteRoot/state' DB_PATH='$RemoteRoot/state/libre-study-web.sqlite3' DEPLOY_INFO_PATH='$RemoteRoot/deploy-info.json' pm2 start '$RemoteRoot/server/libre-study-server.mjs' --name libre-study-web --update-env; fi && pm2 save "@ wsl ssh $HostName $pm2Command Write-Output "Deployed Libre Study web commit $commit to $HostName:$RemoteRoot on port $Port."