Skip to content

Commit db3e23b

Browse files
[ci] Add workflow to auto-update download count in README (#610)
* [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.
1 parent ffa9c3f commit db3e23b

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

.github/workflows/download_count.yaml

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
name: Update Download Count
2+
3+
on:
4+
workflow_dispatch:
5+
schedule:
6+
- cron: "0 0 * * *"
7+
8+
jobs:
9+
update-downloads:
10+
name: Update Download Count
11+
runs-on: ubuntu-latest
12+
13+
permissions:
14+
contents: write
15+
16+
steps:
17+
- uses: actions/checkout@v4
18+
19+
- name: Get download count
20+
run: |
21+
# check downloads
22+
downloads=$(curl https://api.npmjs.org/downloads/point/1970-01-01:$(date -d '+1 day' +"%Y-%m-%d")/colorjs.io | jq -r .downloads)
23+
echo "Latest download count: $downloads"
24+
# into millions
25+
downloads=$(( (downloads + 500000) / 1000000 ))
26+
# try to find current download count in readme
27+
readme=$(<README.md)
28+
if [[ "$readme" =~ '['([0-9]+)' million total npm downloads]' ]]; then
29+
current_downloads="${BASH_REMATCH[1]}"
30+
# if the new count is bigger, update it
31+
if [[ "$downloads" -gt "$current_downloads" ]]; then
32+
echo "New count ($downloads million) is greater than current $current_downloads million, updating..."
33+
readme=$(echo "$readme" | sed -E "s/\[[0-9]+ million total npm downloads\]/[$downloads million total npm downloads]/")
34+
echo "$readme" > README.md
35+
else
36+
echo "New count ($downloads million) is less than or equal to current $current_downloads million; not doing anything."
37+
fi
38+
fi
39+
echo Done!
40+
41+
- name: Commit changes
42+
uses: stefanzweifel/git-auto-commit-action@8621497c8c39c72f3e2a999a26b4ca1b5058a842 # v5.0.1
43+
with:
44+
commit_message: Update README download count
45+
file_pattern: README.md
46+
# default github-actions user info as commit author
47+
commit_author: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>

0 commit comments

Comments
 (0)