Skip to content

Commit 5e5b37d

Browse files
authored
Add functions to install and upgrade Node.js via winget
1 parent 036f8e4 commit 5e5b37d

File tree

1 file changed

+78
-66
lines changed

1 file changed

+78
-66
lines changed

deepstudio/deepstudio/private-install-v2.ps1

Lines changed: 78 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,82 @@ function Mask-Url([string]$url) {
7171
}
7272
}
7373

74+
function Install-NodeViaWinget {
75+
if (-not (Get-Command winget -ErrorAction SilentlyContinue)) {
76+
Fail "winget not found — cannot auto-install Node.js."
77+
Info "Please install Node.js manually:"
78+
Info "👉 https://nodejs.org/en/download"
79+
Write-Host ""
80+
exit 1
81+
}
82+
83+
Info "📥 Installing Node.js LTS via winget..."
84+
Write-Host ""
85+
try {
86+
& winget install --id OpenJS.NodeJS.LTS --accept-source-agreements --accept-package-agreements
87+
# winget exit codes: 0 = success, -1978335189 = already installed (no error)
88+
if ($LASTEXITCODE -ne 0 -and $LASTEXITCODE -ne -1978335189) {
89+
Fail "winget install returned exit code $LASTEXITCODE."
90+
exit 1
91+
}
92+
# Refresh PATH so node/npm are available in the current session
93+
$env:PATH = [System.Environment]::GetEnvironmentVariable("PATH", "Machine") + ";" + [System.Environment]::GetEnvironmentVariable("PATH", "User")
94+
if (Get-Command node -ErrorAction SilentlyContinue) {
95+
Success "Node.js is ready."
96+
} else {
97+
Warn "Node.js installed but 'node' not found in PATH. You may need to restart your terminal."
98+
exit 1
99+
}
100+
}
101+
catch {
102+
Fail "Failed to install Node.js: $($_.Exception.Message)"
103+
exit 1
104+
}
105+
}
106+
107+
function Upgrade-NodeViaWinget {
108+
if (-not (Get-Command winget -ErrorAction SilentlyContinue)) {
109+
Fail "winget not found — cannot auto-upgrade Node.js."
110+
Info "Please upgrade Node.js manually:"
111+
Info "👉 https://nodejs.org/en/download"
112+
Write-Host ""
113+
exit 1
114+
}
115+
116+
Info "📥 Upgrading Node.js LTS via winget..."
117+
Write-Host ""
118+
try {
119+
& winget upgrade --id OpenJS.NodeJS.LTS --accept-source-agreements --accept-package-agreements
120+
# winget exit codes: 0 = success, -1978335189 = already up to date (no error)
121+
if ($LASTEXITCODE -ne 0 -and $LASTEXITCODE -ne -1978335189) {
122+
# If upgrade fails, try uninstall + reinstall as fallback
123+
Warn "winget upgrade returned exit code $LASTEXITCODE. Trying reinstall..."
124+
& winget uninstall --id OpenJS.NodeJS.LTS --accept-source-agreements 2>$null
125+
& winget install --id OpenJS.NodeJS.LTS --accept-source-agreements --accept-package-agreements
126+
if ($LASTEXITCODE -ne 0 -and $LASTEXITCODE -ne -1978335189) {
127+
Fail "winget reinstall returned exit code $LASTEXITCODE."
128+
exit 1
129+
}
130+
}
131+
# Refresh PATH
132+
$env:PATH = [System.Environment]::GetEnvironmentVariable("PATH", "Machine") + ";" + [System.Environment]::GetEnvironmentVariable("PATH", "User")
133+
if (Get-Command node -ErrorAction SilentlyContinue) {
134+
$newVersionStr = (& node --version 2>$null) -replace '^v', ''
135+
Success "Node.js is now at v$newVersionStr."
136+
} else {
137+
Warn "Node.js upgraded but 'node' not found in PATH. You may need to restart your terminal."
138+
exit 1
139+
}
140+
}
141+
catch {
142+
Fail "Failed to upgrade Node.js: $($_.Exception.Message)"
143+
exit 1
144+
}
145+
}
146+
74147
function Require-Node {
75-
$nodeAvailable = [bool](Get-Command node -ErrorAction SilentlyContinue)
76148
$MinNodeVersion = 22
149+
$nodeAvailable = [bool](Get-Command node -ErrorAction SilentlyContinue)
77150

78151
if (-not $nodeAvailable) {
79152
Write-Host ""
@@ -82,40 +155,11 @@ function Require-Node {
82155
Write-Host " │ Node.js >= $MinNodeVersion is required to continue. │" -ForegroundColor Yellow
83156
Write-Host " └─────────────────────────────────────────────────────┘" -ForegroundColor Yellow
84157
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-
94158
Write-Host " 📥 " -NoNewline -ForegroundColor Cyan
95159
$installChoice = Read-Host "Install Node.js via winget? [Y/n]"
96160
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-
}
161+
Install-NodeViaWinget
162+
$nodeAvailable = [bool](Get-Command node -ErrorAction SilentlyContinue)
119163
} else {
120164
Fail "Node.js is required. Please install it and try again."
121165
Info "👉 https://nodejs.org/en/download"
@@ -136,50 +180,18 @@ function Require-Node {
136180
if ($major -lt $MinNodeVersion) {
137181
Write-Host ""
138182
Write-Host " ┌─────────────────────────────────────────────────────┐" -ForegroundColor Yellow
139-
Write-Host " │ ⬆️ Node.js upgrade recommended" -ForegroundColor Yellow
183+
Write-Host " │ ⬆️ Node.js upgrade required " -ForegroundColor Yellow
140184
Write-Host " │ Current: v$versionStr" -NoNewline -ForegroundColor Yellow
141-
# Pad to fill the box
142185
$pad = 39 - "Current: v$versionStr".Length
143186
if ($pad -lt 0) { $pad = 0 }
144187
Write-Host (" " * $pad + "") -ForegroundColor Yellow
145188
Write-Host " │ Required: >= v$MinNodeVersion.0.0 │" -ForegroundColor Yellow
146189
Write-Host " └─────────────────────────────────────────────────────┘" -ForegroundColor Yellow
147190
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-
157191
Write-Host " ⬆️ " -NoNewline -ForegroundColor Cyan
158192
$upgradeChoice = Read-Host "Upgrade Node.js to latest LTS via winget? [Y/n]"
159193
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-
}
194+
Upgrade-NodeViaWinget
183195
} else {
184196
Fail "Node.js >= v$MinNodeVersion is required. Please upgrade and try again."
185197
Info "👉 https://nodejs.org/en/download"

0 commit comments

Comments
 (0)