Skip to content

Commit 73ac1cc

Browse files
committed
Refactor installation scripts to improve error handling, code structure, and corepack usage
- we prefer pnpm from corepack - cleaning up takes time and should be a separate step at the end (with spinner) - add empty line above 'cleaning up'
1 parent b19bcdd commit 73ac1cc

2 files changed

Lines changed: 128 additions & 229 deletions

File tree

‎install.ps1‎

Lines changed: 66 additions & 134 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,31 @@
11
$ErrorActionPreference = "Stop"
2-
$targetScheme = if ($args.Count -gt 0) { $args[0] } else { $null }
2+
$targetScheme = $args[0]
33

4-
if (!(Get-Command git -ErrorAction SilentlyContinue)) {
5-
Write-Host "Error: git is required but not installed." -ForegroundColor Red
4+
function Fail($Message) {
5+
Write-Host "Error: $Message" -ForegroundColor Red
66
Pause
77
exit 1
88
}
99

10-
$nodeIsGood = $false
11-
if (Get-Command node -ErrorAction SilentlyContinue) {
12-
$nodeVersion = (node -v) -replace '^v',''
13-
$major = [int]($nodeVersion -split '\.')[0]
14-
if ($major -ge 20) {
15-
$nodeIsGood = $true
16-
}
10+
if (!(Get-Command git -ErrorAction SilentlyContinue)) {
11+
Fail "git is required but not installed."
1712
}
1813

19-
if (!$nodeIsGood) {
20-
$switched = $false
14+
$nodeIsGood = (Get-Command node -ErrorAction SilentlyContinue) -and ([version]((node -v) -replace '^v','')).Major -ge 20
2115

22-
if (!$switched -and (Get-Command fnm -ErrorAction SilentlyContinue)) {
16+
if (!$nodeIsGood) {
17+
if (Get-Command fnm -ErrorAction SilentlyContinue) {
2318
fnm env --shell powershell | Out-String | Invoke-Expression
2419
fnm install 22 *>$null
2520
fnm use 22 *>$null
26-
$switched = $true
27-
}
28-
29-
if (!$switched -and (Get-Command nvm -ErrorAction SilentlyContinue)) {
21+
} elseif (Get-Command nvm -ErrorAction SilentlyContinue) {
3022
nvm install 22 *>$null
3123
nvm use 22 *>$null
32-
$switched = $true
33-
}
34-
35-
if (!$switched) {
36-
Write-Host "Error: Failed to find a compatible Node.js version and no supported version manager (fnm, nvm) could switch it." -ForegroundColor Red
37-
Pause
38-
exit 1
24+
} else {
25+
Fail "Failed to find a compatible Node.js version and no supported version manager (fnm, nvm) could switch it."
3926
}
4027
}
4128

42-
$availableEditors = @()
43-
$editorCmds = @()
44-
$editorSchemes = @()
45-
4629
$editorsToCheck = @(
4730
@{ Name = "VS Code"; Cmd = "code"; Scheme = "vscode" },
4831
@{ Name = "VS Code Insiders"; Cmd = "code-insiders"; Scheme = "vscode-insiders" },
@@ -51,39 +34,23 @@ $editorsToCheck = @(
5134
@{ Name = "Antigravity"; Cmd = "agy-ide"; Scheme = "agy" }
5235
)
5336

54-
foreach ($editor in $editorsToCheck) {
55-
if (Get-Command $editor.Cmd -ErrorAction SilentlyContinue) {
56-
$availableEditors += $editor.Name
57-
$editorCmds += $editor.Cmd
58-
$editorSchemes += $editor.Scheme
59-
}
60-
}
37+
$availableEditors = @($editorsToCheck | Where-Object { Get-Command $_.Cmd -ErrorAction SilentlyContinue })
6138

6239
if ($availableEditors.Count -eq 0) {
63-
Write-Host "Error: No supported editors found in your PATH." -ForegroundColor Red
64-
Pause
65-
exit 1
40+
Fail "No supported editors found in your PATH."
6641
}
6742

68-
$autoChoice = $null
69-
if ($targetScheme) {
70-
for ($i = 0; $i -lt $editorSchemes.Count; $i++) {
71-
if ($editorSchemes[$i] -eq $targetScheme) {
72-
$autoChoice = $i + 1
73-
break
74-
}
75-
}
76-
}
43+
$idx = [Array]::IndexOf(@($availableEditors.Scheme), $targetScheme)
7744

78-
if ($autoChoice) {
79-
$choice = $autoChoice.ToString()
45+
if ($targetScheme -and $idx -ge 0) {
46+
$selectedIndex = $idx
8047
} else {
8148
Write-Host "`nHint: If your editor is installed but not listed, you might need to add its command to your PATH." -ForegroundColor Yellow
8249
Write-Host "Open your editor, open the Command Palette (Ctrl+Shift+P / Cmd+Shift+P), and find a command like: `"Shell Command: Install 'cursor' command in PATH`" (or 'agy-ide', etc.)." -ForegroundColor Yellow
8350

8451
Write-Host ""
8552
for ($i = 0; $i -lt $availableEditors.Count; $i++) {
86-
Write-Host "$($i + 1)) $($availableEditors[$i])"
53+
Write-Host "$($i + 1)) $($availableEditors[$i].Name)"
8754
}
8855
if ($availableEditors.Count -gt 1) {
8956
Write-Host "a) All detected editors"
@@ -92,139 +59,104 @@ if ($autoChoice) {
9259

9360
$choice = Read-Host "Select where to install CWC to"
9461

95-
if ($choice -eq 'q' -or $choice -eq 'Q') {
62+
if ($choice -eq 'q') {
9663
Write-Host "Installation cancelled."
9764
Pause
9865
exit 0
99-
} elseif ($choice -eq 'a' -or $choice -eq 'A') {
66+
} elseif ($choice -eq 'a') {
10067
if ($availableEditors.Count -le 1) {
101-
Write-Host "Error: Invalid choice. Exiting." -ForegroundColor Red
102-
Pause
103-
exit 1
68+
Fail "Invalid choice. Exiting."
10469
}
70+
$installAll = $true
10571
} else {
10672
if ($choice -notmatch '^[0-9]+$' -or [int]$choice -lt 1 -or [int]$choice -gt $availableEditors.Count) {
107-
Write-Host "Error: Invalid choice. Exiting." -ForegroundColor Red
108-
Pause
109-
exit 1
73+
Fail "Invalid choice. Exiting."
11074
}
75+
$selectedIndex = [int]$choice - 1
11176
}
11277
}
11378

114-
function Start-Spinner {
115-
param([string]$Message)
79+
function Invoke-Step {
80+
param([string]$Message, [scriptblock]$Action, [string]$ErrorMsg)
11681
Write-Progress -Activity "CodeWebChat Installation" -Status $Message
11782
Write-Host -NoNewline " $Message"
118-
return $Message
119-
}
120-
121-
function Stop-Spinner {
122-
param([string]$Message, [bool]$Success = $true)
83+
& $Action
84+
$ok = $?
12385
Write-Progress -Activity "CodeWebChat Installation" -Completed
124-
if ($Success) {
125-
Write-Host "`r$([char]0x2714) " -ForegroundColor Green -NoNewline
126-
Write-Host $Message
127-
} else {
128-
Write-Host "`r$([char]0x2718) " -ForegroundColor Red -NoNewline
129-
Write-Host $Message
130-
}
86+
if ($ok) { Write-Host "`r$([char]0x2714)" -ForegroundColor Green }
87+
else { Write-Host "`r$([char]0x2718)" -ForegroundColor Red }
88+
if (!$ok -and $ErrorMsg) { Fail $ErrorMsg }
89+
return $ok
13190
}
13291

13392
Write-Host ""
13493

94+
$originalLocation = Get-Location
95+
$tmpDir = Join-Path $env:TEMP ("CWC_Install_" + [Guid]::NewGuid().ToString().Substring(0,8))
96+
13597
try {
13698

137-
$tmpDir = Join-Path $env:TEMP ("CWC_Install_" + [Guid]::NewGuid().ToString().Substring(0,8))
13899
New-Item -ItemType Directory -Path $tmpDir | Out-Null
139100

140101
$extractedDir = Join-Path $tmpDir "source"
141102

142-
$spinner = Start-Spinner "Cloning repository..."
143-
$ErrorActionPreference = "Continue"
144-
git clone -b main -c core.longpaths=true --quiet https://github.com/robertpiosik/CodeWebChat.git $extractedDir *>$null
145-
$cmdSuccess = ($LASTEXITCODE -eq 0)
146-
$ErrorActionPreference = "Stop"
147-
Stop-Spinner $spinner $cmdSuccess
103+
$null = Invoke-Step "Cloning repository..." {
104+
git clone -b main -c core.longpaths=true --quiet https://github.com/robertpiosik/CodeWebChat.git $extractedDir *>$null
105+
} "Cloning repository failed."
148106

149-
if (!$cmdSuccess) {
150-
Write-Host "Error: Cloning repository failed." -ForegroundColor Red
151-
Pause
152-
exit 1
153-
}
107+
Set-Location $extractedDir
154108

155-
Push-Location $extractedDir
109+
$null = Invoke-Step "Installing dependencies..." {
110+
corepack.cmd pnpm install *>$null
111+
} "Failed to install dependencies."
156112

157-
$spinner = Start-Spinner "Installing dependencies..."
158-
$ErrorActionPreference = "Continue"
159-
npx.cmd --yes pnpm install *>$null
160-
$cmdSuccess = ($LASTEXITCODE -eq 0)
161-
Stop-Spinner $spinner $cmdSuccess
162-
163-
if (!$cmdSuccess) {
164-
Write-Host "Error: Failed to install dependencies." -ForegroundColor Red
165-
Pause
166-
exit 1
167-
}
168-
169-
$spinner = Start-Spinner "Building extension..."
170113
Set-Location apps/editor
171-
npx.cmd --yes pnpm run build *>$null
172-
$cmdSuccess = ($LASTEXITCODE -eq 0)
173-
$ErrorActionPreference = "Stop"
174-
Stop-Spinner $spinner $cmdSuccess
175114

176-
if (!$cmdSuccess) {
177-
Write-Host "Error: Build failed." -ForegroundColor Red
178-
Pop-Location
179-
Pause
180-
exit 1
181-
}
115+
$null = Invoke-Step "Building extension..." {
116+
corepack.cmd pnpm run build *>$null
117+
} "Build failed."
182118

183119
$vsixFile = Get-ChildItem *.vsix -ErrorAction SilentlyContinue | Select-Object -First 1
184120

185121
if (!$vsixFile) {
186-
Write-Host "Error: VSIX file was not generated." -ForegroundColor Red
187-
Pop-Location
188-
Pause
189-
exit 1
122+
Fail "VSIX file was not generated."
190123
}
191124

192125
function Install-ToEditor($idx) {
193-
$editorName = $availableEditors[$idx]
194-
$editorCmd = $editorCmds[$idx]
195-
196-
$spinner = Start-Spinner "Installing to $editorName..."
197-
$ErrorActionPreference = "Continue"
198-
& $editorCmd --install-extension $vsixFile.FullName --force *>$null
199-
$cmdSuccess = ($LASTEXITCODE -eq 0)
200-
$ErrorActionPreference = "Stop"
201-
202-
Stop-Spinner $spinner $cmdSuccess
126+
$editor = $availableEditors[$idx]
127+
$ok = Invoke-Step "Installing to $($editor.Name)..." {
128+
& $editor.Cmd --install-extension $vsixFile.FullName --force *>$null
129+
}
203130

204131
Write-Host ""
205-
if ($cmdSuccess) {
132+
if ($ok) {
206133
if ($targetScheme) {
207134
Write-Host "Updated successfully! Please restart the editor for changes to take effect."
208135
} else {
209-
Write-Host "Successfully installed to $editorName!"
136+
Write-Host "Successfully installed to $($editor.Name)!"
210137
}
211138
} else {
212-
Write-Host "Error: Failed to install to $editorName." -ForegroundColor Red
139+
Write-Host "Error: Failed to install to $($editor.Name)." -ForegroundColor Red
213140
}
214141
}
215142

216-
if ($choice -eq 'a' -or $choice -eq 'A') {
217-
for ($i = 0; $i -lt $availableEditors.Count; $i++) {
218-
Install-ToEditor $i
143+
$indices = if ($installAll) { 0..($availableEditors.Count - 1) } else { $selectedIndex }
144+
foreach ($i in $indices) { Install-ToEditor $i }
145+
146+
Set-Location $originalLocation
147+
148+
Write-Host ""
149+
$null = Invoke-Step "Cleaning up..." {
150+
if (Test-Path $tmpDir) {
151+
Remove-Item -Path $tmpDir -Recurse -Force -ErrorAction SilentlyContinue
219152
}
220-
} else {
221-
Install-ToEditor ([int]$choice - 1)
222-
}
153+
} "Failed to clean up."
223154

224155
} finally {
225-
Write-Progress -Activity "CodeWebChat Installation" -Completed
156+
Set-Location $originalLocation -ErrorAction SilentlyContinue
157+
if (Test-Path $tmpDir) {
158+
Remove-Item -Path $tmpDir -Recurse -Force -ErrorAction SilentlyContinue
159+
}
226160
}
227161

228-
Pop-Location
229-
Remove-Item -Path $tmpDir -Recurse -Force -ErrorAction SilentlyContinue
230162
Pause

0 commit comments

Comments
 (0)