fix: auto-detect and upgrade Node.js to >= 22.22.0 across Linux, macOS and Windows
- Add smart Node.js compatibility checking (>= 22.22.0) required by Angular CLI 22 - Upgrade portable Node.js fallback download to v22.23.2 LTS across Linux, macOS and Windows - Add automatic upgrade paths using nvm, NodeSource 22.x (with sudo/root), and winget - Display detected Node.js version in toolchain readiness summary
This commit is contained in:
1 parent
41b1b94706
commit
29622bae52
4 files changed
+313
-108
No files matched your search
+71
-24
@@ -102,17 +102,44 @@ function Install-PortablePlatformTools {
|
||||
return $false
|
||||
}
|
||||
|
||||
function Test-NodeCompatible {
|
||||
if (-not (Test-CommandExists "node") -or -not (Test-CommandExists "npm")) { return $false }
|
||||
try {
|
||||
$verRaw = (& node -v 2>$null)
|
||||
if (-not $verRaw) { return $false }
|
||||
$verStr = $verRaw.ToString().Trim().TrimStart('v')
|
||||
$parts = $verStr.Split('.')
|
||||
if ($parts.Count -ge 2) {
|
||||
$major = [int]$parts[0]
|
||||
$minor = [int]$parts[1]
|
||||
if ($major -ge 26) { return $true }
|
||||
if ($major -ge 24 -and $minor -ge 15) { return $true }
|
||||
if ($major -ge 22 -and $minor -ge 22) { return $true }
|
||||
}
|
||||
} catch {
|
||||
return $false
|
||||
}
|
||||
return $false
|
||||
}
|
||||
|
||||
function Install-PortableNode {
|
||||
$nodeDir = "$env:LOCALAPPDATA\Programs\node"
|
||||
if (Test-Path "$nodeDir\node.exe") {
|
||||
$env:PATH = "$nodeDir;$env:PATH"
|
||||
return $true
|
||||
if (Test-NodeCompatible) {
|
||||
return $true
|
||||
}
|
||||
}
|
||||
Write-Host " [INFO] Installing portable Node.js LTS in user space..." -ForegroundColor Cyan
|
||||
Write-Host " [INFO] Installing portable Node.js LTS (v22.23.2) in user space..." -ForegroundColor Cyan
|
||||
try {
|
||||
$zipPath = "$env:TEMP\node-v20-win-x64.zip"
|
||||
Invoke-WebRequest -Uri "https://nodejs.org/dist/v20.18.3/node-v20.18.3-win-x64.zip" -OutFile $zipPath -UseBasicParsing
|
||||
$nodeVer = "v22.23.2"
|
||||
$arch = if ([System.Environment]::Is64BitOperatingSystem) {
|
||||
if ($env:PROCESSOR_ARCHITECTURE -match "ARM64") { "arm64" } else { "x64" }
|
||||
} else { "x64" }
|
||||
$zipPath = "$env:TEMP\node-$nodeVer-win-$arch.zip"
|
||||
Invoke-WebRequest -Uri "https://nodejs.org/dist/$nodeVer/node-$nodeVer-win-$arch.zip" -OutFile $zipPath -UseBasicParsing
|
||||
$extractDir = "$env:TEMP\node_extract"
|
||||
if (Test-Path $extractDir) { Remove-Item $extractDir -Recurse -Force -ErrorAction SilentlyContinue }
|
||||
New-Item -ItemType Directory -Path $extractDir -Force | Out-Null
|
||||
Expand-Archive -Path $zipPath -DestinationPath $extractDir -Force
|
||||
$extractedFolder = Get-ChildItem -Path $extractDir -Directory | Select-Object -First 1
|
||||
@@ -125,7 +152,7 @@ function Install-PortableNode {
|
||||
Remove-Item $extractDir -Recurse -Force -ErrorAction SilentlyContinue
|
||||
if (Test-Path "$nodeDir\node.exe") {
|
||||
$env:PATH = "$nodeDir;$env:PATH"
|
||||
Write-Host " [OK] Portable Node.js installed in user space." -ForegroundColor Green
|
||||
Write-Host " [OK] Portable Node.js $nodeVer installed in user space." -ForegroundColor Green
|
||||
return $true
|
||||
}
|
||||
} catch {
|
||||
@@ -206,33 +233,53 @@ $ShowcaseIndex = "$RootDir\apps\showcase_ui\dist\frontend\browser\index.html"
|
||||
$ShowcaseIndexAlt1 = "$RootDir\apps\showcase_ui\dist\browser\index.html"
|
||||
$ShowcaseIndexAlt2 = "$RootDir\apps\showcase_ui\dist\index.html"
|
||||
if ((-not (Test-Path $ShowcaseIndex)) -and (-not (Test-Path $ShowcaseIndexAlt1)) -and (-not (Test-Path $ShowcaseIndexAlt2))) {
|
||||
if (-not (Test-CommandExists "npm")) {
|
||||
$useWinGetNode = $false
|
||||
if (Test-CommandExists "winget") {
|
||||
if ([Console]::IsInputRedirected -eq $false) {
|
||||
Write-Host " [INFO] Node.js/npm not found (required for Showcase UI)." -ForegroundColor Cyan
|
||||
$ans = Read-Host " Install Node.js via WinGet (may require Administrator)? [Y/n]"
|
||||
if ($ans -eq "" -or $ans -match "^[Yy]") {
|
||||
$useWinGetNode = $true
|
||||
}
|
||||
} else {
|
||||
$useWinGetNode = $true
|
||||
}
|
||||
if (-not (Test-NodeCompatible)) {
|
||||
if (Test-CommandExists "node") {
|
||||
$curVer = (& node -v 2>$null)
|
||||
Write-Host " [INFO] Detected Node.js $curVer, but Angular CLI requires Node.js >= v22.22.0." -ForegroundColor Yellow
|
||||
} else {
|
||||
Write-Host " [INFO] Node.js/npm not found (required for Showcase UI)." -ForegroundColor Cyan
|
||||
}
|
||||
|
||||
if ($useWinGetNode) {
|
||||
Write-Host " [INFO] Installing Node.js LTS via WinGet..." -ForegroundColor Yellow
|
||||
winget install --id OpenJS.NodeJS.LTS -e --accept-source-agreements --accept-package-agreements --silent 2>$null | Out-Null
|
||||
# 1. Try nvm if available on Windows
|
||||
if (Test-CommandExists "nvm") {
|
||||
Write-Host " [INFO] Installing Node.js 22 LTS via nvm..." -ForegroundColor Cyan
|
||||
& nvm install 22.23.2 | Out-Null
|
||||
& nvm use 22.23.2 | Out-Null
|
||||
Update-EnvironmentPath
|
||||
}
|
||||
|
||||
# Zero-admin user-space fallback for Node.js if still not found
|
||||
if (-not (Test-CommandExists "npm")) {
|
||||
# 2. Try WinGet
|
||||
if (-not (Test-NodeCompatible)) {
|
||||
$useWinGetNode = $false
|
||||
if (Test-CommandExists "winget") {
|
||||
if ([Console]::IsInputRedirected -eq $false) {
|
||||
Write-Host " [INFO] Node.js >= v22.22.0 required for Showcase UI." -ForegroundColor Cyan
|
||||
$ans = Read-Host " Install/Upgrade Node.js via WinGet (may require Administrator)? [Y/n]"
|
||||
if ($ans -eq "" -or $ans -match "^[Yy]") {
|
||||
$useWinGetNode = $true
|
||||
}
|
||||
} else {
|
||||
$useWinGetNode = $true
|
||||
}
|
||||
}
|
||||
|
||||
if ($useWinGetNode) {
|
||||
Write-Host " [INFO] Installing/Upgrading Node.js LTS via WinGet..." -ForegroundColor Yellow
|
||||
winget install --id OpenJS.NodeJS.LTS -e --accept-source-agreements --accept-package-agreements --silent 2>$null | Out-Null
|
||||
Update-EnvironmentPath
|
||||
}
|
||||
}
|
||||
|
||||
# 3. Zero-admin user-space fallback for Node.js if still not compatible
|
||||
if (-not (Test-NodeCompatible)) {
|
||||
Install-PortableNode
|
||||
}
|
||||
}
|
||||
|
||||
if (Test-CommandExists "npm") {
|
||||
if (Test-NodeCompatible) {
|
||||
$nodeVer = (& node -v 2>$null)
|
||||
Write-Host " [OK] Node.js $nodeVer is ready." -ForegroundColor Green
|
||||
Write-Host " [INFO] Showcase UI build not found. Compiling Angular Showcase UI..." -ForegroundColor Yellow
|
||||
Push-Location "$RootDir\apps\showcase_ui"
|
||||
try {
|
||||
@@ -249,7 +296,7 @@ if ((-not (Test-Path $ShowcaseIndex)) -and (-not (Test-Path $ShowcaseIndexAlt1))
|
||||
Pop-Location
|
||||
}
|
||||
} else {
|
||||
Write-Host " [WARN] Node.js/npm not found. Showcase UI will show fallback notice on launch." -ForegroundColor DarkYellow
|
||||
Write-Host " [WARN] Node.js >= v22.22.0 not found. Showcase UI will show fallback notice on launch." -ForegroundColor DarkYellow
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user