-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstart.ps1
More file actions
355 lines (298 loc) · 10.7 KB
/
Copy pathstart.ps1
File metadata and controls
355 lines (298 loc) · 10.7 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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
$ErrorActionPreference = "Stop"
$Root = Split-Path -Parent $MyInvocation.MyCommand.Path
$LogDir = Join-Path $Root ".local-logs"
$ServerOut = Join-Path $LogDir "server.out.log"
$ServerErr = Join-Path $LogDir "server.err.log"
$EditorOut = Join-Path $LogDir "editor.out.log"
$EditorErr = Join-Path $LogDir "editor.err.log"
$BackendProcess = $null
$FrontendProcess = $null
$BackendPort = 7777
$FrontendPort = 3000
$EditorUrl = "http://localhost:$FrontendPort"
$EditorProbeUrls = @("http://127.0.0.1:$FrontendPort", "http://localhost:$FrontendPort")
function Write-Step {
param([string] $Message)
Write-Host " $Message"
}
function Resolve-RequiredCommand {
param(
[string[]] $Names,
[string] $ErrorMessage
)
foreach ($Name in $Names) {
$Command = Get-Command $Name -ErrorAction SilentlyContinue
if ($Command) {
if ($Command.Source) {
return $Command.Source
}
return $Command.Path
}
}
throw $ErrorMessage
}
function Stop-ProcessTree {
param([int] $TargetProcessId)
if ($TargetProcessId -le 0) {
return
}
& taskkill.exe /PID $TargetProcessId /T /F > $null 2>&1
}
function Stop-PortListener {
param([int] $Port)
try {
$Listeners = Get-NetTCPConnection -LocalPort $Port -State Listen -ErrorAction SilentlyContinue
$ProcessIds = $Listeners | Select-Object -ExpandProperty OwningProcess -Unique
foreach ($ProcessId in $ProcessIds) {
if ($ProcessId -and $ProcessId -ne $PID) {
Stop-ProcessTree -TargetProcessId $ProcessId
}
}
} catch {
# Port cleanup is best-effort; the server will report a clear error if the port is still busy.
}
}
function Get-PortProcessIds {
param([int] $Port)
try {
@(Get-NetTCPConnection -LocalPort $Port -State Listen -ErrorAction SilentlyContinue |
Select-Object -ExpandProperty OwningProcess -Unique)
} catch {
@()
}
}
function Test-PortInUse {
param([int] $Port)
return @(Get-PortProcessIds -Port $Port).Count -gt 0
}
function Test-BlacknodeEditorReady {
foreach ($Url in $EditorProbeUrls) {
try {
$Response = Invoke-WebRequest -Uri $Url -UseBasicParsing -TimeoutSec 2
if ($Response.StatusCode -ge 200 -and $Response.Content -match "<title>Blacknode</title>") {
return $true
}
} catch {
# Try the next loopback spelling before deciding the editor is not ours.
}
}
return $false
}
function Test-BlacknodeEditorProcessOnPort {
param([int] $Port)
$EditorDir = (Join-Path $Root "editor").Replace("\", "/").ToLowerInvariant()
foreach ($ProcessId in @(Get-PortProcessIds -Port $Port)) {
try {
$ProcessInfo = Get-CimInstance Win32_Process -Filter "ProcessId=$ProcessId" -ErrorAction Stop
$CommandLine = [string] $ProcessInfo.CommandLine
$Normalized = $CommandLine.Replace("\", "/").ToLowerInvariant()
if ($Normalized.Contains($EditorDir) -and $Normalized.Contains("vite")) {
return $true
}
} catch {
# If process inspection fails, keep the conservative port-busy behavior.
}
}
return $false
}
function Wait-BlacknodeEditorReady {
param([int] $TimeoutSeconds = 5)
$Deadline = (Get-Date).AddSeconds($TimeoutSeconds)
while ((Get-Date) -lt $Deadline) {
if (Test-BlacknodeEditorReady) {
return $true
}
Start-Sleep -Milliseconds 500
}
return $false
}
function Wait-PortFree {
param(
[int] $Port,
[int] $TimeoutSeconds = 5
)
$Deadline = (Get-Date).AddSeconds($TimeoutSeconds)
while ((Get-Date) -lt $Deadline) {
if (-not (Test-PortInUse -Port $Port)) {
return $true
}
Start-Sleep -Milliseconds 500
}
return -not (Test-PortInUse -Port $Port)
}
function Write-PortBusyError {
param([int] $Port)
Write-Host ""
Write-Host " ERROR: Port $Port is already in use by another app."
$ProcessIds = Get-PortProcessIds -Port $Port
if (@($ProcessIds).Count -gt 0) {
Write-Host ""
Write-Host " Listening process id(s): $(@($ProcessIds) -join ', ')"
}
Write-Host " Close that app or free port $Port, then run start.bat again."
}
function Test-FrontendDependencies {
Push-Location (Join-Path $Root "editor")
try {
& node -e "import('vite').then(() => {}).catch(() => process.exit(1))" > $null 2>&1
return $LASTEXITCODE -eq 0
} finally {
Pop-Location
}
}
function Install-FrontendDependencies {
param([string] $Message)
Write-Step $Message
Push-Location (Join-Path $Root "editor")
try {
& $Npm install
if ($LASTEXITCODE -ne 0) {
throw "Frontend dependency install failed."
}
} finally {
Pop-Location
}
}
function Start-HiddenProcess {
param(
[string] $FilePath,
[string[]] $Arguments,
[string] $WorkingDirectory,
[string] $OutLog,
[string] $ErrLog
)
Remove-Item -LiteralPath $OutLog, $ErrLog -Force -ErrorAction SilentlyContinue
Start-Process `
-FilePath $FilePath `
-ArgumentList $Arguments `
-WorkingDirectory $WorkingDirectory `
-WindowStyle Hidden `
-RedirectStandardOutput $OutLog `
-RedirectStandardError $ErrLog `
-PassThru
}
function Assert-ProcessRunning {
param(
[System.Diagnostics.Process] $Process,
[string] $Name,
[string] $ErrorLog
)
$Process.Refresh()
if (-not $Process.HasExited) {
return
}
Write-Host ""
Write-Host " ERROR: $Name stopped during startup."
if (Test-Path -LiteralPath $ErrorLog) {
Write-Host ""
Write-Host " Last log lines:"
Get-Content -LiteralPath $ErrorLog -Tail 30 | ForEach-Object { Write-Host " $_" }
}
throw "$Name failed to start."
}
function Open-Browser {
if ($env:BLACKNODE_NO_BROWSER -eq "1") {
Write-Step "Browser launch skipped (BLACKNODE_NO_BROWSER=1)."
return
}
Write-Step "Opening browser..."
Start-Process $EditorUrl
}
function Stop-Services {
if ($script:FrontendProcess -and -not $script:FrontendProcess.HasExited) {
Stop-ProcessTree -TargetProcessId $script:FrontendProcess.Id
}
if ($script:BackendProcess -and -not $script:BackendProcess.HasExited) {
Stop-ProcessTree -TargetProcessId $script:BackendProcess.Id
}
}
try {
$Banner = Join-Path $Root "banner.ps1"
if (Test-Path -LiteralPath $Banner) {
& $Banner
} else {
Write-Host ""
Write-Host " BLACKNODE"
Write-Host ""
}
New-Item -ItemType Directory -Path $LogDir -Force | Out-Null
$Python = Resolve-RequiredCommand -Names @("python", "py") -ErrorMessage "Python 3.11+ is required."
$Npm = Resolve-RequiredCommand -Names @("npm.cmd", "npm") -ErrorMessage "npm is required. Install Node.js 20.19+ or 22.12+."
Write-Step "Checking Python dependencies..."
& $Python -m pip install -r (Join-Path $Root "editor-server\requirements.txt") -q --disable-pip-version-check
if ($LASTEXITCODE -ne 0) {
throw "Python dependency install failed."
}
& $Python -c "import importlib.metadata; importlib.metadata.version('blacknode')" > $null 2>&1
if ($LASTEXITCODE -ne 0) {
Write-Step "Installing blacknode package for the CLI..."
& $Python -m pip install -e $Root -q --disable-pip-version-check
if ($LASTEXITCODE -ne 0) {
throw "Blacknode package install failed."
}
}
# Optional: install CuPy for the GPU/CUDA nodes when an NVIDIA GPU is present.
# Non-fatal: a failure here never blocks the editor.
if (Get-Command nvidia-smi -ErrorAction SilentlyContinue) {
& $Python -c "import cupy" > $null 2>&1
if ($LASTEXITCODE -ne 0) {
Write-Step "NVIDIA GPU detected - installing CuPy for CUDA nodes (one-time, large download)..."
& $Python -m pip install cupy-cuda12x -q --disable-pip-version-check
if ($LASTEXITCODE -ne 0) {
Write-Host " CuPy install failed; GPU/CUDA nodes stay unavailable but the editor will run." -ForegroundColor Yellow
}
}
}
$NodeModules = Join-Path $Root "editor\node_modules"
if (-not (Test-Path -LiteralPath $NodeModules)) {
Install-FrontendDependencies -Message "Installing frontend dependencies (first run, this can take a minute)..."
} elseif (-not (Test-FrontendDependencies)) {
Install-FrontendDependencies -Message "Repairing frontend dependencies for this OS..."
}
Write-Step "Done."
Write-Host ""
Stop-PortListener -Port $BackendPort
Write-Step "[1/2] Starting Python server (http://127.0.0.1:$BackendPort)"
$script:BackendProcess = Start-HiddenProcess `
-FilePath $Python `
-Arguments @("server.py") `
-WorkingDirectory (Join-Path $Root "editor-server") `
-OutLog $ServerOut `
-ErrLog $ServerErr
Start-Sleep -Seconds 3
Assert-ProcessRunning -Process $script:BackendProcess -Name "Python server" -ErrorLog $ServerErr
if (Test-PortInUse -Port $FrontendPort) {
$ExistingBlacknodeEditor = (Wait-BlacknodeEditorReady) -or (Test-BlacknodeEditorProcessOnPort -Port $FrontendPort)
if (-not $ExistingBlacknodeEditor) {
Write-PortBusyError -Port $FrontendPort
throw "Visual editor port is busy."
}
Write-Step "Stopping existing visual editor on port $FrontendPort..."
Stop-PortListener -Port $FrontendPort
if (-not (Wait-PortFree -Port $FrontendPort)) {
Write-PortBusyError -Port $FrontendPort
throw "Visual editor port is busy."
}
}
Write-Step "[2/2] Starting visual editor ($EditorUrl)"
$script:FrontendProcess = Start-HiddenProcess `
-FilePath $Npm `
-Arguments @("run", "dev", "--", "--strictPort") `
-WorkingDirectory (Join-Path $Root "editor") `
-OutLog $EditorOut `
-ErrLog $EditorErr
Start-Sleep -Seconds 5
Assert-ProcessRunning -Process $script:FrontendProcess -Name "Visual editor" -ErrorLog $EditorErr
Open-Browser
Write-Host ""
Write-Step "Logs: .local-logs\server.out.log and .local-logs\editor.out.log"
Write-Step "Press Ctrl+C to stop."
Write-Host ""
while ($true) {
Assert-ProcessRunning -Process $script:BackendProcess -Name "Python server" -ErrorLog $ServerErr
Assert-ProcessRunning -Process $script:FrontendProcess -Name "Visual editor" -ErrorLog $EditorErr
Start-Sleep -Seconds 1
}
} finally {
Stop-Services
}