Skip to content

Update npm/clones/jsDelivr shields in root README #47

Update npm/clones/jsDelivr shields in root README

Update npm/clones/jsDelivr shields in root README #47

name: Update npm/clones/jsDelivr shields in root README
on:
schedule:
- cron: "45 3 * * 1,3,5" # every Mon/Wed/Fri @ 3:45 AM
jobs:
update-root-npm-clones-jsd-shields:
runs-on: ubuntu-latest
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
id: get-stats
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/calculate git clone count
biweekly_clones=$(curl -sSL \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer ${{ secrets.REPO_SYNC_PAT }}" \
-H "X-GitHub-Api-Version: 2022-11-28" \
"https://api.github.com/repos/adamlui/js-utils/traffic/clones" |
sed -n -E '0,/.*"count": ([0-9]+).*/ s//\1/p')
total_clones=$((biweekly_clones * 2))
echo "Biweekly git clones: $biweekly_clones"
echo -e "\n-----\nTotal monthly git clones: $total_clones\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_clones=$(format_total "$total_clones")
echo "Formatted total monthly git clones: $formatted_total_clones"
formatted_total_requests=$(format_total $((total_pkg_requests + total_repo_requests)))
echo "Formatted total monthly jsDelivr requests: $formatted_total_requests"
# Expose as outputs for update step next
echo "total_downloads=$formatted_total_downloads" >> $GITHUB_OUTPUT
echo "total_clones=$formatted_total_clones" >> $GITHUB_OUTPUT
echo "total_requests=$formatted_total_requests" >> $GITHUB_OUTPUT
- name: Update README shields
id: update-shields
run: |
cd ${{ github.workspace }}/adamlui/js-utils
total_downloads="${{ steps.get-stats.outputs.total_downloads }}"
total_clones="${{ steps.get-stats.outputs.total_clones }}"
total_requests="${{ steps.get-stats.outputs.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 git clones shield
if [ "$total_clones" == "0" ] ; then echo "Error getting total git clones"
else # perform update
for readme in $(find docs/ -name "README.md") ; do
old_readme=$(<"$readme")
sed -i -E "s|(badge/[^-]+-)[0-9.,km+]+(/[^?]+\?logo=github)|\1$total_clones\2|gI" "$readme"
new_readme=$(<"$readme")
if [ "$old_readme" != "$new_readme" ] ; then clones_updated=true ; fi
done
if [ "$clones_updated" = true ] ; then echo "Git clones shields updated to $total_clones"
else echo "Git clones 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))
[ "$clones_updated" = true ] && ((shield_types_updated+=1))
[ "$requests_updated" = true ] && ((shield_types_updated+=1))
if [ "$shield_types_updated" -gt 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 [ "$clones_updated" = true ] ; then
[ "$downloads_updated" = true ] && commit_msg+="/" ; commit_msg+="clones" ; fi
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_OUTPUT # expose as output
- name: Push to adamlui/js-utils
run: |
cd ${{ github.workspace }}/adamlui/js-utils
git config --global user.name "kudo-sync-bot"
git config --global user.email "auto-sync@kudoai.com"
git add .
git commit -n -m "${{ steps.update-shields.outputs.commit_msg }}" || true
git push