File tree Expand file tree Collapse file tree 1 file changed +36
-0
lines changed Expand file tree Collapse file tree 1 file changed +36
-0
lines changed Original file line number Diff line number Diff line change
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 ( / g i t h u b \. c o m \/ ( [ ^ / ] + ) \/ ( [ ^ / ] + ) (?: \/ | $ ) / )
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
+ } ) ( )
You can’t perform that action at this time.
0 commit comments