-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstart-dashboard.ps1
More file actions
62 lines (55 loc) · 1.52 KB
/
Copy pathstart-dashboard.ps1
File metadata and controls
62 lines (55 loc) · 1.52 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
param(
[int]$Port = 3777,
[switch]$NoBrowser
)
$Project = "C:\Users\amanu\Documents\Projects\opencode-usage-tracking"
$Url = "http://localhost:$Port"
$Log = Join-Path $Project ".dashboard.log"
$LogErr = "$Log.err"
function Test-PortOpen([int]$p) {
try {
$client = New-Object System.Net.Sockets.TcpClient
$iar = $client.BeginConnect("127.0.0.1", $p, $null, $null)
$ok = $iar.AsyncWaitHandle.WaitOne(400)
if ($ok) {
$client.EndConnect($iar)
$client.Close()
return $true
}
$client.Close()
return $false
} catch {
return $false
}
}
# Already running, nothing to do.
if (Test-PortOpen $Port) {
exit 0
}
# Prefer the production server; fall back to dev if no build exists.
$hasBuild = Test-Path (Join-Path $Project ".next\BUILD_ID")
$script = if ($hasBuild) { "start" } else { "dev" }
try {
$p = Start-Process -FilePath "npm.cmd" -ArgumentList "run", $script `
-WorkingDirectory $Project -WindowStyle Hidden -PassThru `
-RedirectStandardOutput $Log -RedirectStandardError $LogErr
} catch {
Write-Host "dashboard: failed to start ($_)" -ForegroundColor Red
exit 1
}
$deadline = (Get-Date).AddSeconds(40)
while ((Get-Date) -lt $deadline) {
Start-Sleep -Milliseconds 500
if (Test-PortOpen $Port) {
if (-not $NoBrowser) {
Start-Process $Url
}
exit 0
}
if ($p.HasExited) {
Write-Host "dashboard: server exited early, see $Log" -ForegroundColor Red
exit 1
}
}
Write-Host "dashboard: timed out waiting for $Url, see $Log" -ForegroundColor Red
exit 1