Skip to content

Commit 662ad4a

Browse files
committed
add script to update stars count on libraries json file
1 parent f9a8f2b commit 662ad4a

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

scripts/updateStars.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import fs from "fs"
2+
import path from "path"
3+
4+
const filePath = path.join(process.cwd(), "/scripts/example-repos.json")
5+
const reposGroup = JSON.parse(fs.readFileSync(filePath, "utf-8"))
6+
7+
async function getStars(repoUrl) {
8+
const match = repoUrl.match(/github\.com\/([^/]+)\/([^/]+)(?:\/|$)/)
9+
if(!match) throw new Error(`Invalid github repo URL: ${repoUrl}`)
10+
const [, owner, repo] = match
11+
const res = await fetch(`https://api.github.com/repos/${owner}/${repo}`, {
12+
headers: {
13+
Accept: "application/vnd.github+json"
14+
}
15+
})
16+
if(!res.ok) throw new Error(`${repoUrl} -> ${res.status} ${res.statusText}`)
17+
const data = await res.json()
18+
return data.stargazers_count ?? null
19+
}
20+
21+
(async () => {
22+
const objectEntries = Object.values(reposGroup)
23+
for(const group of objectEntries) {
24+
for(const lib of group.libs) {
25+
try {
26+
const stars = await getStars(lib.repoUrl)
27+
lib.stars = stars
28+
console.log(`✅ ${lib.repoUrl} -> ${stars}`)
29+
} catch(e) {
30+
lib.stars = null
31+
console.error(`❌ ${lib.repoUrl}: ${e.message}`)
32+
}
33+
}
34+
}
35+
fs.writeFileSync(filePath, JSON.stringify(reposGroup, null, 2) + "\n")
36+
})()

0 commit comments

Comments
 (0)