-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_github_dependents_count.js
62 lines (54 loc) · 1.8 KB
/
get_github_dependents_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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import fs from "fs/promises";
import path from "path";
import { exec } from "child_process";
import util from "util";
const execPromise = util.promisify(exec);
async function fetchDependentsCount() {
const command = `curl -s "https://github.com/saadeghi/daisyui/network/dependents" | grep -B 1 ' Repositories' | head -1 | sed 's/ *//;s/,//'`;
try {
const { stdout } = await execPromise(command);
const dependentsCount = parseInt(stdout.trim(), 10);
if (isNaN(dependentsCount) || dependentsCount === 0) {
throw new Error("Invalid dependents count");
}
return dependentsCount;
} catch (error) {
console.error(`Failed to fetch dependents count: ${error.message}`);
return null;
}
}
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 updateDependentsCount() {
const filePath = path.resolve("docs", "stats.json");
try {
const dependentsCount = await fetchDependentsCount();
if (dependentsCount === null || dependentsCount === undefined) {
console.log("Skipping update due to invalid dependents count.");
return;
}
const fileData = await readStatsFile(filePath);
if (fileData.dependents_count !== dependentsCount) {
fileData.dependents_count = dependentsCount;
await writeStatsFile(filePath, fileData);
console.log("Dependents count updated.");
} else {
console.log("Dependents count has not changed.");
}
} catch (error) {
console.error("Error:", error);
}
}
updateDependentsCount();