Last active
November 15, 2024 21:57
-
-
Save johnnyshankman/758eb0aaa073bce596785d44356bb446 to your computer and use it in GitHub Desktop.
This file contains 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
#!/bin/bash | |
# Input parameters | |
folders=$1 # Comma-separated list of folders | |
packages=$2 # Comma-separated list of npm package names | |
versions=$3 # Comma-separated list of version strings | |
# Check if the user is logged into GitHub CLI | |
if ! gh auth status > /dev/null 2>&1; then | |
echo "You are not logged into GitHub CLI. Please run 'gh auth login' to authenticate then try again." | |
exit 1 | |
fi | |
if [ -z "$folders" ] || [ -z "$packages" ] || [ -z "$versions" ]; then | |
echo "Usage: $0 <folders> <packages> <versions>" | |
exit 1 | |
fi | |
# Convert comma-separated lists into arrays | |
IFS=',' read -r -a folder_array <<< "$folders" | |
IFS=',' read -r -a package_array <<< "$packages" | |
IFS=',' read -r -a version_array <<< "$versions" | |
# Check if the number of packages matches the number of versions | |
if [ "${#package_array[@]}" -ne "${#version_array[@]}" ]; then | |
echo "The number of packages must match the number of version strings." | |
exit 1 | |
fi | |
for folder in "${folder_array[@]}"; do | |
if [ -d "$folder" ]; then | |
cd "$folder" || { echo "Failed to change directory to $folder"; exit 1; } | |
if [ -f "package.json" ]; then | |
echo "Processing $folder/package.json" | |
# Blow out any unstaged changes | |
git reset | |
# Checkout main branch | |
git checkout main | |
# Fetch latest changes | |
git fetch --all | |
# Reset to the latest commit | |
git reset --hard origin/main | |
# Pull latest changes | |
git pull | |
# Initialize strings to accumulate package updates | |
update_summary="" | |
branch_name="" | |
for i in "${!package_array[@]}"; do | |
package_name="${package_array[i]}" | |
version_string="${version_array[i]}" | |
# Sanitize the package name and version string for branch name use | |
sanitized_package_name=$(echo "$package_name" | sed 's/[@\/]/-/g') | |
sanitized_version_string=$(echo "$version_string" | sed 's/[\^~]//g') | |
# Check if the package exists in package.json and update or add it | |
if jq -e ".dependencies[\"$package_name\"]" package.json > /dev/null; then | |
# Update the package version | |
jq ".dependencies[\"$package_name\"]=\"$version_string\"" package.json > tmp.$$.json && mv tmp.$$.json package.json | |
else | |
# Add the package if it doesn't exist | |
jq ".dependencies += {\"$package_name\": \"$version_string\"}" package.json > tmp.$$.json && mv tmp.$$.json package.json | |
fi | |
# Install updated packages | |
npm install | |
# Accumulate package updates | |
update_summary+="$package_name to $version_string, " | |
branch_name+="update-js-${sanitized_package_name}-${sanitized_version_string}-" | |
done | |
# Trim trailing comma and space | |
update_summary=${update_summary%, } | |
branch_name=${branch_name%-} | |
# Create a new git branch | |
git checkout -b "$branch_name" | |
# Add changes to git | |
git add package.json package-lock.json | |
# Commit changes | |
commit_message="Update $update_summary" | |
git commit -m "$commit_message" | |
# Push branch to remote | |
git push origin HEAD | |
# Create a Pull Request | |
gh pr create --title "$commit_message" --body "This PR updates the following packages: $update_summary." | |
# Switch back to main branch | |
git checkout main | |
else | |
echo "package.json not found in $folder" | |
fi | |
cd - > /dev/null || { echo "Failed to change directory back"; exit 1; } | |
else | |
echo "Folder $folder does not exist" | |
fi | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
updated so that it requires github cli and auto-opens the PR for you as I was wasting a shit ton of time doing that.