diff --git a/extensions/copilot/src/extension/chatSessions/vscode-node/copilotCLIShim.ps1 b/extensions/copilot/src/extension/chatSessions/vscode-node/copilotCLIShim.ps1 index dd0a9f68e25..d109294c208 100644 --- a/extensions/copilot/src/extension/chatSessions/vscode-node/copilotCLIShim.ps1 +++ b/extensions/copilot/src/extension/chatSessions/vscode-node/copilotCLIShim.ps1 @@ -14,6 +14,23 @@ # Minimum required Copilot CLI version $RequiredVersion = "0.0.342" +$PackageName = "@github/copilot" + +function Invoke-NpmGlobalCommand { + param( + [Parameter(Mandatory = $true)][ValidateSet('install', 'update')][string]$Command, + [Parameter(Mandatory = $true)][string]$Package + ) + + $npmArgs = @($Command, '-g', $Package) + + $npmCmd = Get-Command npm.cmd -ErrorAction SilentlyContinue + if ($npmCmd) { + & npm.cmd @npmArgs + } else { + & npm @npmArgs + } +} function Find-RealCopilot { # Find the real copilot binary, avoiding this script if it's in PATH @@ -73,7 +90,7 @@ function Test-AndLaunchCopilot { $answer = Read-Host "Install GitHub Copilot CLI? (y/N)" if ($answer -eq "y" -or $answer -eq "Y") { try { - & npm install -g @github/copilot + Invoke-NpmGlobalCommand -Command 'install' -Package $PackageName if ($LASTEXITCODE -eq 0) { Test-AndLaunchCopilot $Arguments return @@ -98,7 +115,7 @@ function Test-AndLaunchCopilot { if ($answer -eq "y" -or $answer -eq "Y") { Write-Host "Reinstalling GitHub Copilot CLI..." try { - & npm install -g @github/copilot + Invoke-NpmGlobalCommand -Command 'install' -Package $PackageName if ($LASTEXITCODE -eq 0) { Test-AndLaunchCopilot $Arguments return @@ -125,7 +142,7 @@ function Test-AndLaunchCopilot { $answer = Read-Host "Would you like to reinstall GitHub Copilot CLI? (y/N)" if ($answer -eq "y" -or $answer -eq "Y") { try { - & npm install -g @github/copilot + Invoke-NpmGlobalCommand -Command 'install' -Package $PackageName if ($LASTEXITCODE -eq 0) { Test-AndLaunchCopilot $Arguments return @@ -159,7 +176,7 @@ function Test-AndLaunchCopilot { $answer = Read-Host "Reinstall GitHub Copilot CLI? (y/N)" if ($answer -eq "y" -or $answer -eq "Y") { try { - & npm install -g @github/copilot + Invoke-NpmGlobalCommand -Command 'install' -Package $PackageName if ($LASTEXITCODE -eq 0) { Test-AndLaunchCopilot $Arguments return @@ -182,7 +199,7 @@ function Test-AndLaunchCopilot { $answer = Read-Host "Update GitHub Copilot CLI? (y/N)" if ($answer -eq "y" -or $answer -eq "Y") { try { - & npm update -g @github/copilot + Invoke-NpmGlobalCommand -Command 'update' -Package $PackageName if ($LASTEXITCODE -eq 0) { Test-AndLaunchCopilot $Arguments return