This repository was archived by the owner on Jul 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathloadData.ts
More file actions
60 lines (52 loc) · 1.39 KB
/
Copy pathloadData.ts
File metadata and controls
60 lines (52 loc) · 1.39 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
import fetch from "node-fetch";
import fs from "node:fs/promises";
import type { GetRepoLangsQuery } from "./generated/graphql";
const query = /* GraphQL */ `
query getRepoLangs {
viewer {
repositories(ownerAffiliations: OWNER, isFork: false, first: 100) {
totalCount
nodes {
name
languages(first: 100) {
edges {
size
node {
color
name
}
}
}
}
pageInfo {
endCursor
}
}
}
}
`;
const result = (
(await fetch("https://api.github.com/graphql", {
method: "POST",
headers: {
Authorization: `Bearer ${process.env.GH_PAT}`,
},
body: JSON.stringify({ query }),
}).then(async (res) => {
const obj = (await res.json()) as any;
if (res.status >= 400) {
throw new Error(JSON.stringify(obj, null, 4));
}
return obj;
})) as { data: unknown }
).data as GetRepoLangsQuery;
const repos = result.viewer.repositories.nodes ?? [];
const data: { [repoName: string]: { [langName: string]: number } } = {};
for (const repo of repos) {
const repoLangs: typeof data[string] = {};
for (const edge of repo?.languages?.edges ?? []) {
repoLangs[edge!.node.name] = edge!.size;
}
data[repo!.name] = repoLangs;
}
await fs.writeFile("data/data.json", JSON.stringify(data, null, 2));