Skip to content

Commit 66ba7af

Browse files
committed
Implement comprehensive error logging and automatic log viewing on failure
- remove logging - On error, log into `~/.cwc/error.log` and open it so user can see what's wrong in detail.
1 parent f4839c3 commit 66ba7af

2 files changed

Lines changed: 155 additions & 61 deletions

File tree

‎install.ps1‎

Lines changed: 72 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -356,8 +356,38 @@ switch -Regex ($lang) {
356356
}
357357
}
358358

359+
$cwcDir = Join-Path $env:USERPROFILE ".cwc"
360+
$errorLog = Join-Path $cwcDir "error.log"
361+
$tempLog = Join-Path ([System.IO.Path]::GetTempPath()) "cwc-install-$PID.log"
362+
363+
function Write-ErrorLog {
364+
param([string]$Text)
365+
if ($Text) {
366+
Add-Content -Path $tempLog -Value $Text -Encoding UTF8 -ErrorAction SilentlyContinue
367+
}
368+
}
369+
370+
function Open-ErrorLog {
371+
if (!(Test-Path $cwcDir)) {
372+
New-Item -ItemType Directory -Path $cwcDir -Force -ErrorAction SilentlyContinue | Out-Null
373+
}
374+
if (Test-Path $tempLog) {
375+
Move-Item -Path $tempLog -Destination $errorLog -Force -ErrorAction SilentlyContinue
376+
}
377+
if (Test-Path $errorLog) {
378+
Write-Host "Log: $errorLog" -ForegroundColor Yellow
379+
try {
380+
Start-Process -FilePath $errorLog -ErrorAction Stop
381+
} catch {
382+
Start-Process -FilePath "notepad.exe" -ArgumentList "`"$errorLog`"" -ErrorAction SilentlyContinue
383+
}
384+
}
385+
}
386+
359387
function Fail($Message) {
360388
Write-Host "$msg_error $Message" -ForegroundColor Red
389+
Write-ErrorLog "`n$msg_error $Message"
390+
Open-ErrorLog
361391
Pause
362392
exit 1
363393
}
@@ -373,12 +403,14 @@ if (!$nodeIsGood) {
373403
$ErrorActionPreference = "Continue"
374404
try {
375405
if (Get-Command fnm -ErrorAction SilentlyContinue) {
406+
Write-ErrorLog "`n=== fnm ==="
376407
fnm env --shell powershell | Out-String | Invoke-Expression
377-
fnm install 22 *>$null
378-
fnm use 22 *>$null
408+
fnm install 22 *>&1 | ForEach-Object { Write-ErrorLog ($_ | Out-String).TrimEnd("`r", "`n") }
409+
fnm use 22 *>&1 | ForEach-Object { Write-ErrorLog ($_ | Out-String).TrimEnd("`r", "`n") }
379410
} elseif (Get-Command nvm -ErrorAction SilentlyContinue) {
380-
nvm install 22 *>$null
381-
nvm use 22 *>$null
411+
Write-ErrorLog "`n=== nvm ==="
412+
nvm install 22 *>&1 | ForEach-Object { Write-ErrorLog ($_ | Out-String).TrimEnd("`r", "`n") }
413+
nvm use 22 *>&1 | ForEach-Object { Write-ErrorLog ($_ | Out-String).TrimEnd("`r", "`n") }
382414
} else {
383415
Fail $msg_node_required
384416
}
@@ -417,6 +449,9 @@ if ($targetScheme -and $idx -ge 0) {
417449

418450
if ($choice -eq 'q') {
419451
Write-Host $msg_cancelled
452+
if (Test-Path $tempLog) {
453+
Remove-Item -Path $tempLog -Force -ErrorAction SilentlyContinue
454+
}
420455
Pause
421456
exit 0
422457
} else {
@@ -430,14 +465,23 @@ if ($targetScheme -and $idx -ge 0) {
430465
function Invoke-Step {
431466
param([string]$Message, [scriptblock]$Action, [string]$ErrorMsg)
432467
Write-Host -NoNewline " $Message"
468+
Write-ErrorLog "`n=== $Message ==="
433469

434470
$prevPreference = $ErrorActionPreference
435471
$ErrorActionPreference = "Continue"
436472
$ok = $true
473+
$stepState = @{ Failed = $false }
437474
try {
438-
& $Action
439-
if (-not $?) { $ok = $false }
475+
& {
476+
$global:LASTEXITCODE = 0
477+
& $Action
478+
if (-not $?) { $stepState.Failed = $true }
479+
} *>&1 | ForEach-Object {
480+
Write-ErrorLog ($_ | Out-String).TrimEnd("`r", "`n")
481+
}
482+
if ($stepState.Failed) { $ok = $false }
440483
} catch {
484+
Write-ErrorLog ($_ | Out-String)
441485
$ok = $false
442486
} finally {
443487
$ErrorActionPreference = $prevPreference
@@ -453,7 +497,8 @@ Write-Host ""
453497

454498
if (!(Get-Command pnpm -ErrorAction SilentlyContinue)) {
455499
if (Get-Command npm -ErrorAction SilentlyContinue) {
456-
npm install -g pnpm *>$null
500+
Write-ErrorLog "`n=== Installing pnpm ==="
501+
npm install -g pnpm *>&1 | ForEach-Object { Write-ErrorLog ($_ | Out-String).TrimEnd("`r", "`n") }
457502
if (!(Get-Command pnpm -ErrorAction SilentlyContinue)) {
458503
$npmGlobalBin = npm prefix -g 2>$null
459504
if ($npmGlobalBin) {
@@ -465,21 +510,19 @@ if (!(Get-Command pnpm -ErrorAction SilentlyContinue)) {
465510

466511
$originalLocation = Get-Location
467512

468-
$cwcDir = Join-Path $env:USERPROFILE ".cwc"
469-
470513
function Clone-Repo {
471514
$cloneArgs = @("clone", "-c", "core.longpaths=true", "--quiet", "--depth", "1")
472515
if ($tagName) {
473516
$cloneArgs += @("--branch", $tagName)
474517
}
475518
$cloneArgs += @("https://github.com/CodeWebChat/CWC.git", $repoDir)
476519

477-
git @cloneArgs *>$null
520+
git @cloneArgs
478521
if ($LASTEXITCODE -ne 0) {
479522
if (Test-Path $repoDir) {
480523
Remove-Item -Path $repoDir -Recurse -Force -ErrorAction SilentlyContinue
481524
}
482-
git @cloneArgs *>$null
525+
git @cloneArgs
483526
if ($LASTEXITCODE -ne 0) { throw "Failed to clone" }
484527
}
485528
}
@@ -497,17 +540,17 @@ $updated = $false
497540
if (Test-Path (Join-Path $repoDir ".git")) {
498541
$updated = Invoke-Step "$msg_step_cloning..." {
499542
if ($tagName) {
500-
git -C $repoDir fetch --depth 1 origin "+refs/tags/${tagName}:refs/tags/${tagName}" --quiet *>$null
543+
git -C $repoDir fetch --depth 1 origin "+refs/tags/${tagName}:refs/tags/${tagName}" --quiet
501544
if ($LASTEXITCODE -ne 0) { throw "Failed to fetch" }
502-
git -C $repoDir checkout --force $tagName --quiet *>$null
545+
git -C $repoDir checkout --force $tagName --quiet
503546
if ($LASTEXITCODE -ne 0) { throw "Failed to checkout" }
504547
} else {
505-
git -C $repoDir fetch --depth 1 origin main --quiet *>$null
548+
git -C $repoDir fetch --depth 1 origin main --quiet
506549
if ($LASTEXITCODE -ne 0) { throw "Failed to fetch" }
507-
git -C $repoDir reset --hard FETCH_HEAD --quiet *>$null
550+
git -C $repoDir reset --hard FETCH_HEAD --quiet
508551
if ($LASTEXITCODE -ne 0) { throw "Failed to reset" }
509552
}
510-
git -C $repoDir clean -fd --quiet *>$null
553+
git -C $repoDir clean -fd --quiet
511554
if ($LASTEXITCODE -ne 0) { throw "Failed to clean" }
512555
} $msg_step_cloning
513556
}
@@ -524,7 +567,7 @@ if (-not $updated) {
524567
Set-Location $repoDir
525568

526569
$depsOk = Invoke-Step "$msg_step_deps..." {
527-
pnpm --filter gemini-coder --filter browser install *>$null
570+
pnpm --filter gemini-coder --filter browser install
528571
if ($LASTEXITCODE -ne 0) { throw "pnpm install failed" }
529572
}
530573

@@ -536,7 +579,7 @@ if (-not $depsOk) {
536579
$prevPreference = $ErrorActionPreference
537580
$ErrorActionPreference = "Continue"
538581
try {
539-
pnpm store prune *>$null
582+
pnpm store prune *>&1 | ForEach-Object { Write-ErrorLog ($_ | Out-String).TrimEnd("`r", "`n") }
540583
} catch {
541584
# Ignore
542585
}
@@ -549,17 +592,17 @@ if (-not $depsOk) {
549592
Set-Location $repoDir
550593

551594
$null = Invoke-Step "$msg_step_deps..." {
552-
pnpm --filter gemini-coder --filter browser install *>$null
595+
pnpm --filter gemini-coder --filter browser install
553596
if ($LASTEXITCODE -ne 0) { throw "pnpm install failed" }
554597
} $msg_step_deps
555598
}
556599

557600
Remove-Item "apps/editor/*.vsix" -Force -ErrorAction SilentlyContinue
558601

559602
$null = Invoke-Step "$msg_step_build..." {
560-
pnpm --filter gemini-coder --filter browser build *>$null
603+
pnpm --filter gemini-coder --filter browser build
561604
if ($LASTEXITCODE -ne 0) { throw "pnpm build failed" }
562-
node -e "const fs = require('fs'); const version = JSON.parse(fs.readFileSync('apps/editor/package.json', 'utf8')).version; const manifestPath = 'apps/browser/out/manifest.json'; const manifest = JSON.parse(fs.readFileSync(manifestPath, 'utf8')); manifest.version = version; fs.writeFileSync(manifestPath, JSON.stringify(manifest, null, 2) + '\n');" *>$null
605+
node -e "const fs = require('fs'); const version = JSON.parse(fs.readFileSync('apps/editor/package.json', 'utf8')).version; const manifestPath = 'apps/browser/out/manifest.json'; const manifest = JSON.parse(fs.readFileSync(manifestPath, 'utf8')); manifest.version = version; fs.writeFileSync(manifestPath, JSON.stringify(manifest, null, 2) + '\n');"
563606
if ($LASTEXITCODE -ne 0) { throw "Failed to update browser manifest version" }
564607
} $msg_step_build
565608

@@ -572,7 +615,7 @@ if (!$vsixFile) {
572615
}
573616

574617
$ok = Invoke-Step "$msg_step_install $($editor.Name)..." {
575-
& $editor.Cmd --install-extension $vsixFile.FullName --force *>$null
618+
& $editor.Cmd --install-extension $vsixFile.FullName --force
576619
if ($LASTEXITCODE -ne 0) { throw "Extension install failed" }
577620
}
578621

@@ -581,11 +624,17 @@ if ($ok) {
581624
Write-Host "$msg_success $($editor.Name)!"
582625
}
583626
} else {
584-
Write-Host "$msg_error $msg_install_fail $($editor.Name)." -ForegroundColor Red
627+
Fail "$msg_install_fail $($editor.Name)."
585628
}
586629

630+
} catch {
631+
Write-ErrorLog ($_ | Out-String)
632+
Fail $_.Exception.Message
587633
} finally {
588634
Set-Location $originalLocation -ErrorAction SilentlyContinue
635+
if (Test-Path $tempLog) {
636+
Remove-Item -Path $tempLog -Force -ErrorAction SilentlyContinue
637+
}
589638
}
590639

591640
if (-not $updated) {

0 commit comments

Comments
 (0)