-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
46 lines (32 loc) · 1.35 KB
/
index.js
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
const { writeFileSync, readFileSync } = require("fs");
const { GITHUB_API_URL, GITHUB_REPOSITORY, GH_TOKEN } = process.env;
async function updateStargazersList() {
let CURRENT_PAGE = 1;
let requestURL = `${GITHUB_API_URL}/repos/${GITHUB_REPOSITORY}/stargazers`;
let stargazersFetched = [];
let readmeData;
try {
const readReadme = readFileSync("README.md", "utf8");
readmeData = readReadme.replace(/^\d+\. \[@.*]\(.*\)/gm, "").trimEnd();
} catch (err) {
throw new Error(err);
}
while (true) {
const request = await fetch(`${requestURL}?per_page=100&page=${CURRENT_PAGE}`, {
headers: { Authorization: `Bearer ${GH_TOKEN}`, Accept: "application/json" },
});
if (request.status === 403 || request.status === 429 || request.status === 401) {
throw new Error(`Request failed with status code ${request.status} (${request.statusText})`);
}
const data = await request.json();
if (data.length === 0) break;
data.map(({ login, html_url }) => {
return stargazersFetched.push({ login, html_url });
});
CURRENT_PAGE++;
}
const updatedStargazers = stargazersFetched.map((x, i) => `${i + 1}. [@${x.login}](${x.html_url})`).join("\n");
const updatedDescription = readmeData.replace(/\d+\//gm, `${stargazersFetched.length}/`);
writeFileSync("README.md", `${updatedDescription}\n\n${updatedStargazers}\n`, "utf8");
}
updateStargazersList();