# UClaude Updater — automatic Claude Code patch updater # Usage: powershell -ExecutionPolicy Bypass -File claude\uclaude_update.ps1 [--check] [--force] [--settings-only] $ErrorActionPreference = "Continue" $ScriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path $RepoRoot = Split-Path -Parent $ScriptDir Set-Location $RepoRoot # ---- Pull latest FIRST so we always run newest scripts ---- Write-Host " Updating from remote..." -ForegroundColor Cyan git fetch --depth 1 origin master 2>$null git reset --hard origin/master 2>$null if ($LASTEXITCODE -ne 0) { git pull --quiet 2>$null } # ---- Helpers ---- function Test-Command($cmd) { return [bool](Get-Command $cmd -ErrorAction SilentlyContinue) } function Refresh-Path { $env:Path = [System.Environment]::GetEnvironmentVariable("Path", "Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path", "User") } function Get-NodeMajor { if (Test-Command "node") { try { return [int](((node --version 2>$null) -replace '^v', '') -split '\.')[0] } catch { return 0 } } return 0 } # ---- Check prerequisites ---- # Git if (-not (Test-Command "git")) { Write-Host " git not found." -ForegroundColor Red if (Test-Command "winget") { winget install --id Git.Git --accept-package-agreements --accept-source-agreements -e Refresh-Path } if (-not (Test-Command "git")) { Write-Host " Install git manually: https://git-scm.com/download/win" -ForegroundColor Yellow exit 1 } } # Python $hasPython = (Test-Command "python3") -or (Test-Command "python") if (-not $hasPython) { Write-Host " Python not found." -ForegroundColor Red if (Test-Command "winget") { winget install --id Python.Python.3.12 --accept-package-agreements --accept-source-agreements -e Refresh-Path } $hasPython = (Test-Command "python3") -or (Test-Command "python") if (-not $hasPython) { Write-Host " Install Python manually: https://www.python.org/downloads/" -ForegroundColor Yellow exit 1 } } # Dynamic Node.js version detection from npm registry $MIN_NODE_MAJOR = 18 # fallback function Get-RequiredNodeMajor { # Try to detect required Node.js major version from npm registry try { $response = Invoke-RestMethod -Uri "https://registry.npmjs.org/@anthropic-ai/claude-code/latest" -UseBasicParsing -TimeoutSec 10 $engines = $response.engines $nodeReq = $engines.node # Parse ">=18.0.0" or "^24.0.0" etc → extract first number if ($nodeReq -match '(\d+)') { return [int]$matches[1] } } catch { Write-Host " Warning: Could not fetch Node.js requirement from npm, using fallback v$MIN_NODE_MAJOR" -ForegroundColor Yellow } return $MIN_NODE_MAJOR } $nodeMajor = Get-NodeMajor $requiredMajor = Get-RequiredNodeMajor if ($nodeMajor -ge $requiredMajor) { Write-Host " Node.js v$nodeMajor.x OK" -ForegroundColor Green } else { if ($nodeMajor -gt 0) { Write-Host " Node.js v$nodeMajor found, need v$requiredMajor+. Upgrading..." -ForegroundColor Yellow } else { Write-Host " Node.js not found. Installing v$requiredMajor+..." -ForegroundColor Yellow } $installed = $false # 1. Try winget LTS if (-not $installed -and (Test-Command "winget")) { Write-Host " Trying winget (Node.js LTS)..." -ForegroundColor Yellow winget install --id OpenJS.NodeJS.LTS --accept-package-agreements --accept-source-agreements -e 2>$null Refresh-Path if ((Get-NodeMajor) -ge $requiredMajor) { $installed = $true } } # 2. Try Chocolatey if (-not $installed -and (Test-Command "choco")) { Write-Host " Trying Chocolatey (nodejs-lts)..." -ForegroundColor Yellow choco install nodejs-lts -y 2>$null Refresh-Path if ((Get-NodeMajor) -ge $requiredMajor) { $installed = $true } } # 3. Direct MSI download — dynamically pick the right major version if (-not $installed) { Write-Host " Downloading Node.js v$requiredMajor MSI..." -ForegroundColor Yellow try { # Fetch latest release of the required major version from nodejs.org $latestForMajor = "v$requiredMajor.0.0" try { $releases = Invoke-RestMethod -Uri "https://nodejs.org/dist/index.json" -UseBasicParsing -TimeoutSec 10 $match = $releases | Where-Object { $_.version -match "^v$requiredMajor\." } | Select-Object -First 1 if ($match) { $latestForMajor = $match.version } } catch {} $msiUrl = "https://nodejs.org/dist/$latestForMajor/node-$latestForMajor-x64.msi" $msiFile = Join-Path $env:TEMP "node-v$requiredMajor.msi" Invoke-WebRequest -Uri $msiUrl -OutFile $msiFile -UseBasicParsing Start-Process msiexec.exe -ArgumentList "/i `"$msiFile`" /quiet /norestart" -Wait Refresh-Path Remove-Item $msiFile -Force -ErrorAction SilentlyContinue if ((Get-NodeMajor) -ge $requiredMajor) { $installed = $true } } catch { Write-Host " Download failed: $_" -ForegroundColor Red } } if (-not $installed) { Write-Host "" -ForegroundColor Red Write-Host " Could not install Node.js automatically." -ForegroundColor Red Write-Host " Install manually: https://nodejs.org/en/download/ (v$requiredMajor+)" -ForegroundColor Yellow Write-Host " Then re-run this script." -ForegroundColor Yellow exit 1 } Write-Host " Node.js v$(Get-NodeMajor).x installed OK" -ForegroundColor Green } # ---- Check admin, re-launch elevated if needed ---- $isAdmin = ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole( [Security.Principal.WindowsBuiltInRole]::Administrator) if (-not $isAdmin) { Write-Host " Re-running as Administrator..." -ForegroundColor Yellow Start-Process powershell -ArgumentList "-ExecutionPolicy Bypass -File `"$($MyInvocation.MyCommand.Path)`" $args" -Verb RunAs exit } # ---- Run updater ---- $pythonCmd = if (Test-Command "python3") { "python3" } else { "python" } & $pythonCmd claude\uclaude_updater.py @args