├── utils └── json.mjs ├── stats.json ├── package.json ├── GET_STARTED.md ├── .github └── workflows │ └── update.yml ├── collect.mjs ├── README.md └── .gitignore /utils/json.mjs: -------------------------------------------------------------------------------- 1 | import fs from "fs" 2 | 3 | export default filePath => JSON.parse(fs.readFileSync(filePath)) -------------------------------------------------------------------------------- /stats.json: -------------------------------------------------------------------------------- 1 | { 2 | "schemaVersion": 1, 3 | "label": "NPM", 4 | "message": "Internal Server Error/npm-stat/api/download-counts02024-06-14T01:14:51.663+00:00 Downloads", 5 | "color": "orange", 6 | "style": "for-the-badge", 7 | "namedLogo": "npm" 8 | } -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "github-readme-npm-downloads", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "index.js", 6 | "scripts": { 7 | "start": "node collect.mjs" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "git+https://github.com/maddhruv/github-readme-npm-downloads.git" 12 | }, 13 | "keywords": [], 14 | "author": "", 15 | "license": "ISC", 16 | "bugs": { 17 | "url": "https://github.com/maddhruv/github-readme-npm-downloads/issues" 18 | }, 19 | "homepage": "https://github.com/maddhruv/github-readme-npm-downloads#readme", 20 | "dependencies": { 21 | "lodash": "^4.17.21", 22 | "markdown-magic": "^1.0.0", 23 | "markdown-table": "^2.0.0", 24 | "npmtotal": "^1.4.0" 25 | }, 26 | "npm-stats": "maddhruv" 27 | } 28 | -------------------------------------------------------------------------------- /GET_STARTED.md: -------------------------------------------------------------------------------- 1 | # Get Started 2 | 3 | 1. `Fork` this repository. 4 | 2. Add your npm username/author or list of packages in `package.json` as `npm-stats` key. 5 | for author 6 | 7 | ```js 8 | { 9 | "npm-stats": "maddhruv" 10 | } 11 | ``` 12 | 13 | or for packages 14 | 15 | ```js 16 | { 17 | "npm-stats": [ 18 | "post-merge-install", 19 | "engines-ok" 20 | ] 21 | } 22 | ``` 23 | 24 | 3. Run `npm i` and then `npm start` to generate the downloads. 25 | 4. The repo comes with a daily CRON job that updates the downloads. 26 | 5. For updating the badge replace `maddhruv` in badge endpoint to your github username/orgname. 27 | https://img.shields.io/endpoint?url=https%3A%2F%2Fraw.githubusercontent.com%2F`username`%2Fgithub-readme-npm-downloads%2Fmaster%2Fstats.json 28 | 6. Enable `GitHub Actions` for your forked repo, as it is disabled by default for forks. 29 | 30 | ## Ref 31 | 32 | - [npmtotal](https://github.com/maddhruv/npmtotal) - Find you npm download statistics -------------------------------------------------------------------------------- /.github/workflows/update.yml: -------------------------------------------------------------------------------- 1 | name: cron 2 | 3 | on: 4 | schedule: 5 | - cron: "1 0 * * *" 6 | push: 7 | branches: 8 | - master 9 | 10 | jobs: 11 | update: 12 | runs-on: ubuntu-latest 13 | steps: 14 | - uses: actions/checkout@v2 15 | 16 | - uses: actions/setup-node@v1 17 | with: 18 | node-version: "14.x" 19 | 20 | - name: Cache node modules 21 | uses: actions/cache@v1 22 | with: 23 | path: ~/.npm 24 | key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }} 25 | restore-keys: | 26 | ${{ runner.os }}-node- 27 | 28 | - name: Install 29 | run: | 30 | npm ci 31 | 32 | - name: Collect 33 | run: | 34 | npm start 35 | 36 | - name: Add 37 | run: | 38 | git config --global user.email "action@github.com" 39 | git config --global user.name "GitHub Action" 40 | git status 41 | git add . 42 | git commit -m ":package: Update Downloads" 43 | 44 | - name: Push 45 | uses: ad-m/github-push-action@master 46 | with: 47 | github_token: ${{ secrets.GITHUB_TOKEN }} 48 | -------------------------------------------------------------------------------- /collect.mjs: -------------------------------------------------------------------------------- 1 | import path from 'path'; 2 | import fs from 'fs'; 3 | import _ from 'lodash'; 4 | import table from 'markdown-table'; 5 | import markdownMagic from 'markdown-magic'; 6 | import npmtotal from 'npmtotal' 7 | 8 | import json from './utils/json.mjs'; 9 | 10 | const { "npm-stats": key } = json('./package.json'); 11 | const badgeStats = json('./stats.json'); 12 | 13 | 14 | if (!key) { 15 | throw new Error("Please add `npm-stats` to your package.json"); 16 | } 17 | 18 | (async () => { 19 | const stats = await npmtotal(key, { 20 | startDate: "2017-01-01" 21 | }); 22 | 23 | const sortedStats = stats.stats.map(pkg => { 24 | const [name, count] = pkg; 25 | return [`[${name}](https://www.npmjs.com/package/${name})`, count]; 26 | }); 27 | 28 | badgeStats.message = `${stats.sum} Downloads`; 29 | 30 | await fs.writeFileSync("./stats.json", JSON.stringify(badgeStats, null, 2)); 31 | 32 | generate(sortedStats, stats.sum); 33 | })(); 34 | 35 | function generate(data, sum) { 36 | const config = { 37 | transforms: { 38 | PACKAGES() { 39 | return table([ 40 | ["Name", "Downloads"], 41 | ...data, 42 | ["**Sum**", `**${sum}**`] 43 | ]); 44 | } 45 | } 46 | }; 47 | 48 | markdownMagic(path.join("README.md"), config, () => { 49 | console.log(`Updated total downloads - ${sum}`); 50 | }); 51 | } 52 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # maddhruv npm-statistics 2 | 3 | ![NPM Stats](https://img.shields.io/endpoint?url=https%3A%2F%2Fraw.githubusercontent.com%2Fmaddhruv%2Fgithub-readme-npm-downloads%2Fmaster%2Fstats.json) 4 | 5 | All of my npm packages and their downloads 6 | 7 | ## Packages 8 | 9 | 10 | 11 | | Name | Downloads | 12 | | ---------------------------------------------------- | ------------------------------------------------------------------------------------ | 13 | | [error](https://www.npmjs.com/package/error) | Internal Server Error | 14 | | [path](https://www.npmjs.com/package/path) | /npm-stat/api/download-counts | 15 | | [status](https://www.npmjs.com/package/status) | 0 | 16 | | [timestamp](https://www.npmjs.com/package/timestamp) | 2024-06-14T01:14:51.663+00:00 | 17 | | **Sum** | **Internal Server Error/npm-stat/api/download-counts02024-06-14T01:14:51.663+00:00** | 18 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | yarn-debug.log* 6 | yarn-error.log* 7 | lerna-debug.log* 8 | 9 | # Diagnostic reports (https://nodejs.org/api/report.html) 10 | report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json 11 | 12 | # Runtime data 13 | pids 14 | *.pid 15 | *.seed 16 | *.pid.lock 17 | 18 | # Directory for instrumented libs generated by jscoverage/JSCover 19 | lib-cov 20 | 21 | # Coverage directory used by tools like istanbul 22 | coverage 23 | *.lcov 24 | 25 | # nyc test coverage 26 | .nyc_output 27 | 28 | # Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files) 29 | .grunt 30 | 31 | # Bower dependency directory (https://bower.io/) 32 | bower_components 33 | 34 | # node-waf configuration 35 | .lock-wscript 36 | 37 | # Compiled binary addons (https://nodejs.org/api/addons.html) 38 | build/Release 39 | 40 | # Dependency directories 41 | node_modules/ 42 | jspm_packages/ 43 | 44 | # Snowpack dependency directory (https://snowpack.dev/) 45 | web_modules/ 46 | 47 | # TypeScript cache 48 | *.tsbuildinfo 49 | 50 | # Optional npm cache directory 51 | .npm 52 | 53 | # Optional eslint cache 54 | .eslintcache 55 | 56 | # Microbundle cache 57 | .rpt2_cache/ 58 | .rts2_cache_cjs/ 59 | .rts2_cache_es/ 60 | .rts2_cache_umd/ 61 | 62 | # Optional REPL history 63 | .node_repl_history 64 | 65 | # Output of 'npm pack' 66 | *.tgz 67 | 68 | # Yarn Integrity file 69 | .yarn-integrity 70 | 71 | # dotenv environment variables file 72 | .env 73 | .env.test 74 | 75 | # parcel-bundler cache (https://parceljs.org/) 76 | .cache 77 | .parcel-cache 78 | 79 | # Next.js build output 80 | .next 81 | out 82 | 83 | # Nuxt.js build / generate output 84 | .nuxt 85 | dist 86 | 87 | # Gatsby files 88 | .cache/ 89 | # Comment in the public line in if your project uses Gatsby and not Next.js 90 | # https://nextjs.org/blog/next-9-1#public-directory-support 91 | # public 92 | 93 | # vuepress build output 94 | .vuepress/dist 95 | 96 | # Serverless directories 97 | .serverless/ 98 | 99 | # FuseBox cache 100 | .fusebox/ 101 | 102 | # DynamoDB Local files 103 | .dynamodb/ 104 | 105 | # TernJS port file 106 | .tern-port 107 | 108 | # Stores VSCode versions used for testing VSCode extensions 109 | .vscode-test 110 | 111 | # yarn v2 112 | .yarn/cache 113 | .yarn/unplugged 114 | .yarn/build-state.yml 115 | .yarn/install-state.gz 116 | .pnp.* 117 | --------------------------------------------------------------------------------