Files
genshin-assistant/scripts/dev-admin-start.ps1
T

97 lines
5.0 KiB
PowerShell

param(
[string]$ProjectRoot
)
# Mit -NoExit gestartet: dieses Fenster bleibt immer offen (siehe dev-admin.cmd),
# aber ein sauberer try/catch mit klarer Meldung ist trotzdem besser als ein
# roher Stacktrace, wenn z.B. der Dev-Port schon belegt ist.
$ErrorActionPreference = "Stop"
try {
$project = (Resolve-Path -LiteralPath $ProjectRoot).Path
$logDir = Join-Path $project "outputs\admin-start"
New-Item -ItemType Directory -Force -Path $logDir | Out-Null
$logPath = Join-Path $logDir "admin-dev.log"
try {
Start-Transcript -Path $logPath -Append | Out-Null
} catch {
Write-Host "WARNUNG: Admin-Start-Log konnte nicht geschrieben werden: $($_.Exception.Message)" -ForegroundColor Yellow
}
Write-Host "Projekt: $project"
Write-Host "Admin-Log: $logPath"
# A UAC-elevated process gets its environment rebuilt fresh from the
# registry; it does NOT inherit PATH edits that only exist in the calling
# (non-elevated) shell session (e.g. a version manager that only patched
# the current terminal). If node/npm resolve normally but not here, that
# is almost always the cause - fail with a clear message instead of a
# cryptic "npm is not recognized" a few lines down.
$npmCommand = Get-Command npm.cmd -ErrorAction SilentlyContinue
if (-not $npmCommand) { $npmCommand = Get-Command npm -ErrorAction SilentlyContinue }
$nodeCommand = Get-Command node.exe -ErrorAction SilentlyContinue
if (-not $nodeCommand) { $nodeCommand = Get-Command node -ErrorAction SilentlyContinue }
if (-not $npmCommand -or -not $nodeCommand) {
Write-Host ""
Write-Host "FEHLER: npm/node wurden in diesem administrativen Fenster nicht gefunden." -ForegroundColor Red
Write-Host "npm gefunden: $([bool]$npmCommand) node gefunden: $([bool]$nodeCommand)" -ForegroundColor Red
Write-Host "Ein 'Als Administrator ausfuehren'-Prozess bekommt seine Umgebung frisch aus der Registry -" -ForegroundColor Red
Write-Host "PATH-Aenderungen, die nur in deiner normalen (nicht-elevierten) Sitzung gelten (z.B. nvm/Volta" -ForegroundColor Red
Write-Host "ohne dauerhafte Registry-Eintragung), sind hier nicht sichtbar, obwohl 'npm run dev' normal funktioniert." -ForegroundColor Red
Write-Host "PATH in diesem Fenster:" -ForegroundColor DarkGray
Write-Host $env:PATH -ForegroundColor DarkGray
Write-Host ""
Write-Host "Loesung: node/npm dauerhaft im PATH eintragen (System-Umgebungsvariablen, nicht nur die Sitzung)," -ForegroundColor Yellow
Write-Host "z.B. ueber die Windows-Systemsteuerung 'Umgebungsvariablen bearbeiten' oder 'setx PATH ...'." -ForegroundColor Yellow
throw "npm/node nicht im administrativen PATH gefunden."
}
# Kill stale instances of THIS project BEFORE checking the port: a leftover
# electron/vite/node/input-helper cluster from a previous run (e.g. a window
# force-closed via Task Manager, which skips main.ts's graceful will-quit
# cleanup and orphans the spawned input-helper.ps1 child) is the most common
# reason a fresh start silently talks to an old instance instead of
# replacing it. Shared with "npm run dev" itself via the predev hook, so
# both start paths get the same guarantee.
& (Join-Path $PSScriptRoot "kill-stale-instances.ps1")
$devPort = 5173
$portInUse = $null
try {
$portInUse = Get-NetTCPConnection -LocalPort $devPort -State Listen -ErrorAction Stop | Select-Object -First 1
} catch {
# Get-NetTCPConnection kann auf manchen Systemen fehlen; das ist kein Fehler.
$portInUse = $null
}
if ($portInUse) {
$portOwner = Get-Process -Id $portInUse.OwningProcess -ErrorAction SilentlyContinue
Write-Host ""
Write-Host "WARNUNG: Port $devPort ist noch belegt (Prozess: $($portOwner.ProcessName), PID $($portInUse.OwningProcess)) - auch nach dem Beenden alter Instanzen dieses Projekts." -ForegroundColor Yellow
Write-Host "Das ist wahrscheinlich ein unabhaengiger Prozess (z.B. ein anderes Projekt auf Port $devPort)." -ForegroundColor Yellow
Write-Host "'npm run dev' weicht dann auf einen anderen Port aus oder verbindet sich mit dem falschen Server." -ForegroundColor Yellow
Write-Host "Falls noetig: Prozess PID $($portInUse.OwningProcess) manuell beenden und dieses Fenster neu starten." -ForegroundColor Yellow
Write-Host ""
}
Set-Location -LiteralPath $project
Write-Host "Starte npm run dev (Administrator)..."
npm run dev
$exitCode = $LASTEXITCODE
if ($exitCode -ne 0) {
Write-Host ""
Write-Host "npm run dev wurde mit Fehlercode $exitCode beendet." -ForegroundColor Red
Write-Host "Die Ausgabe oben zeigt die genaue Ursache (z.B. belegter Port, fehlende Abhaengigkeiten)." -ForegroundColor Red
} else {
Write-Host ""
Write-Host "npm run dev wurde beendet (Fenster schliessen oder Strg+C fuer Neustart)."
}
} catch {
Write-Host ""
Write-Host "Fehler beim Start: $($_.Exception.Message)" -ForegroundColor Red
Write-Host $_.ScriptStackTrace -ForegroundColor DarkGray
}
Write-Host ""
Write-Host "Dieses Fenster bleibt offen (siehe Meldungen oben). Schliessen mit Enter oder dem Fenster-X."