Skip to content

Instantly share code, notes, and snippets.

@johnnyshankman
Last active November 15, 2024 21:57
Show Gist options
  • Save johnnyshankman/758eb0aaa073bce596785d44356bb446 to your computer and use it in GitHub Desktop.
Save johnnyshankman/758eb0aaa073bce596785d44356bb446 to your computer and use it in GitHub Desktop.
#!/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
@johnnyshankman
Copy link
Author

johnnyshankman commented Jul 4, 2024

Manifold Example

./update-npm-pkgs.sh \
  "studio-app-rich-text-minting,studio-app-claim-v2,studio-app-burn-redeem-v2,studio-app-physicals-v2,studio-app-gachapon,studio-app-marketplace-v2,studio-app-curate-page,studio-app-thanks-page,studio-app-frame-edition,studio-app-batch-multi,studio-app-batch-edition,studio-app-edition,studio-app-one-of-one,studio-app-giveaways" \
  "@manifoldxyz/studio-app-sdk-react" \
  "^5.1.10"
  • studio-app-rich-text-minting
  • studio-app-claim-v2
  • studio-app-burn-redeem-v2
  • studio-app-physicals-v2
  • studio-app-gachapon
  • studio-app-marketplace-v2
  • studio-app-curate-page
  • studio-app-thanks-page
  • studio-app-frame-edition
  • studio-app-batch-multi
  • studio-app-batch-edition
  • studio-app-edition
  • studio-app-one-of-one
  • studio-app-giveaways

@donpdang
Copy link

donpdang commented Aug 1, 2024

Changed up the code here so it works for multiple packages and versions as well

#!/bin/bash

# Input parameters
folders=$1         # Comma-separated list of folders
package_names=$2    # Comma-separated list of npm package names
version_strings=$3  # Comma-separated list of Version strings, corresponding to the package names

if [ -z "$folders" ] || [ -z "$package_names" ] || [ -z "$version_strings" ]; then
  echo "Usage: $0 <folders> <package_names> <version_strings>"
  exit 1
fi

# Sanitize the packages name for branch name use
sanitized_package_name=$(echo "$package_names" | sed 's/[@\/]/-/g' | sed 's/,/-/g')

# Sanitize the versions string for branch name use by removing '^' and '~'
sanitized_version_string=$(echo "$version_strings" | sed 's/[\^~]//g' | sed 's/,/-/g')

# Convert comma-separated list into an array of packages
IFS=',' read -r -a folder_array <<< "$folders"

# Convert comma-separated list of packages name into an array of package names
IFS=',' read -r -a package_name_array <<< "$package_names"

# Convert comma-separated list of versions into an array of versions
IFS=',' read -r -a version_array <<< "$version_strings"

# Check if package_name_array and version_array have the same length
if [ ${#package_name_array[@]} -ne ${#version_array[@]} ]; then
    echo "Error: The number of package names and version strings must be the same."
    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
      # Checkout main branch
      git checkout main

      # Pull latest changes
      git pull

      # Create a new git branch
      branch_name="update-${sanitized_package_name}-${sanitized_version_string}"
      git checkout -b "$branch_name"

      for i in "${!package_name_array[@]}"; do
        package_name="${package_name_array[i]}"
        version_string="${version_array[i]}"
        echo "Processing $folder/package.json"

        # Update the package version in package.json
        jq ".dependencies[\"$package_name\"]=\"$version_string\"" package.json > tmp.$$.json && mv tmp.$$.json package.json

        # Install updated packages
        npm install

        # Add changes to git
        git add package.json package-lock.json

        # Commit changes
        git commit -m "Update $package_name to $version_string"
      done
      # Push branch to remote
      git push origin "$branch_name"

      # 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

@donpdang
Copy link

donpdang commented Aug 1, 2024

Example:
./update-npm-pkgs.sh \ studio-app-batch-mint,studio-app-burn-redeem-v2,studio-app-physicals-v2,studio-app-gachapon,studio-app-marketplace-v2,studio-app-curate-page,studio-app-thanks-page,studio-app-frame-edition,studio-app-batch-mint,studio-app-batch-edition,studio-app-single-mint,studio-app-claim-v2 @manifoldxyz/studio-app-sdk-react,@manifoldxyz/studio-app-sdk ^7.2.1,^6.2.0

@johnnyshankman
Copy link
Author

oh niiiiiice so that you can update two packages at once in all repos? @donpdang smart. i like it. will update gist shortly.

@johnnyshankman
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment