1
- const fetch = require ( `node-fetch` ) ;
2
- const numbro = require ( "numbro" ) ;
3
- const timeago = require ( ' timeago.js' ) ;
1
+ const fetch = require ( `node-fetch` )
2
+ const numbro = require ( "numbro" )
3
+ const timeago = require ( " timeago.js" )
4
4
5
5
const getGitHubStats = async githubRepo => {
6
6
const [ owner , repoName ] = githubRepo . split ( "/" )
7
7
const accessToken = process . env . GITHUB_ACCESS_TOKEN
8
8
if ( ! accessToken ) {
9
- return { } ;
9
+ return {
10
+ accessToken : false ,
11
+ }
10
12
}
11
13
const query = /* GraphQL */ `
12
14
fragment defaultBranchRefFragment on Ref {
@@ -25,7 +27,7 @@ const getGitHubStats = async githubRepo => {
25
27
}
26
28
}
27
29
}
28
- query($owner: String!, $repoName: String!, $since: GitTimestamp!) {
30
+ query ($owner: String!, $repoName: String!, $since: GitTimestamp!) {
29
31
repositoryOwner(login: $owner) {
30
32
repository(name: $repoName) {
31
33
defaultBranchRef {
@@ -43,12 +45,16 @@ const getGitHubStats = async githubRepo => {
43
45
licenseInfo {
44
46
name
45
47
}
46
- releases(last : 1) {
48
+ releases(first : 1) {
47
49
nodes {
48
- publishedAt
50
+ publishedAt
49
51
}
50
52
}
51
- tags: refs(refPrefix: "refs/tags/", first: 1, orderBy: {field: TAG_COMMIT_DATE, direction: DESC}) {
53
+ tags: refs(
54
+ refPrefix: "refs/tags/"
55
+ first: 1
56
+ orderBy: { field: TAG_COMMIT_DATE, direction: DESC }
57
+ ) {
52
58
nodes {
53
59
name
54
60
target {
@@ -81,7 +87,7 @@ const getGitHubStats = async githubRepo => {
81
87
} )
82
88
const responseJson = await response . json ( )
83
89
if ( responseJson && responseJson . errors ) {
84
- throw JSON . stringify ( responseJson . errors ) ;
90
+ throw JSON . stringify ( responseJson . errors )
85
91
}
86
92
if ( ! responseJson || ! responseJson . data ) {
87
93
throw `GitHub returned empty response for ${ owner } /${ repoName } `
@@ -97,27 +103,33 @@ const getGitHubStats = async githubRepo => {
97
103
const stars = repo . stargazers . totalCount
98
104
const commitHistory = repo . defaultBranchRef . target . history . edges
99
105
100
- let hasCommitsInLast3Months = false ;
106
+ let hasCommitsInLast3Months = false
101
107
commitHistory . forEach ( commit => {
102
108
if ( ! commit . node . author . name . match ( / b o t / i) ) {
103
- hasCommitsInLast3Months = true ;
109
+ hasCommitsInLast3Months = true
104
110
}
105
111
} )
106
112
const formattedStars = numbro ( stars ) . format ( {
107
113
average : true ,
108
- } ) ;
109
-
110
- const releases = [ ] ;
111
- if ( repo . tags && repo . tags . nodes && repo . tags . nodes . length && repo . tags . nodes [ 0 ] . target . target && repo . tags . nodes [ 0 ] . target . target . pushedDate ) {
112
- releases . push ( repo . tags . nodes [ 0 ] . target . target . pushedDate ) ;
114
+ } )
115
+
116
+ const releases = [ ]
117
+ if (
118
+ repo . tags &&
119
+ repo . tags . nodes &&
120
+ repo . tags . nodes . length &&
121
+ repo . tags . nodes [ 0 ] . target . target &&
122
+ repo . tags . nodes [ 0 ] . target . target . pushedDate
123
+ ) {
124
+ releases . push ( repo . tags . nodes [ 0 ] . target . target . pushedDate )
113
125
}
114
126
if ( repo . releases && repo . releases . nodes && repo . releases . nodes . length ) {
115
127
releases . push ( repo . releases . nodes [ 0 ] . publishedAt )
116
128
}
117
- if ( owner . includes ( "graphql" ) ) {
129
+ if ( owner . includes ( "graphql" ) ) {
118
130
console . log ( { releases, repoName } )
119
131
}
120
-
132
+
121
133
const lastRelease = releases . filter ( Boolean ) . sort ( ) . reverse ( ) [ 0 ]
122
134
123
135
return {
@@ -151,32 +163,29 @@ const getGemStats = async packageName => {
151
163
}
152
164
153
165
const sortLibs = async libs => {
154
- let totalStars = 0 ;
166
+ let totalStars = 0
155
167
const libsWithScores = await Promise . all (
156
168
libs . map ( async lib => {
157
- const [
158
- npmStats = { } ,
159
- gemStars = { } ,
160
- githubStats = { } ,
161
- ] = await Promise . all ( [
162
- lib . npm && getNpmStats ( lib . npm ) ,
163
- lib . gem && getGemStats ( lib . gem ) ,
164
- lib . github && getGitHubStats ( lib . github ) ,
165
- ] )
169
+ const [ npmStats = { } , gemStars = { } , githubStats = { } ] =
170
+ await Promise . all ( [
171
+ lib . npm && getNpmStats ( lib . npm ) ,
172
+ lib . gem && getGemStats ( lib . gem ) ,
173
+ lib . github && getGitHubStats ( lib . github ) ,
174
+ ] )
166
175
const result = {
167
176
...lib ,
168
177
...npmStats ,
169
178
...gemStars ,
170
179
...githubStats ,
171
180
}
172
- totalStars += result . stars || 0 ;
173
- return result ;
181
+ totalStars += result . stars || 0
182
+ return result
174
183
} )
175
184
)
176
185
const sortedLibs = libsWithScores . sort ( ( a , b ) => {
177
186
let aScore = 0 ,
178
187
bScore = 0
179
- if ( "downloadCount" in a && ' downloadCount' in b ) {
188
+ if ( "downloadCount" in a && " downloadCount" in b ) {
180
189
if ( a . downloadCount > b . downloadCount ) {
181
190
aScore += 40
182
191
} else if ( b . downloadCount > a . downloadCount ) {
@@ -189,7 +198,7 @@ const sortLibs = async libs => {
189
198
if ( "hasCommitsInLast3Months" in b && b . hasCommitsInLast3Months ) {
190
199
bScore += 30
191
200
}
192
- if ( ' stars' in a && ' stars' in b ) {
201
+ if ( " stars" in a && " stars" in b ) {
193
202
if ( a . stars > b . stars ) {
194
203
aScore += 40
195
204
} else if ( a . stars < b . stars ) {
0 commit comments