Last active
August 10, 2025 12:30
-
-
Save avestura/f4a541ddb80203625f8ef5f9d67ee606 to your computer and use it in GitHub Desktop.
Finds all yarn.lock files in a git repo and publish them into the target registry
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| $failedLogFile = "failed-publishes.txt" | |
| New-Item -ItemType File -Path $failedLogFile -Force | Out-Null | |
| $uniquePackages = [System.Collections.Generic.HashSet[string]]::new() | |
| git ls-files *yarn.lock | ForEach-Object { | |
| $yarnfile = $_.ToString() | |
| Write-Host "📦 scanning $yarnfile" | |
| $filePath = Join-Path (Get-Location) $yarnfile | |
| $fileContent = [System.IO.File]::ReadAllText($filePath) | |
| $matches = ([regex]'resolved "(.*)"').Matches($fileContent) | |
| foreach ($match in $matches) { | |
| $pkg = $match.Groups[1].Value | |
| $uniquePackages.Add($pkg) | Out-Null | |
| } | |
| } | |
| foreach ($pkg in $uniquePackages) { | |
| Write-Host "⬆️ checking & publishing '$pkg'" | |
| $targetUrl = $pkg ` | |
| -replace '^http://nexus1:8081/repository/repo1/', 'http://nexus2/repository/repo1/' ` | |
| -replace '^http://nexus1:8081/repository/repo2/', 'http://nexus2/repository/repo2/' | |
| try { | |
| $response = Invoke-WebRequest -Uri $targetUrl -Method Head -UseBasicParsing -ErrorAction SilentlyContinue | |
| write-host "status: " $response.StatusCode | |
| if ($response.StatusCode -eq 200) { | |
| Write-Host "✅ Already exists: $targetUrl" | |
| continue | |
| } | |
| } catch { | |
| write-host "error in request: " $_ | |
| Write-Host "📦 Publishing new package: $pkg" | |
| npm publish $pkg | |
| if ($LASTEXITCODE -ne 0) { | |
| Write-Warning "❌ Failed to publish '$pkg'" | |
| $pkg | Out-File -FilePath $failedLogFile -Append -Encoding utf8 | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment