@@ -71,13 +71,131 @@ function Mask-Url([string]$url) {
7171 }
7272}
7373
74- function Require-Npm {
75- if (-not (Get-Command npm - ErrorAction SilentlyContinue)) {
74+ function Require-Node {
75+ $nodeAvailable = [bool ](Get-Command node - ErrorAction SilentlyContinue)
76+ $MinNodeVersion = 22
77+
78+ if (-not $nodeAvailable ) {
7679 Write-Host " "
77- Fail " Node.js / npm not found."
78- Info " Please install Node.js first:"
79- Info " 👉 https://nodejs.org/en/download"
80+ Write-Host " ┌─────────────────────────────────────────────────────┐" - ForegroundColor Yellow
81+ Write-Host " │ ⚙️ Node.js is not installed │" - ForegroundColor Yellow
82+ Write-Host " │ Node.js >= $MinNodeVersion is required to continue. │" - ForegroundColor Yellow
83+ Write-Host " └─────────────────────────────────────────────────────┘" - ForegroundColor Yellow
8084 Write-Host " "
85+
86+ if (-not (Get-Command winget - ErrorAction SilentlyContinue)) {
87+ Fail " winget not found — cannot auto-install Node.js."
88+ Info " Please install Node.js manually:"
89+ Info " 👉 https://nodejs.org/en/download"
90+ Write-Host " "
91+ exit 1
92+ }
93+
94+ Write-Host " 📥 " - NoNewline - ForegroundColor Cyan
95+ $installChoice = Read-Host " Install Node.js via winget? [Y/n]"
96+ if ([string ]::IsNullOrWhiteSpace($installChoice ) -or $installChoice -match ' ^[Yy]' ) {
97+ Info " 📥 Installing Node.js via winget..."
98+ Write-Host " "
99+ try {
100+ & winget install -- id OpenJS.NodeJS.LTS -- accept- source- agreements -- accept- package- agreements
101+ if ($LASTEXITCODE -ne 0 ) {
102+ Fail " winget install returned exit code $LASTEXITCODE ."
103+ exit 1
104+ }
105+ # Refresh PATH so node/npm are available in the current session
106+ $env: PATH = [System.Environment ]::GetEnvironmentVariable(" PATH" , " Machine" ) + " ;" + [System.Environment ]::GetEnvironmentVariable(" PATH" , " User" )
107+ if (Get-Command node - ErrorAction SilentlyContinue) {
108+ Success " Node.js installed successfully."
109+ $nodeAvailable = $true
110+ } else {
111+ Warn " Node.js installed but 'node' not found in PATH. You may need to restart your terminal."
112+ exit 1
113+ }
114+ }
115+ catch {
116+ Fail " Failed to install Node.js: $ ( $_.Exception.Message ) "
117+ exit 1
118+ }
119+ } else {
120+ Fail " Node.js is required. Please install it and try again."
121+ Info " 👉 https://nodejs.org/en/download"
122+ Write-Host " "
123+ exit 1
124+ }
125+ }
126+
127+ # Check Node.js version — must be >= MinNodeVersion
128+ if ($nodeAvailable ) {
129+ try {
130+ $versionStr = & node -- version 2> $null
131+ # node --version returns e.g. "v22.1.0"
132+ $versionStr = $versionStr -replace ' ^v' , ' '
133+ $major = [int ]($versionStr.Split (' .' )[0 ])
134+ Dim " Node.js version: v$versionStr "
135+
136+ if ($major -lt $MinNodeVersion ) {
137+ Write-Host " "
138+ Write-Host " ┌─────────────────────────────────────────────────────┐" - ForegroundColor Yellow
139+ Write-Host " │ ⬆️ Node.js upgrade recommended │" - ForegroundColor Yellow
140+ Write-Host " │ Current: v$versionStr " - NoNewline - ForegroundColor Yellow
141+ # Pad to fill the box
142+ $pad = 39 - " Current: v$versionStr " .Length
143+ if ($pad -lt 0 ) { $pad = 0 }
144+ Write-Host (" " * $pad + " │" ) - ForegroundColor Yellow
145+ Write-Host " │ Required: >= v$MinNodeVersion .0.0 │" - ForegroundColor Yellow
146+ Write-Host " └─────────────────────────────────────────────────────┘" - ForegroundColor Yellow
147+ Write-Host " "
148+
149+ if (-not (Get-Command winget - ErrorAction SilentlyContinue)) {
150+ Fail " winget not found — cannot auto-upgrade Node.js."
151+ Info " Please upgrade Node.js manually:"
152+ Info " 👉 https://nodejs.org/en/download"
153+ Write-Host " "
154+ exit 1
155+ }
156+
157+ Write-Host " ⬆️ " - NoNewline - ForegroundColor Cyan
158+ $upgradeChoice = Read-Host " Upgrade Node.js to latest LTS via winget? [Y/n]"
159+ if ([string ]::IsNullOrWhiteSpace($upgradeChoice ) -or $upgradeChoice -match ' ^[Yy]' ) {
160+ Info " 📥 Upgrading Node.js via winget..."
161+ Write-Host " "
162+ try {
163+ & winget upgrade -- id OpenJS.NodeJS.LTS -- accept- source- agreements -- accept- package- agreements
164+ if ($LASTEXITCODE -ne 0 ) {
165+ Fail " winget upgrade returned exit code $LASTEXITCODE ."
166+ exit 1
167+ }
168+ # Refresh PATH
169+ $env: PATH = [System.Environment ]::GetEnvironmentVariable(" PATH" , " Machine" ) + " ;" + [System.Environment ]::GetEnvironmentVariable(" PATH" , " User" )
170+ $newVersionStr = (& node -- version 2> $null ) -replace ' ^v' , ' '
171+ $newMajor = [int ]($newVersionStr.Split (' .' )[0 ])
172+ if ($newMajor -ge $MinNodeVersion ) {
173+ Success " Node.js upgraded to v$newVersionStr ."
174+ } else {
175+ Warn " Node.js is now v$newVersionStr but still below v$MinNodeVersion . You may need to restart your terminal."
176+ exit 1
177+ }
178+ }
179+ catch {
180+ Fail " Failed to upgrade Node.js: $ ( $_.Exception.Message ) "
181+ exit 1
182+ }
183+ } else {
184+ Fail " Node.js >= v$MinNodeVersion is required. Please upgrade and try again."
185+ Info " 👉 https://nodejs.org/en/download"
186+ Write-Host " "
187+ exit 1
188+ }
189+ }
190+ }
191+ catch {
192+ Warn " Could not determine Node.js version: $ ( $_.Exception.Message ) "
193+ }
194+ }
195+
196+ # Final check: npm must be available
197+ if (-not (Get-Command npm - ErrorAction SilentlyContinue)) {
198+ Fail " npm not found (Node.js may need a terminal restart)."
81199 exit 1
82200 }
83201}
@@ -292,7 +410,7 @@ function Get-AzAccessToken {
292410# ---------------------------
293411# Start
294412# ---------------------------
295- Require- Npm
413+ Require- Node
296414
297415Show-Banner
298416
0 commit comments