Update npm/jsDelivr shields in root README #17
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
name: Update npm/jsDelivr shields in root README | |
on: | |
schedule: | |
- cron: "45 3 * * 5" # every Fri @ 3:45 AM | |
permissions: | |
contents: read | |
jobs: | |
update-root-npm-jsd-shields: | |
runs-on: ubuntu-latest | |
env: | |
TZ: PST8PDT | |
steps: | |
- name: Checkout adamlui/js-utils | |
uses: actions/checkout@v4 | |
with: | |
token: ${{ secrets.REPO_SYNC_PAT }} | |
repository: adamlui/js-utils | |
path: adamlui/js-utils | |
- name: Fetch/sum npm download + git clone + JSD request counts | |
run: | | |
NPM_PKGS=("@adamlui/geolocate" "@adamlui/minify.js" "@adamlui/scss-to-css" | |
"generate-ip" "generate-pw" "gulp-minify.js") | |
JSD_NPM_PKGS=("@adamlui/geolocate" "generate-ip" "generate-pw") | |
JSD_GH_REPOS=("adamlui/js-utils" "adamlui/minify.js" "adamlui/scss-to-css") | |
expand_num() { # expand nums abbreviated w/ 'k' or 'm' suffix to integers | |
local num=$(echo "$1" | tr '[:upper:]' '[:lower:]') # convert to lowercase | |
if [[ $num =~ k$ ]] ; then | |
num="${num%k}" # remove 'k' suffix | |
num=$(awk "BEGIN { printf \"%.0f\", $num * 1000 }") # multiply by 1000 | |
elif [[ $num =~ m$ ]] ; then | |
num="${num%m}" # remove 'm' suffix | |
num=$(awk "BEGIN { printf \"%.0f\", $num * 1000000 }") # multiply by 1000000 | |
fi ; echo "$num" | |
} | |
format_total() { | |
local num=$1 ; first_digit="${num:0:1}" second_digit="${num:1:1}" | |
second_digit_rounded=$(( second_digit < 5 ? 0 : 5 )) | |
if (( num >= 1000000000 )) ; then # 1B+ w/ one decimal place | |
formatted_num="$(( num / 1000000000 ))" | |
remainder=$(( (num % 1000000000) / 100000000 )) | |
if (( remainder != 0 )) ; then formatted_num+=".$remainder" ; fi | |
formatted_num+="B+" | |
elif (( num >= 10000000 )) ; then # abbr 10,000,000+ to 999,000,000+ | |
formatted_num=$(printf "%'.f+" $((( num / 1000000 ) * 1000000 ))) | |
elif (( num >= 1000000 )) ; then # abbr 1,000,000+ to 9,500,000+ | |
formatted_num="${first_digit},${second_digit}00,000+" | |
elif (( num >= 100000 )) ; then # abbr 100,000+ to 950,000+ | |
formatted_num="${first_digit}${second_digit_rounded}0,000+" | |
elif (( num >= 10000 )) ; then # abbr 10,000+ to 90,000+ | |
formatted_num="${first_digit}0,000+" | |
elif (( num >= 1000 )) ; then # abbr 1K to 9.9K | |
formatted_num="$(( num / 1000 ))" | |
remainder=$(( (num % 1000) / 100 )) | |
if (( remainder != 0 )) ; then formatted_num+=".$remainder" ; fi | |
formatted_num+="K" | |
else formatted_num="$num" ; fi # preserve <1K as is | |
echo "$formatted_num" | |
} | |
# Fetch/sum npm download counts | |
for pkg in "${NPM_PKGS[@]}" ; do | |
pkg_downloads=$(curl -s "https://img.shields.io/npm/dm/$pkg.svg" | | |
sed -n 's/.*<title>downloads: \([0-9,.km]\+\).*<\/title>.*/\1/Ip') | |
pkg_downloads=$(expand_num "$pkg_downloads") | |
echo "$pkg npm downloads: $pkg_downloads" | |
total_downloads=$((total_downloads + pkg_downloads)) | |
done ; echo -e "\n-----\nTotal monthly npm downloads: $total_downloads\n-----\n" | |
# Fetch/sum jsDelivr request counts for npm pkgs | |
for pkg in "${JSD_NPM_PKGS[@]}" ; do | |
pkg_requests=$(curl -s "https://img.shields.io/jsdelivr/npm/hm/$pkg.svg" | | |
sed -n -E 's|.*<title>jsdelivr: ([0-9,.km]+).*</title>.*|\1|Ip') | |
pkg_requests=$(expand_num "$pkg_requests") | |
echo "$pkg jsDelivr (npm) hits: $pkg_requests" | |
total_pkg_requests=$((total_pkg_requests + pkg_requests)) | |
done ; echo -e "\n-----\nTotal monthly jsDelivr (npm) requests: $total_pkg_requests\n-----\n" | |
# Fetch/sum jsDelivr request counts for GH repos | |
for repo in "${JSD_GH_REPOS[@]}" ; do | |
repo_requests=$(curl -s "https://img.shields.io/jsdelivr/gh/hm/$repo.svg" | | |
sed -n -E 's|.*<title>jsdelivr: ([0-9,.km]+).*</title>.*|\1|Ip') | |
repo_requests=$(expand_num "$repo_requests") | |
echo "$repo jsDelivr (GH) hits: $repo_requests" | |
total_repo_requests=$((total_repo_requests + repo_requests)) | |
done ; echo -e "\n-----\nTotal monthly jsDelivr (GH) requests: $total_repo_requests\n-----\n" | |
# Format totals | |
formatted_total_downloads=$(format_total "$total_downloads") | |
echo "Formatted total monthly npm downloads: $formatted_total_downloads" | |
formatted_total_requests=$(format_total $((total_pkg_requests + total_repo_requests))) | |
echo "Formatted total monthly jsDelivr requests: $formatted_total_requests" | |
# Expose totals for update step next | |
echo "TOTAL_DOWNLOADS=$formatted_total_downloads" >> $GITHUB_ENV | |
echo "TOTAL_REQUESTS=$formatted_total_requests" >> $GITHUB_ENV | |
- name: Update README shields | |
run: | | |
cd ${{ github.workspace }}/adamlui/js-utils | |
TOTAL_DOWNLOADS="${{ env.TOTAL_DOWNLOADS }}" | |
TOTAL_REQUESTS="${{ env.TOTAL_REQUESTS }}" | |
# Update npm shield | |
if [ "$TOTAL_DOWNLOADS" == "0" ] ; then echo "Error getting total npm downloads" | |
else # perform update | |
old_readme=$(<docs/README.md) | |
sed -i -E "s|(badge/Downloads-)[0-9.,km+]+|\1$TOTAL_DOWNLOADS|Ig" docs/README.md | |
new_readme=$(<docs/README.md) | |
if [ "$old_readme" != "$new_readme" ] ; then downloads_updated=true ; fi | |
if [ "$downloads_updated" = true ] ; then echo "npm shield updated to $TOTAL_DOWNLOADS" | |
else echo "npm shield already up-to-date" ; fi | |
fi | |
# Update jsDelivr shield | |
if [ "$TOTAL_REQUESTS" == "0" ] ; then echo "Error getting total jsDelivr requests" | |
else # perform update | |
old_readme=$(<docs/README.md) | |
sed -i -E "s|(badge/jsDelivr_[^-]+-)[0-9.,km+]+|\1$TOTAL_REQUESTS|Ig" docs/README.md | |
new_readme=$(<docs/README.md) | |
if [ "$old_readme" != "$new_readme" ] ; then requests_updated=true ; fi | |
if [ "$requests_updated" = true ] ; then echo "jsDelivr shield updated to $TOTAL_REQUESTS" | |
else echo "jsDelivr shield already up-to-date" ; fi | |
fi | |
# Count shield types updated for commit msg | |
shield_types_updated=0 | |
[ "$downloads_updated" = true ] && ((shield_types_updated+=1)) | |
[ "$requests_updated" = true ] && ((shield_types_updated+=1)) | |
if (( "$shield_types_updated" > 1 )) ; then multi_shield_types_updated=true ; fi | |
# Define commit msg for push step next | |
commit_msg="Updated " | |
[ "$downloads_updated" = true ] && commit_msg+="npm" | |
if [ "$requests_updated" = true ] ; then | |
[ "$multi_shield_types_updated" = true ] && commit_msg+="/" ; commit_msg+="jsDelivr" ; fi | |
commit_msg+=" shield counter" ; [ "$multi_shield_types_updated" = true ] && commit_msg+="s" | |
commit_msg+=" in root README" | |
echo "COMMIT_MSG=$commit_msg" >> $GITHUB_ENV # expose for push step | |
# Set Updated flag to check in subseuqent steps | |
if (( "$shield_types_updated" > 0 )) ; then echo "SHIELDS_UPDATED=true" >> $GITHUB_ENV ; fi | |
- name: Config committer | |
if: env.SHIELDS_UPDATED == 'true' | |
run: | | |
gpg --batch --import <(echo "${{ secrets.GPG_PRIVATE_KEY }}") | |
git config --global commit.gpgsign true | |
git config --global user.name "kudo-sync-bot" | |
git config --global user.email "[email protected]" | |
git config --global user.signingkey "${{ secrets.GPG_PRIVATE_ID }}" | |
- name: Push changes to adamlui/js-utils | |
if: env.SHIELDS_UPDATED == 'true' | |
run: | | |
cd ${{ github.workspace }}/adamlui/js-utils | |
git pull # again to sync w/ concurrent workflow updates | |
git add . | |
git commit -n -m "${{ env.COMMIT_MSG }}" || true | |
git push |