|
| 1 | +import { writeFile } from 'node:fs/promises'; |
| 2 | +import { dirname, join } from 'node:path'; |
| 3 | +import { fileURLToPath } from 'node:url'; |
| 4 | + |
| 5 | +/** |
| 6 | + * Fetches last-week npm download counts and GitHub star counts, then writes |
| 7 | + * `trusted-stack-stats.json` for the docs home page. |
| 8 | + * |
| 9 | + * Requires Node.js >=22.18 (strip types). Run: |
| 10 | + * `pnpm -C docs update-trusted-stack-stats` |
| 11 | + * or: `node docs/.vitepress/theme/data/fetch-trusted-stack-stats.ts` |
| 12 | + */ |
| 13 | +import type { |
| 14 | + TrustedStackProjectId, |
| 15 | + TrustedStackStatProject, |
| 16 | + TrustedStackStatsFile, |
| 17 | +} from './trusted-stack-stats.types'; |
| 18 | + |
| 19 | +const currentDir = dirname(fileURLToPath(import.meta.url)); |
| 20 | +const OUT = join(currentDir, 'trusted-stack-stats.json'); |
| 21 | + |
| 22 | +interface ProjectSource { |
| 23 | + readonly id: TrustedStackProjectId; |
| 24 | + readonly npmPackage: string; |
| 25 | + readonly githubRepo: string; |
| 26 | +} |
| 27 | + |
| 28 | +const PROJECTS: readonly ProjectSource[] = [ |
| 29 | + { id: 'vite', npmPackage: 'vite', githubRepo: 'vitejs/vite' }, |
| 30 | + { id: 'vitest', npmPackage: 'vitest', githubRepo: 'vitest-dev/vitest' }, |
| 31 | + /** OXC row uses `oxlint` npm weekly downloads as a concrete proxy for the Oxc toolchain. */ |
| 32 | + { id: 'oxc', npmPackage: 'oxlint', githubRepo: 'oxc-project/oxc' }, |
| 33 | +]; |
| 34 | + |
| 35 | +function formatWeeklyDownloads(n: number): string { |
| 36 | + if (n >= 10_000_000) { |
| 37 | + // "m+" reads as a lower bound, so avoid rounding up. |
| 38 | + return `${Math.floor(n / 1e6)}m+`; |
| 39 | + } |
| 40 | + const m = n / 1e6; |
| 41 | + const s = m.toFixed(1).replace(/\.0$/, ''); |
| 42 | + return `${s}m+`; |
| 43 | +} |
| 44 | + |
| 45 | +function formatStars(s: number): string { |
| 46 | + return `${(s / 1000).toFixed(1)}k`; |
| 47 | +} |
| 48 | + |
| 49 | +function parseNpmDownloadsJson(data: unknown, pkg: string): number { |
| 50 | + if (typeof data !== 'object' || data === null || !('downloads' in data)) { |
| 51 | + throw new Error(`npm API ${pkg}: unexpected payload`); |
| 52 | + } |
| 53 | + const downloads = (data as { downloads: unknown }).downloads; |
| 54 | + if (typeof downloads !== 'number') { |
| 55 | + throw new Error(`npm API ${pkg}: unexpected payload`); |
| 56 | + } |
| 57 | + return downloads; |
| 58 | +} |
| 59 | + |
| 60 | +async function npmLastWeekDownloads(pkg: string): Promise<number> { |
| 61 | + const url = `https://api.npmjs.org/downloads/point/last-week/${encodeURIComponent(pkg)}`; |
| 62 | + const res = await fetch(url); |
| 63 | + if (!res.ok) { |
| 64 | + const body = await res.text(); |
| 65 | + throw new Error(`npm API ${pkg}: HTTP ${res.status} ${body}`); |
| 66 | + } |
| 67 | + return parseNpmDownloadsJson(await res.json(), pkg); |
| 68 | +} |
| 69 | + |
| 70 | +function parseGithubRepoJson(data: unknown, repo: string): number { |
| 71 | + if (typeof data !== 'object' || data === null || !('stargazers_count' in data)) { |
| 72 | + throw new Error(`GitHub API ${repo}: unexpected payload`); |
| 73 | + } |
| 74 | + const count = (data as { stargazers_count: unknown }).stargazers_count; |
| 75 | + if (typeof count !== 'number') { |
| 76 | + throw new Error(`GitHub API ${repo}: unexpected payload`); |
| 77 | + } |
| 78 | + return count; |
| 79 | +} |
| 80 | + |
| 81 | +async function fetchGithubStargazers(repo: string): Promise<number> { |
| 82 | + const url = `https://api.github.com/repos/${repo}`; |
| 83 | + const headers: Record<string, string> = { |
| 84 | + Accept: 'application/vnd.github+json', |
| 85 | + 'X-GitHub-Api-Version': '2022-11-28', |
| 86 | + 'User-Agent': |
| 87 | + 'voidzero-dev/vite-plus (docs/.vitepress/theme/data/fetch-trusted-stack-stats.ts)', |
| 88 | + }; |
| 89 | + const token = process.env.GITHUB_TOKEN; |
| 90 | + if (token !== undefined && token !== '') { |
| 91 | + headers.Authorization = `Bearer ${token}`; |
| 92 | + } |
| 93 | + const res = await fetch(url, { headers }); |
| 94 | + if (!res.ok) { |
| 95 | + const body = await res.text(); |
| 96 | + throw new Error(`GitHub API ${repo}: HTTP ${res.status} ${body}`); |
| 97 | + } |
| 98 | + return parseGithubRepoJson(await res.json(), repo); |
| 99 | +} |
| 100 | + |
| 101 | +async function main(): Promise<void> { |
| 102 | + const projects: TrustedStackStatProject[] = []; |
| 103 | + for (const p of PROJECTS) { |
| 104 | + const [npmWeeklyDownloads, stars] = await Promise.all([ |
| 105 | + npmLastWeekDownloads(p.npmPackage), |
| 106 | + fetchGithubStargazers(p.githubRepo), |
| 107 | + ]); |
| 108 | + const row: TrustedStackStatProject = { |
| 109 | + id: p.id, |
| 110 | + npmPackage: p.npmPackage, |
| 111 | + githubRepo: p.githubRepo, |
| 112 | + npmWeeklyDownloads, |
| 113 | + githubStargazers: stars, |
| 114 | + npmWeeklyDownloadsDisplay: formatWeeklyDownloads(npmWeeklyDownloads), |
| 115 | + githubStarsDisplay: formatStars(stars), |
| 116 | + }; |
| 117 | + projects.push(row); |
| 118 | + } |
| 119 | + const payload: TrustedStackStatsFile = { projects }; |
| 120 | + await writeFile(OUT, `${JSON.stringify(payload, null, 2)}\n`, 'utf8'); |
| 121 | + console.error(`Wrote ${OUT} at ${new Date().toISOString()}`); |
| 122 | +} |
| 123 | + |
| 124 | +void main().catch((err: unknown) => { |
| 125 | + console.error(err); |
| 126 | + process.exitCode = 1; |
| 127 | +}); |
0 commit comments