-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlaunch.ps1
More file actions
155 lines (132 loc) · 4.1 KB
/
launch.ps1
File metadata and controls
155 lines (132 loc) · 4.1 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
#!/usr/bin/env pwsh
# DevPossible.Ton Documentation Server Launcher
# Copyright (c) 2024 DevPossible, LLC
param(
[int]$Port = 8080,
[switch]$NoBrowser,
[string]$Browser = "default"
)
$ErrorActionPreference = "Stop"
# Colors for output
function Write-Info { Write-Host $args -ForegroundColor Cyan }
function Write-Success { Write-Host $args -ForegroundColor Green }
function Write-Warning { Write-Host $args -ForegroundColor Yellow }
function Write-Error { Write-Host $args -ForegroundColor Red }
Write-Info "========================================="
Write-Info " DevPossible.Ton Documentation Server"
Write-Info " © 2024 DevPossible, LLC"
Write-Info "========================================="
Write-Host ""
# Check if Node.js is installed
try {
$nodeVersion = node --version 2>$null
if ($LASTEXITCODE -ne 0) { throw }
Write-Success "✓ Node.js detected: $nodeVersion"
} catch {
Write-Error "✗ Node.js is not installed or not in PATH"
Write-Host " Please install Node.js from https://nodejs.org/"
exit 1
}
# Check if npm is installed
try {
$npmVersion = npm --version 2>$null
if ($LASTEXITCODE -ne 0) { throw }
Write-Success "✓ npm detected: $npmVersion"
} catch {
Write-Error "✗ npm is not installed or not in PATH"
exit 1
}
# Check if npx is available
try {
$npxVersion = npx --version 2>$null
if ($LASTEXITCODE -ne 0) { throw }
Write-Success "✓ npx detected: $npxVersion"
} catch {
Write-Error "✗ npx is not available"
exit 1
}
# Get the script's directory
$scriptPath = Split-Path -Parent $MyInvocation.MyCommand.Definition
$docPath = Join-Path $scriptPath "doc\doc-html"
# Verify doc-html folder exists
if (!(Test-Path $docPath)) {
Write-Error "✗ Documentation folder not found: $docPath"
exit 1
}
# Check if index.html exists
$indexPath = Join-Path $docPath "index.html"
if (!(Test-Path $indexPath)) {
Write-Error "✗ index.html not found in: $docPath"
exit 1
}
Write-Success "✓ Documentation folder found: $docPath"
Write-Host ""
# Find an available port if the default is in use
function Test-Port {
param($Port)
try {
$tcpClient = New-Object System.Net.Sockets.TcpClient
$tcpClient.Connect("127.0.0.1", $Port)
$tcpClient.Close()
return $true
} catch {
return $false
}
}
$originalPort = $Port
while (Test-Port $Port) {
Write-Warning "Port $Port is already in use"
$Port++
if ($Port -gt ($originalPort + 10)) {
Write-Error "✗ Could not find an available port"
exit 1
}
}
$url = "http://localhost:$Port"
Write-Info "Starting documentation server..."
Write-Host " Directory: $docPath"
Write-Host " URL: $url"
Write-Host ""
# Launch browser after a short delay (unless -NoBrowser is specified)
if (!$NoBrowser) {
$browserJob = Start-Job -ScriptBlock {
param($url, $browser)
Start-Sleep -Seconds 2
if ($browser -eq "default") {
Start-Process $url
} else {
Start-Process $browser -ArgumentList $url
}
} -ArgumentList $url, $Browser
Write-Info "Browser will open automatically in 2 seconds..."
}
Write-Host ""
Write-Success "Server is starting on $url"
Write-Warning "Press Ctrl+C to stop the server"
Write-Host ""
Write-Host "----------------------------------------"
Write-Host ""
# Start the server using npx http-server
# Using http-server as it's a popular, simple static file server
try {
# Change to doc-html directory and start server
Push-Location $docPath
# Run http-server with options:
# -p: port
# -c-1: disable caching
# -o: open browser (disabled as we handle it ourselves)
# --cors: enable CORS
# -s: suppress log messages (we'll remove this for visibility)
npx --yes http-server . -p $Port -c-1 --cors
} catch {
Write-Error "✗ Failed to start server: $_"
} finally {
Pop-Location
# Clean up browser job if it exists
if (!$NoBrowser -and $browserJob) {
Stop-Job $browserJob -ErrorAction SilentlyContinue
Remove-Job $browserJob -ErrorAction SilentlyContinue
}
}
Write-Host ""
Write-Info "Server stopped"