|
| 1 | +// @ts-check |
| 2 | +// Node.js script to fetch GitHub repo and NPM info, then output to public/data.json |
| 3 | +import { writeFileSync } from 'node:fs'; |
| 4 | +import fetch from 'node-fetch'; |
| 5 | + |
| 6 | +const GITHUB_USERNAME = 'baendlorel'; |
| 7 | +const GITHUB_API_BASE = 'https://api.github.com'; |
| 8 | +const NPM_REGISTRY = 'https://registry.npmjs.org/'; |
| 9 | +const OUTPUT_PATH = './fetched-data.json'; |
| 10 | + |
| 11 | +async function fetchRepos() { |
| 12 | + const res = await fetch( |
| 13 | + `${GITHUB_API_BASE}/users/${GITHUB_USERNAME}/repos?per_page=100&sort=updated` |
| 14 | + ); |
| 15 | + if (!res.ok) { |
| 16 | + throw new Error('GitHub API error: ' + res.status); |
| 17 | + } |
| 18 | + const repos = await res.json(); |
| 19 | + // return repos.filter((repo) => !repo.fork && !repo.private); |
| 20 | + return repos.filter((repo) => !repo.private); |
| 21 | +} |
| 22 | + |
| 23 | +async function fetchNpmInfo(pkgName) { |
| 24 | + try { |
| 25 | + const res = await fetch(`${NPM_REGISTRY}${pkgName}`); |
| 26 | + if (!res.ok) return null; |
| 27 | + const npmData = await res.json(); |
| 28 | + return { |
| 29 | + version: npmData['dist-tags']?.latest || 'unknown', |
| 30 | + description: npmData.description || '', |
| 31 | + homepage: npmData.homepage || '', |
| 32 | + repository: npmData.repository?.url || '', |
| 33 | + }; |
| 34 | + } catch { |
| 35 | + return null; |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +async function enrichRepos(repos) { |
| 40 | + return Promise.all( |
| 41 | + repos.map(async (repo) => { |
| 42 | + let npmInfo = null; |
| 43 | + try { |
| 44 | + const pkgRes = await fetch( |
| 45 | + `${GITHUB_API_BASE}/repos/${GITHUB_USERNAME}/${repo.name}/contents/package.json` |
| 46 | + ); |
| 47 | + if (pkgRes.ok) { |
| 48 | + const pkgData = await pkgRes.json(); |
| 49 | + const pkgContent = JSON.parse(Buffer.from(pkgData.content, 'base64').toString()); |
| 50 | + npmInfo = await fetchNpmInfo(pkgContent.name); |
| 51 | + } |
| 52 | + } catch (e) { |
| 53 | + console.error(`Error fetching package.json for ${repo.name}:`, e); |
| 54 | + } |
| 55 | + return { |
| 56 | + name: repo.name, |
| 57 | + description: repo.description, |
| 58 | + html_url: repo.html_url, |
| 59 | + stargazers_count: repo.stargazers_count, |
| 60 | + forks_count: repo.forks_count, |
| 61 | + language: repo.language, |
| 62 | + updated_at: repo.updated_at, |
| 63 | + homepage: repo.homepage, |
| 64 | + topics: repo.topics, |
| 65 | + npm: npmInfo ?? null, |
| 66 | + is_npm_package: !!npmInfo, |
| 67 | + }; |
| 68 | + }) |
| 69 | + ); |
| 70 | +} |
| 71 | + |
| 72 | +(async () => { |
| 73 | + const repos = await fetchRepos(); |
| 74 | + const data = await enrichRepos(repos); |
| 75 | + writeFileSync(OUTPUT_PATH, JSON.stringify(data, null, 2)); |
| 76 | + console.log('Data file generated:', OUTPUT_PATH); |
| 77 | +})(); |
0 commit comments