-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathrestart.ps1
More file actions
209 lines (179 loc) · 6.5 KB
/
Copy pathrestart.ps1
File metadata and controls
209 lines (179 loc) · 6.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
param(
[switch]$NoBuild
)
$ErrorActionPreference = "Stop"
$Root = Split-Path -Parent $MyInvocation.MyCommand.Path
$Port = 3100
$LogDir = Join-Path $Root "logs"
$CorepackHome = Join-Path $Root "data\corepack"
$CorepackShimDir = Join-Path $Root "data\corepack-shims"
function Find-Mise {
$candidates = @()
$cmd = Get-Command "mise.exe" -ErrorAction SilentlyContinue
if ($cmd) {
$candidates += $cmd.Source
}
if ($env:USERPROFILE) {
$candidates += (Join-Path $env:USERPROFILE ".local\bin\mise.exe")
}
$candidates += "C:\Users\voroj\.local\bin\mise.exe"
foreach ($candidate in $candidates) {
if ($candidate -and (Test-Path -LiteralPath $candidate)) {
return (Resolve-Path -LiteralPath $candidate).Path
}
}
return $null
}
function Initialize-MiseEnvironment {
param([string]$MiseExe)
if (-not $MiseExe) {
return
}
$binDir = Split-Path -Parent $MiseExe
$localDir = Split-Path -Parent $binDir
$userRoot = Split-Path -Parent $localDir
$localAppData = Join-Path $userRoot "AppData\Local"
$shimsDir = Join-Path $localAppData "mise\shims"
if (Test-Path -LiteralPath $localAppData) {
$env:USERPROFILE = $userRoot
$env:HOME = $userRoot
$env:LOCALAPPDATA = $localAppData
}
$pathParts = @($binDir, $shimsDir)
foreach ($part in $pathParts) {
if ($part -and (Test-Path -LiteralPath (Split-Path -Parent $part))) {
$env:PATH = "$part;$env:PATH"
}
}
}
function Get-ProjectPackageManager {
$packageJson = Join-Path $Root "package.json"
$pkg = Get-Content -LiteralPath $packageJson -Raw | ConvertFrom-Json
$packageManager = $pkg.packageManager
if ([string]::IsNullOrWhiteSpace($packageManager)) {
throw "package.json is missing packageManager."
}
$name = ($packageManager -split '@')[0]
if ($name -ne "pnpm") {
throw "Only pnpm is supported by restart.ps1, packageManager=$packageManager."
}
return $packageManager
}
function Invoke-PnpmBuild {
$pmSpec = Get-ProjectPackageManager
Write-Host "[2/3] Building with $pmSpec..." -ForegroundColor Cyan
$miseExe = Find-Mise
Initialize-MiseEnvironment $miseExe
$corepack = Get-Command "corepack.cmd" -ErrorAction SilentlyContinue
if (-not $corepack) {
$corepack = Get-Command "corepack" -ErrorAction SilentlyContinue
}
if ($miseExe -or $corepack) {
New-Item -ItemType Directory -Force -Path $CorepackHome | Out-Null
New-Item -ItemType Directory -Force -Path $CorepackShimDir | Out-Null
$pnpmShim = Join-Path $CorepackShimDir "pnpm.cmd"
if ($miseExe) {
$shimContent = @(
"@echo off",
"set `"COREPACK_HOME=$CorepackHome`"",
"`"$miseExe`" exec -- corepack $pmSpec %*"
)
} else {
$shimContent = @(
"@echo off",
"set `"COREPACK_HOME=$CorepackHome`"",
"`"$($corepack.Source)`" $pmSpec %*"
)
}
Set-Content -LiteralPath $pnpmShim -Value $shimContent -Encoding ASCII
$oldCorepackHome = $env:COREPACK_HOME
$oldPath = $env:PATH
$env:COREPACK_HOME = $CorepackHome
$env:PATH = "$CorepackShimDir;$oldPath"
try {
& $pnpmShim build | ForEach-Object { Write-Host $_ }
$buildExitCode = $LASTEXITCODE
return $buildExitCode
} finally {
$env:COREPACK_HOME = $oldCorepackHome
$env:PATH = $oldPath
}
}
$pnpm = Get-Command "pnpm.cmd" -ErrorAction SilentlyContinue
if (-not $pnpm) {
$pnpm = Get-Command "pnpm" -ErrorAction SilentlyContinue
}
if ($pnpm) {
& $pnpm.Name build | ForEach-Object { Write-Host $_ }
$buildExitCode = $LASTEXITCODE
return $buildExitCode
}
throw "Cannot find corepack or pnpm. Install pnpm or enable Node.js corepack."
}
function Stop-GatewayOnPort {
$conn = Get-NetTCPConnection -LocalPort $Port -State Listen -ErrorAction SilentlyContinue
if (-not $conn) {
Write-Host "[1/3] No process on port $Port, skipping." -ForegroundColor Gray
return
}
$procId = $conn.OwningProcess | Select-Object -First 1
Write-Host "[1/3] Stopping process $procId on port $Port..." -ForegroundColor Yellow
Stop-Process -Id $procId -Force -ErrorAction SilentlyContinue
for ($i = 0; $i -lt 20; $i++) {
Start-Sleep -Milliseconds 500
$still = Get-NetTCPConnection -LocalPort $Port -State Listen -ErrorAction SilentlyContinue
if (-not $still) {
return
}
}
throw "Port $Port was not released within 10 seconds."
}
function Start-Gateway {
New-Item -ItemType Directory -Force -Path $LogDir | Out-Null
$stamp = Get-Date -Format "yyyyMMdd-HHmmss"
$stdout = Join-Path $LogDir "gateway-$stamp.out.log"
$stderr = Join-Path $LogDir "gateway-$stamp.err.log"
$miseExe = Find-Mise
Initialize-MiseEnvironment $miseExe
Write-Host "[3/3] Starting gateway..." -ForegroundColor Green
if ($miseExe) {
$command = "cd /d `"$Root`" && `"$miseExe`" exec -- node packages\gateway\dist\index.js >> `"$stdout`" 2>> `"$stderr`""
} else {
$command = "cd /d `"$Root`" && node packages\gateway\dist\index.js >> `"$stdout`" 2>> `"$stderr`""
}
$shell = New-Object -ComObject WScript.Shell
$runner = "cmd.exe /d /c `"$command`""
$null = $shell.Run($runner, 0, $false)
for ($i = 0; $i -lt 20; $i++) {
Start-Sleep -Milliseconds 500
$check = Get-NetTCPConnection -LocalPort $Port -State Listen -ErrorAction SilentlyContinue
if ($check) {
$gatewayPid = $check.OwningProcess | Select-Object -First 1
Write-Host "Gateway running on port $Port (PID: $gatewayPid)" -ForegroundColor Green
Write-Host "stdout: $stdout" -ForegroundColor DarkGray
Write-Host "stderr: $stderr" -ForegroundColor DarkGray
return
}
}
Write-Host "Gateway failed to start!" -ForegroundColor Red
if (Test-Path $stderr) {
Get-Content -LiteralPath $stderr -Tail 80
}
exit 1
}
Push-Location $Root
try {
Stop-GatewayOnPort
if ($NoBuild) {
Write-Host "[2/3] Skipping build (-NoBuild)." -ForegroundColor Gray
} else {
$code = Invoke-PnpmBuild
if ($code -ne 0) {
Write-Host "Build failed!" -ForegroundColor Red
exit $code
}
}
Start-Gateway
} finally {
Pop-Location
}