Repository navigation
Expand file tree
/
Copy pathstats.mjs
More file actions
106 lines (91 loc) · 3 KB
/
Copy pathstats.mjs
File metadata and controls
106 lines (91 loc) · 3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
// Pulls every number the README renders. Public APIs only, no keys except an
// optional GITHUB_TOKEN for rate headroom. Writes data/stats.json.
import { writeFileSync, mkdirSync } from "node:fs";
import { dirname, join } from "node:path";
import { fileURLToPath } from "node:url";
const ROOT = join(dirname(fileURLToPath(import.meta.url)), "..");
const USER = "ArihantDeva";
const TOKEN = process.env.GITHUB_TOKEN || process.env.GH_TOKEN || "";
const ghHeaders = { Accept: "application/vnd.github+json", "User-Agent": USER };
if (TOKEN) ghHeaders.Authorization = `Bearer ${TOKEN}`;
async function getJSON(url, headers = {}) {
const res = await fetch(url, { headers: { "User-Agent": USER, ...headers } });
if (!res.ok) throw new Error(`${res.status} ${url}`);
return res.json();
}
const NPM_PACKAGES = ["@arihantdeva/heimdall", "@arihantdeva/janus"];
// Repos that carry the story. Stars and pushes are read live so the page can
// never drift from GitHub.
const FEATURED = ["heimdall", "janus", "Aegis", "agent-partner", "hunt"];
async function main() {
const user = await getJSON(`https://api.github.com/users/${USER}`, ghHeaders);
const repoList = await getJSON(
`https://api.github.com/users/${USER}/repos?per_page=100&sort=pushed`,
ghHeaders
);
const repos = {};
for (const name of FEATURED) {
const r = repoList.find((x) => x.name === name);
if (!r) continue;
repos[name] = {
name: r.name,
stars: r.stargazers_count,
forks: r.forks_count,
language: r.language,
description: r.description,
topics: r.topics || [],
url: r.html_url,
pushedAt: r.pushed_at,
};
}
// npm downloads, last 30 days, summed across packages
let npmDownloads = 0;
let npmOk = true;
for (const pkg of NPM_PACKAGES) {
try {
const d = await getJSON(
`https://api.npmjs.org/downloads/point/last-month/${pkg}`
);
npmDownloads += d.downloads || 0;
} catch {
npmOk = false;
}
}
const totalStars = Object.values(repos).reduce((a, r) => a + r.stars, 0);
const ownRepos = repoList.filter((r) => !r.fork).length;
const forkRepos = repoList.filter((r) => r.fork).length;
const stats = {
generatedAt: new Date().toISOString().slice(0, 10),
user: {
login: user.login,
name: user.name,
bio: user.bio,
blog: user.blog,
location: user.location,
followers: user.followers,
publicRepos: user.public_repos,
createdAt: user.created_at,
},
repos,
totals: {
stars: totalStars,
npmDownloads,
npmOk,
ownRepos,
forkRepos,
featuredCount: Object.keys(repos).length,
},
};
mkdirSync(join(ROOT, "data"), { recursive: true });
writeFileSync(
join(ROOT, "data/stats.json"),
JSON.stringify(stats, null, 2) + "\n"
);
console.log(
`stats.json → ${Object.keys(repos).length} featured repos, ${totalStars} stars, ${npmDownloads} npm/mo`
);
}
main().catch((e) => {
console.error("stats failed:", e.message);
process.exit(1);
});