Skip to content

Instantly share code, notes, and snippets.

@avestura
Last active August 10, 2025 12:30
Show Gist options
  • Select an option

  • Save avestura/f4a541ddb80203625f8ef5f9d67ee606 to your computer and use it in GitHub Desktop.

Select an option

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
$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