Skip to content

Commit

Permalink
[ci] Add workflow to auto-update download count in README (#610)
Browse files Browse the repository at this point in the history
* [ci] Add workflow to auto-update downloads

This workflow runs a bash script to pull the latest download count from
npm, and update the README.md number if the new value is larger.

* [ci] Round download count number

* [ci] Tweak download count script

Makes the regex match look for the exact same string as sed does.
Probably won't actually matter, but doesn't hurt to do.
  • Loading branch information
MysteryBlokHed authored Jan 31, 2025
1 parent ffa9c3f commit db3e23b
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions .github/workflows/download_count.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
name: Update Download Count

on:
workflow_dispatch:
schedule:
- cron: "0 0 * * *"

jobs:
update-downloads:
name: Update Download Count
runs-on: ubuntu-latest

permissions:
contents: write

steps:
- uses: actions/checkout@v4

- name: Get download count
run: |
# check downloads
downloads=$(curl https://api.npmjs.org/downloads/point/1970-01-01:$(date -d '+1 day' +"%Y-%m-%d")/colorjs.io | jq -r .downloads)
echo "Latest download count: $downloads"
# into millions
downloads=$(( (downloads + 500000) / 1000000 ))
# try to find current download count in readme
readme=$(<README.md)
if [[ "$readme" =~ '['([0-9]+)' million total npm downloads]' ]]; then
current_downloads="${BASH_REMATCH[1]}"
# if the new count is bigger, update it
if [[ "$downloads" -gt "$current_downloads" ]]; then
echo "New count ($downloads million) is greater than current $current_downloads million, updating..."
readme=$(echo "$readme" | sed -E "s/\[[0-9]+ million total npm downloads\]/[$downloads million total npm downloads]/")
echo "$readme" > README.md
else
echo "New count ($downloads million) is less than or equal to current $current_downloads million; not doing anything."
fi
fi
echo Done!
- name: Commit changes
uses: stefanzweifel/git-auto-commit-action@8621497c8c39c72f3e2a999a26b4ca1b5058a842 # v5.0.1
with:
commit_message: Update README download count
file_pattern: README.md
# default github-actions user info as commit author
commit_author: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>

0 comments on commit db3e23b

Please sign in to comment.