返回 DeepSeek-Reasonix
fetch-community.mjs
根目录 / site / scripts / fetch-community.mjs
1 // Prebuild: fetch live community stats + bake contributor avatars into public/av/.
2 // Never fails the build — falls back to the committed snapshot on any error.
3 import { mkdir, readFile, rm, writeFile } from 'node:fs/promises';
4 import sharp from 'sharp';
5
6 const repo = 'esengine/DeepSeek-Reasonix';
7 const api = `https://api.github.com/repos/${repo}`;
8 const headers = { 'User-Agent': 'reasonix-site', Accept: 'application/vnd.github+json' };
9 if (process.env.GITHUB_TOKEN) headers.Authorization = `Bearer ${process.env.GITHUB_TOKEN}`;
10
11 const fallback = JSON.parse(await readFile('src/data/contributors.json', 'utf8'));
12 let { stars, mergedPrs, contributors, list } = fallback;
13
14 try {
15 const r = await fetch(`${api}/contributors?per_page=100`, { headers });
16 if (r.ok) {
17 const humans = (await r.json())
18 .filter((c) => c.type === 'User')
19 .map((c) => ({ login: c.login, avatar: String(c.avatar_url).split('?')[0], url: c.html_url, c: c.contributions }));
20 if (humans.length) { list = humans; contributors = humans.length; }
21 }
22 } catch {}
23 try {
24 const r = await fetch(api, { headers });
25 if (r.ok) { const d = await r.json(); if (typeof d.stargazers_count === 'number') stars = d.stargazers_count; }
26 } catch {}
27 try {
28 const r = await fetch(`https://api.github.com/search/issues?q=repo:${repo}+is:pr+is:merged&per_page=1`, { headers });
29 if (r.ok) { const d = await r.json(); if (typeof d.total_count === 'number') mergedPrs = d.total_count; }
30 } catch {}
31
32 // 64px is the largest rendered avatar; bake at 2x. GitHub honours ?s= for its
33 // own CDN but serves some custom avatars unscaled and in whatever format was
34 // uploaded, so re-encode rather than trusting the response.
35 const AVATAR_PX = 128;
36
37 await rm('public/av', { recursive: true, force: true });
38 await mkdir('public/av', { recursive: true });
39 let baked = 0;
40 let bakedBytes = 0;
41 const out = await Promise.all(list.map(async (c) => {
42 const remote = `${c.avatar}?s=${AVATAR_PX}`;
43 try {
44 const r = await fetch(remote);
45 if (!r.ok) throw new Error(String(r.status));
46 const webp = await sharp(Buffer.from(await r.arrayBuffer()))
47 .resize(AVATAR_PX, AVATAR_PX, { fit: 'cover' })
48 .webp({ quality: 80 })
49 .toBuffer();
50 await writeFile(`public/av/${c.login}.webp`, webp);
51 baked++;
52 bakedBytes += webp.byteLength;
53 return { ...c, avatar: `/av/${c.login}.webp` };
54 } catch {
55 return { ...c, avatar: remote };
56 }
57 }));
58
59 await writeFile('src/data/community.json', JSON.stringify({ stars, mergedPrs, contributors, list: out }, null, 2) + '\n');
60 console.log(
61 `community: ${contributors} contributors · ${stars} stars · ${mergedPrs} merged PRs · ` +
62 `${baked}/${out.length} avatars baked (${(bakedBytes / 1024).toFixed(0)} KB)`,
63 );
64
64 lines Plain Text