Skip to content

Instantly share code, notes, and snippets.

@amgdy
Last active June 11, 2023 18:11
Show Gist options
  • Save amgdy/4906fb1d7abb5ddd86f1feca3c436438 to your computer and use it in GitHub Desktop.
Save amgdy/4906fb1d7abb5ddd86f1feca3c436438 to your computer and use it in GitHub Desktop.
Publish NPM packages to local registry and modify the tarball if it has a custom registry like wombat-dressing-room.appspot.com
# Set the path to the folder containing tarball files
$folderPath = "path/to/folder"
# Get the list of tarball files with .tgz extension in the folder
$tarballFiles = Get-ChildItem -Path $folderPath -File -Recurse -Filter "*.tgz" | Where-Object { !$_.PSIsContainer }
foreach ($tarballFile in $tarballFiles) {
# Get the directory and base name of the tarball file
$tarballDirectory = Split-Path -Parent $tarballFile.FullName
$tarballBaseName = [System.IO.Path]::GetFileNameWithoutExtension($tarballFile.FullName)
# Set the path for the temporary folder
$tempFolderPath = Join-Path -Path $tarballDirectory -ChildPath "temp"
# Create the temporary folder for extracting the tarball contents
New-Item -ItemType Directory -Path $tempFolderPath | Out-Null
# Flag to track if modifications were made
$modificationsMade = $false
try {
# Extract the tarball contents to the temporary folder
tar -xf $tarballFile.FullName -C $tempFolderPath
# Find all package.json files inside the extracted folder
$packageJsonFiles = Get-ChildItem -Path $tempFolderPath -Recurse -Filter "package.json" | Where-Object { !$_.PSIsContainer }
# Iterate over each package.json file
foreach ($packageJsonFile in $packageJsonFiles) {
# Load the package.json file as JSON
$packageJson = Get-Content $packageJsonFile.FullName | ConvertFrom-Json
# Check if 'publishConfig' exists
if ($packageJson.publishConfig) {
# Check if 'registry' exists inside 'publishConfig'
if ($packageJson.publishConfig.registry) {
# Remove the 'registry' key from 'publishConfig'
$packageJson.publishConfig.PSObject.Properties.Remove('registry')
# Convert the modified package.json back to a string
$modifiedPackageJson = $packageJson | ConvertTo-Json -Depth 10
# Save the modified package.json file
$modifiedPackageJson | Set-Content -Path $packageJsonFile.FullName
# Set the modifications flag to true
$modificationsMade = $true
}
}
}
if ($modificationsMade) {
# Set the path for the modified tarball file
$modifiedTarballFilePath = Join-Path -Path $tarballDirectory -ChildPath "$tarballBaseName.modified.tgz"
# Create a new tarball with the modified package.json files
tar -czf $modifiedTarballFilePath -C $tempFolderPath .
# Replace the original tarball file with the modified one
Rename-Item -Path $tarballFile.FullName -NewName ($tarballFile.Name + ".bak")
Move-Item -Path $modifiedTarballFilePath -Destination $tarballFile.FullName
# Output the path to the modified tarball file
Write-Output "Modified tarball file: $tarballFile"
# Set the tarball file path to the modified one
$tarballFile = Get-Item $tarballFile.FullName
}
else {
Write-Output "No modifications were made to the package.json in $($tarballFile.Name)."
}
}
finally {
# Cleanup: Remove the temporary folder
Remove-Item -Path $tempFolderPath -Recurse -Force
}
# Publish the tarball using npm
Write-Output "Publishing $tarballFile..."
npm publish $tarballFile.FullName
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment