|
| 1 | +#!/usr/bin/env node |
| 2 | +const fs = require('fs') |
| 3 | +const path = require('path') |
| 4 | + |
| 5 | +;(async () => { |
| 6 | + const repo = process.argv[2] |
| 7 | + const file = process.argv[3] ?? '.vitepress/contributors.json' |
| 8 | + console.log(`get-contributors - repo: ${repo} - file: ${file}`) |
| 9 | + |
| 10 | + if (!repo || !file) { |
| 11 | + console.error('Usage: node get-contributors.js user/repo [contributors.json]') |
| 12 | + process.exit(1) |
| 13 | + } |
| 14 | + |
| 15 | + fs.mkdirSync(path.dirname(file), { recursive: true }) |
| 16 | + |
| 17 | + let data = [] |
| 18 | + try { |
| 19 | + data = await githubContributors(repo) |
| 20 | + } catch (e) { |
| 21 | + console.warn(e) |
| 22 | + } |
| 23 | + console.log(`get-contributors - contributors: ${data.length}`) |
| 24 | + fs.writeFileSync(file, JSON.stringify(data), 'utf8') |
| 25 | +})() |
| 26 | + |
| 27 | +async function githubContributors(repo) { |
| 28 | + let results = [] |
| 29 | + let page = 1 |
| 30 | + |
| 31 | + while (true) { |
| 32 | + const url = `https://api.github.com/repos/${repo}/contributors?per_page=100&page=${page}` |
| 33 | + const data = { headers: { Accept: 'application/vnd.github+json' } } |
| 34 | + |
| 35 | + const response = await fetch(url, data) |
| 36 | + // console.log('response:', response) |
| 37 | + if (!response.ok) break |
| 38 | + |
| 39 | + const contributors = await response.json() |
| 40 | + // console.log('contributors:', contributors) |
| 41 | + if (!contributors.length) break |
| 42 | + |
| 43 | + const filtered = contributors |
| 44 | + .filter((c) => c.type === 'User') |
| 45 | + .map((c) => ({ |
| 46 | + login: c.login, |
| 47 | + avatar_url: c.avatar_url, |
| 48 | + contributions: c.contributions, |
| 49 | + })) |
| 50 | + // console.log('filtered:', filtered) |
| 51 | + |
| 52 | + results.push(...filtered) |
| 53 | + page++ |
| 54 | + await new Promise((resolve) => setTimeout(resolve, 250)) |
| 55 | + } |
| 56 | + |
| 57 | + return results |
| 58 | +} |
0 commit comments