mirror of
https://git.hexahost.dev/smueller/HexaHost-Frontend.git
synced 2026-06-02 06:08:42 +00:00
186 lines
5.1 KiB
PowerShell
186 lines
5.1 KiB
PowerShell
#Requires -Version 5.1
|
|
<#
|
|
.SYNOPSIS
|
|
Erstellt einen Production-Build und veroeffentlicht ihn auf den Branch main.
|
|
|
|
.DESCRIPTION
|
|
1. Wechselt auf main und setzt ihn auf den Stand von dev
|
|
2. Entfernt Kommentare, minifiziert CSS, obfuskiert JavaScript
|
|
3. Committet und pusht main (optional)
|
|
4. Wechselt zurueck auf dev (Quellcode bleibt unveraendert)
|
|
|
|
.PARAMETER Push
|
|
Pusht main nach origin (Standard: nur lokaler Commit)
|
|
|
|
.PARAMETER DryRun
|
|
Fuehrt Git-Schritte nur simuliert aus (Build wird trotzdem erstellt)
|
|
|
|
.PARAMETER AllowDirty
|
|
Erlaubt uncommittete Aenderungen
|
|
|
|
.PARAMETER Message
|
|
Commit-Nachricht fuer den Production-Build
|
|
|
|
.EXAMPLE
|
|
.\scripts\publish-to-main.ps1
|
|
|
|
.EXAMPLE
|
|
.\scripts\publish-to-main.ps1 -Push
|
|
#>
|
|
[CmdletBinding()]
|
|
param(
|
|
[switch]$Push,
|
|
[switch]$DryRun,
|
|
[switch]$AllowDirty,
|
|
[string]$Message = ""
|
|
)
|
|
|
|
$ErrorActionPreference = "Stop"
|
|
$Root = (Resolve-Path (Join-Path $PSScriptRoot "..")).Path
|
|
$BuildDir = Join-Path $Root "scripts\build"
|
|
$OriginalBranch = ""
|
|
|
|
function Write-Step([string]$Text) {
|
|
Write-Host ""
|
|
Write-Host "==> $Text" -ForegroundColor Cyan
|
|
}
|
|
|
|
function Ensure-GitClean {
|
|
$status = git -C $Root status --porcelain
|
|
if ($status) {
|
|
throw "Uncommittete Aenderungen im Repository. Bitte zuerst committen oder stashen."
|
|
}
|
|
}
|
|
|
|
function Resolve-NodeTool([string]$ToolName) {
|
|
$command = Get-Command $ToolName -ErrorAction SilentlyContinue
|
|
if ($command) {
|
|
return $command.Source
|
|
}
|
|
|
|
$candidates = @(
|
|
(Join-Path $env:ProgramFiles "nodejs\$ToolName.cmd"),
|
|
(Join-Path ${env:ProgramFiles(x86)} "nodejs\$ToolName.cmd"),
|
|
(Join-Path $env:LOCALAPPDATA "Programs\nodejs\$ToolName.cmd"),
|
|
"c:\Program Files\cursor\resources\app\resources\helpers\node.exe"
|
|
)
|
|
|
|
foreach ($candidate in $candidates) {
|
|
if ($ToolName -eq "node" -and (Test-Path $candidate)) {
|
|
return $candidate
|
|
}
|
|
if ($ToolName -ne "node" -and (Test-Path $candidate)) {
|
|
return $candidate
|
|
}
|
|
}
|
|
|
|
return $null
|
|
}
|
|
|
|
function Ensure-Node {
|
|
$script:NodeExe = Resolve-NodeTool "node"
|
|
$script:NpmExe = Resolve-NodeTool "npm"
|
|
|
|
if (-not $script:NodeExe) {
|
|
throw "Node.js ist nicht installiert. Bitte Node.js 18+ installieren: https://nodejs.org/"
|
|
}
|
|
if (-not $script:NpmExe) {
|
|
throw "npm wurde nicht gefunden. Bitte Node.js inkl. npm installieren und PATH setzen."
|
|
}
|
|
}
|
|
|
|
try {
|
|
Set-Location $Root
|
|
Ensure-Node
|
|
if (-not $AllowDirty) {
|
|
Ensure-GitClean
|
|
} else {
|
|
Write-Warning "AllowDirty aktiv - uncommittete Aenderungen werden mit veroeffentlicht."
|
|
}
|
|
|
|
$OriginalBranch = (git branch --show-current).Trim()
|
|
if ($OriginalBranch -ne "dev") {
|
|
Write-Warning "Empfohlen: Auf Branch 'dev' starten (aktuell: $OriginalBranch)"
|
|
}
|
|
|
|
if ([string]::IsNullOrWhiteSpace($Message)) {
|
|
$Message = "chore(release): production build $(Get-Date -Format 'yyyy-MM-dd HH:mm')"
|
|
}
|
|
|
|
Write-Step "Installiere Build-Abhaengigkeiten"
|
|
Set-Location $BuildDir
|
|
if (-not $DryRun) {
|
|
& $NpmExe ci --no-fund --no-audit
|
|
if ($LASTEXITCODE -ne 0) { throw "npm ci fehlgeschlagen" }
|
|
}
|
|
|
|
Write-Step "Wechsle auf main und synchronisiere mit dev"
|
|
Set-Location $Root
|
|
if ($DryRun) {
|
|
Write-Host '[DryRun] git checkout main'
|
|
Write-Host '[DryRun] git reset --hard dev'
|
|
} else {
|
|
git checkout main
|
|
git reset --hard dev
|
|
}
|
|
|
|
Write-Step "Production-Build (Kommentare entfernen, JS obfuscaten)"
|
|
Set-Location $BuildDir
|
|
if ($DryRun) {
|
|
Write-Host '[DryRun] npm run build:in-place'
|
|
} else {
|
|
& $NpmExe run build:in-place
|
|
if ($LASTEXITCODE -ne 0) { throw "Production-Build fehlgeschlagen" }
|
|
}
|
|
|
|
Write-Step "Production-Build committen"
|
|
Set-Location $Root
|
|
if ($DryRun) {
|
|
Write-Host '[DryRun] git add -A'
|
|
Write-Host ('[DryRun] git commit -m "' + $Message + '"')
|
|
} else {
|
|
git add -A
|
|
$null = git diff --cached --quiet
|
|
if ($LASTEXITCODE -eq 0) {
|
|
Write-Warning "Keine Build-Aenderungen - nichts zu committen."
|
|
} else {
|
|
git commit -m $Message
|
|
}
|
|
}
|
|
|
|
if ($Push) {
|
|
Write-Step "Push nach origin/main"
|
|
if ($DryRun) {
|
|
Write-Host '[DryRun] git push origin main'
|
|
} else {
|
|
git push origin main
|
|
}
|
|
} else {
|
|
Write-Host "Hinweis: Ohne -Push wurde nur lokal auf main gebaut." -ForegroundColor Yellow
|
|
}
|
|
|
|
Write-Step "Zurueck auf $OriginalBranch"
|
|
if (-not $DryRun) {
|
|
if ([string]::IsNullOrWhiteSpace($OriginalBranch)) {
|
|
git checkout dev
|
|
} else {
|
|
git checkout $OriginalBranch
|
|
}
|
|
}
|
|
|
|
Write-Host ""
|
|
Write-Host "Production-Release abgeschlossen." -ForegroundColor Green
|
|
if (-not $Push -and -not $DryRun) {
|
|
Write-Host "Zum Veroeffentlichen: git push origin main" -ForegroundColor Yellow
|
|
}
|
|
}
|
|
catch {
|
|
Write-Host ""
|
|
Write-Host ('FEHLER: ' + $_.Exception.Message) -ForegroundColor Red
|
|
Set-Location $Root
|
|
if ($OriginalBranch -and -not $DryRun) {
|
|
git checkout $OriginalBranch 2>$null
|
|
}
|
|
exit 1
|
|
}
|