-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_stargazers_count.js
48 lines (41 loc) · 1.26 KB
/
get_stargazers_count.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
47
48
import fs from "fs/promises";
import path from "path";
async function fetchStargazersCount() {
const response = await fetch("https://api.github.com/repos/saadeghi/daisyui");
if (!response.ok) {
throw new Error("Failed to fetch GitHub stargazers count");
}
const data = await response.json();
return data.stargazers_count;
}
async function readStatsFile(filePath) {
try {
const fileContent = await fs.readFile(filePath, "utf-8");
return JSON.parse(fileContent);
} catch (error) {
if (error.code === "ENOENT") {
return {};
}
throw error;
}
}
async function writeStatsFile(filePath, data) {
await fs.writeFile(filePath, JSON.stringify(data, null, 2), "utf-8");
}
async function updateStargazersCount() {
const filePath = path.resolve("docs", "stats.json");
try {
const stargazersCount = await fetchStargazersCount();
const fileData = await readStatsFile(filePath);
if (fileData.stargazers_count !== stargazersCount) {
fileData.stargazers_count = stargazersCount;
await writeStatsFile(filePath, fileData);
console.log("Stargazers count updated.");
} else {
console.log("Stargazers count has not changed.");
}
} catch (error) {
console.error("Error:", error);
}
}
updateStargazersCount();