-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdev.ps1
More file actions
173 lines (143 loc) · 4.86 KB
/
Copy pathdev.ps1
File metadata and controls
173 lines (143 loc) · 4.86 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
[CmdletBinding()]
param(
[switch]$NoInstall,
[Parameter(ValueFromRemainingArguments = $true)]
[string[]]$TauriArgs
)
$ErrorActionPreference = "Stop"
Set-StrictMode -Version Latest
function Test-Command {
param(
[Parameter(Mandatory = $true)]
[string]$Name
)
return $null -ne (Get-Command -Name $Name -ErrorAction SilentlyContinue)
}
function Get-PackageManager {
if (Test-Command -Name "pnpm") {
return "pnpm"
}
if (Test-Command -Name "npm") {
return "npm"
}
throw "Neither pnpm nor npm was found. Please install one of them first."
}
function Invoke-PackageManager {
param(
[Parameter(Mandatory = $true)]
[string]$PackageManager,
[Parameter(Mandatory = $true)]
[string[]]$Arguments
)
& $PackageManager @Arguments
if ($LASTEXITCODE -ne 0) {
throw "Command failed: $PackageManager $($Arguments -join ' ')"
}
}
function Get-CleanArgs {
param(
[string[]]$Arguments
)
if ($null -eq $Arguments) {
return @()
}
return @(
$Arguments | Where-Object { -not [string]::IsNullOrWhiteSpace($_) }
)
}
function Test-PortBindable {
param(
[Parameter(Mandatory = $true)]
[int]$Port
)
$addresses = @([System.Net.IPAddress]::Loopback)
if ([System.Net.Sockets.Socket]::OSSupportsIPv6) {
$addresses = @([System.Net.IPAddress]::IPv6Loopback) + $addresses
}
foreach ($address in $addresses) {
$listener = $null
try {
$listener = [System.Net.Sockets.TcpListener]::new($address, $Port)
if ($address.AddressFamily -eq [System.Net.Sockets.AddressFamily]::InterNetworkV6) {
$listener.Server.DualMode = $false
}
$listener.Start()
}
catch {
return $false
}
finally {
if ($null -ne $listener) {
$listener.Stop()
}
}
}
return $true
}
function Find-DevPortPair {
param(
[int]$StartPort = 3210,
[int]$MaxPort = 65000
)
if ($StartPort -lt 1024) {
$StartPort = 1024
}
if ($StartPort % 2 -ne 0) {
$StartPort++
}
for ($devPort = $StartPort; $devPort -lt $MaxPort; $devPort += 2) {
$hmrPort = $devPort + 1
if ((Test-PortBindable -Port $devPort) -and (Test-PortBindable -Port $hmrPort)) {
return @{
DevPort = $devPort
HmrPort = $hmrPort
}
}
}
throw "Unable to find an available dev/HMR port pair between $StartPort and $MaxPort."
}
$scriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path
$tauriConfigOverridePath = $null
Push-Location -LiteralPath $scriptDir
try {
if (-not (Test-Command -Name "node")) {
throw "Node.js 18+ was not found. Please install Node.js first."
}
$packageManager = Get-PackageManager
Write-Host "Working directory: $scriptDir" -ForegroundColor Cyan
Write-Host "Package manager: $packageManager" -ForegroundColor Cyan
$nodeModulesPath = Join-Path -Path $scriptDir -ChildPath "node_modules"
$needInstall = (-not $NoInstall) -and (-not (Test-Path -LiteralPath $nodeModulesPath))
if ($needInstall) {
Write-Host "node_modules not found. Installing frontend dependencies..." -ForegroundColor Yellow
Invoke-PackageManager -PackageManager $packageManager -Arguments @("install")
}
Write-Host "Starting Tauri dev..." -ForegroundColor Green
$cleanTauriArgs = Get-CleanArgs -Arguments $TauriArgs
$preferredDevPort = if ($env:TAURI_DEV_PORT) { [int]$env:TAURI_DEV_PORT } else { 3210 }
$portPair = Find-DevPortPair -StartPort $preferredDevPort
$devPort = $portPair.DevPort
$hmrPort = $portPair.HmrPort
$devUrl = "http://localhost:$devPort"
$tauriConfigOverridePath = Join-Path $env:TEMP ("ek-omniprobe-tauri-dev-{0}.json" -f [guid]::NewGuid().ToString("N"))
Set-Content -LiteralPath $tauriConfigOverridePath -Value (@{
build = @{
devUrl = $devUrl
}
} | ConvertTo-Json -Compress) -Encoding UTF8
$env:TAURI_DEV_PORT = [string]$devPort
$env:TAURI_DEV_HMR_PORT = [string]$hmrPort
Write-Host "Selected frontend dev port: $devPort (HMR: $hmrPort)" -ForegroundColor DarkCyan
if ($packageManager -eq "pnpm") {
Invoke-PackageManager -PackageManager $packageManager -Arguments (@("tauri", "dev", "--config", $tauriConfigOverridePath) + $cleanTauriArgs)
}
else {
Invoke-PackageManager -PackageManager $packageManager -Arguments (@("run", "tauri", "--", "dev", "--config", $tauriConfigOverridePath) + $cleanTauriArgs)
}
}
finally {
if ($null -ne $tauriConfigOverridePath -and (Test-Path -LiteralPath $tauriConfigOverridePath)) {
Remove-Item -LiteralPath $tauriConfigOverridePath -Force -ErrorAction SilentlyContinue
}
Pop-Location
}