Skip to content

Commit dfbf259

Browse files
committed
Improve npm registry reachability check in script
Replaces Invoke-WebRequest with a custom Test-UrlReachable function using WebClient for checking registry availability. Adds error message output when the adsk npm registry is not reachable for better troubleshooting.
1 parent 7e7bbe4 commit dfbf259

File tree

1 file changed

+19
-4
lines changed

1 file changed

+19
-4
lines changed

src/setnpmreg.ps1

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,25 @@ function createNpmrcFile {
1212
New-Item -Path . -Name .npmrc -ItemType File -Value "registry=$registry`n" -Force
1313
}
1414

15+
function Test-UrlReachable {
16+
param ([string]$url)
17+
try {
18+
$webClient = New-Object System.Net.WebClient
19+
$webClient.DownloadString($url) | Out-Null
20+
return $true
21+
}
22+
catch {
23+
return $false
24+
}
25+
finally {
26+
if ($webClient) { $webClient.Dispose() }
27+
}
28+
}
29+
1530
try {
1631
Write-Host "Checking if adsk npm registry is reachable..." -ForegroundColor Blue
17-
$response = Invoke-WebRequest -Uri $adskNpmRegistry -TimeoutSec 20 -ErrorAction Stop
18-
19-
if ($response.StatusCode -eq 200) {
32+
33+
if (Test-UrlReachable -url $adskNpmRegistry) {
2034
Write-Host "adsk npm registry is reachable" -ForegroundColor Green
2135
createNpmrcFile -registry $adskNpmRegistry
2236
Write-Output "//npm.autodesk.com/artifactory/api/npm/:_authToken=`${NPM_TOKEN}" | Out-File -FilePath .npmrc -Encoding UTF8 -Append
@@ -28,5 +42,6 @@ try {
2842
}
2943
catch {
3044
Write-Host "adsk npm registry is not reachable" -ForegroundColor Red
45+
Write-Host "Error: $($_.Exception.Message)" -ForegroundColor Yellow
3146
createNpmrcFile -registry $npmRegistry
32-
}
47+
}

0 commit comments

Comments
 (0)