From 8f02edad4312ba4a91f2d3cadf2c355d68803da0 Mon Sep 17 00:00:00 2001
From: Adam Thompson-Sharpe <adamthompsonsharpe@gmail.com>
Date: Mon, 2 Dec 2024 09:42:03 -0500
Subject: [PATCH 1/3] [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.
---
 .github/workflows/download_count.yaml | 47 +++++++++++++++++++++++++++
 1 file changed, 47 insertions(+)
 create mode 100644 .github/workflows/download_count.yaml

diff --git a/.github/workflows/download_count.yaml b/.github/workflows/download_count.yaml
new file mode 100644
index 000000000..147ee81b8
--- /dev/null
+++ b/.github/workflows/download_count.yaml
@@ -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 / 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>

From 477e10cf731a499a13b84b8ff844487c419c01b7 Mon Sep 17 00:00:00 2001
From: Adam Thompson-Sharpe <adamthompsonsharpe@gmail.com>
Date: Fri, 27 Dec 2024 00:35:11 -0500
Subject: [PATCH 2/3] [ci] Round download count number

---
 .github/workflows/download_count.yaml | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/.github/workflows/download_count.yaml b/.github/workflows/download_count.yaml
index 147ee81b8..b5d0ccd6e 100644
--- a/.github/workflows/download_count.yaml
+++ b/.github/workflows/download_count.yaml
@@ -22,7 +22,7 @@ jobs:
           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 / 1000000))
+          downloads=$(( (downloads + 500000) / 1000000 ))
           # try to find current download count in readme
           readme=$(<README.md)
           if [[ "$readme" =~ ([0-9]+)" million total npm downloads" ]]; then

From 171842ae070dd493f477b72e3b5c7b9331f56c8d Mon Sep 17 00:00:00 2001
From: Adam Thompson-Sharpe <adamthompsonsharpe@gmail.com>
Date: Fri, 27 Dec 2024 02:04:07 -0500
Subject: [PATCH 3/3] [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.
---
 .github/workflows/download_count.yaml | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/.github/workflows/download_count.yaml b/.github/workflows/download_count.yaml
index b5d0ccd6e..b01476dce 100644
--- a/.github/workflows/download_count.yaml
+++ b/.github/workflows/download_count.yaml
@@ -25,7 +25,7 @@ jobs:
           downloads=$(( (downloads + 500000) / 1000000 ))
           # try to find current download count in readme
           readme=$(<README.md)
-          if [[ "$readme" =~ ([0-9]+)" million total npm downloads" ]]; then
+          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