Skip to content

Commit ef0191f

Browse files
committed
Enhance algorithm
1 parent eaf5ec8 commit ef0191f

File tree

2 files changed

+33
-42
lines changed

2 files changed

+33
-42
lines changed

data/code.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,13 @@
452452
"npm": "graphql-scalars",
453453
"github": "Urigo/graphql-scalars"
454454
},
455+
{
456+
"name": "Postgraphile",
457+
"description": "builds a powerful, extensible and performant GraphQL API from a PostgreSQL schema in seconds; saving you weeks if not months of development time.",
458+
"url": "https://www.graphile.org/postgraphile",
459+
"npm": "postgraphile",
460+
"github": "graphile/postgraphile"
461+
},
455462
{
456463
"name": "SOFA",
457464
"description": "Generate REST API from your GraphQL API.",

scripts/sort-libraries.js

Lines changed: 26 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ const getGitHubStats = async githubRepo => {
44
const [owner, repoName] = githubRepo.split("/")
55
const accessToken = process.env.GITHUB_ACCESS_TOKEN
66
if (!accessToken) {
7-
throw new Error(`You must have GITHUB_ACCESS_TOKEN env variable defined!`)
7+
return {};
88
}
99
const query = /* GraphQL */ `
10-
fragment defaultBranchRef on Ref {
10+
fragment defaultBranchRefFragment on Ref {
1111
target {
1212
... on Commit {
1313
history(since: $since) {
@@ -26,17 +26,8 @@ const getGitHubStats = async githubRepo => {
2626
query($owner: String!, $repoName: String!, $since: GitTimestamp!) {
2727
repositoryOwner(login: $owner) {
2828
repository(name: $repoName) {
29-
mainRef: ref(qualifiedName: "main") {
30-
...defaultBranchRef
31-
}
32-
sourceRef: ref(qualifiedName: "source") {
33-
...defaultBranchRef
34-
}
35-
masterRef: ref(qualifiedName: "master") {
36-
...defaultBranchRef
37-
}
38-
developRef: ref(qualifiedName: "develop") {
39-
...defaultBranchRef
29+
defaultBranchRef {
30+
...defaultBranchRefFragment
4031
}
4132
stargazers {
4233
totalCount
@@ -52,7 +43,7 @@ const getGitHubStats = async githubRepo => {
5243
}
5344
`
5445
const lastMonth = new Date()
55-
lastMonth.setMonth(lastMonth.getMonth() - 1)
46+
lastMonth.setMonth(lastMonth.getMonth() - 3)
5647
const response = await fetch("https://api.github.com/graphql", {
5748
method: "POST",
5849
body: JSON.stringify({
@@ -77,23 +68,15 @@ const getGitHubStats = async githubRepo => {
7768
throw `No GitHub repo found ${owner}/${repoName}`
7869
}
7970
const stars = repo.stargazers.totalCount
80-
const actualDefaultBranch =
81-
repo.mainRef || repo.sourceRef || repo.developRef || repo.masterRef
82-
if (!actualDefaultBranch) {
83-
throw `No default branch found for ${owner}/${repoName}`
84-
}
85-
const commitHistory = actualDefaultBranch.target.history.edges
86-
let commitCount = 0,
87-
daysWithCommitSet = new Set()
71+
const commitHistory = repo.defaultBranchRef.target.history.edges
72+
let hasCommitsInLast3Months = false;
8873
commitHistory.forEach(commit => {
8974
if (!commit.node.author.name.match(/bot/i)) {
90-
commitCount++
91-
daysWithCommitSet.add(new Date(commit.node.pushedDate).getDate())
75+
hasCommitsInLast3Months = true;
9276
}
9377
})
9478
return {
95-
commitCount,
96-
daysWithCommit: daysWithCommitSet.size,
79+
hasCommitsInLast3Months,
9780
stars,
9881
}
9982
}
@@ -120,47 +103,48 @@ const getGemStats = async packageName => {
120103

121104
const sortLibs = async libs => {
122105
if (libs.length === 1) {
123-
return libs;
106+
return libs
124107
}
125108
const libsWithScores = await Promise.all(
126109
libs.map(async lib => {
127-
const [npmStats = {}, gemStars = {}, githubStats = {}] = await Promise.all([
110+
const [
111+
npmStats = {},
112+
gemStars = {},
113+
githubStats = {},
114+
] = await Promise.all([
128115
lib.npm && getNpmStats(lib.npm),
129116
lib.gem && getGemStats(lib.gem),
130117
lib.github && getGitHubStats(lib.github),
131118
])
132119
return {
133120
...lib,
134121
...npmStats,
122+
...gemStars,
135123
...githubStats,
136124
}
137125
})
138126
)
139127
return libsWithScores.sort((a, b) => {
140128
let aScore = 0,
141129
bScore = 0
142-
if (a.npm && b.npm) {
130+
if ("downloadCount" in a && 'downloadCount' in b) {
143131
if (a.downloadCount > b.downloadCount) {
144132
aScore += 40
145133
} else if (b.downloadCount > a.downloadCount) {
146134
bScore += 40
147135
}
148136
}
149-
if (a.github && b.github) {
150-
if (a.daysWithCommit > b.daysWithCommit) {
151-
aScore += 20
152-
} else if (a.daysWithCommit < b.daysWithCommit) {
153-
bScore += 20
154-
}
155-
if (a.commitCount > b.commitCount) {
156-
aScore += 20
157-
} else if (a.commitCount < b.commitCount) {
158-
bScore += 20
159-
}
137+
if ("hasCommitsInLast3Months" in a && a.hasCommitsInLast3Months) {
138+
aScore += 30
139+
}
140+
if ("hasCommitsInLast3Months" in b && b.hasCommitsInLast3Months) {
141+
bScore += 30
142+
}
143+
if ('stars' in a && 'stars' in b) {
160144
if (a.stars > b.stars) {
161-
aScore += 30
145+
aScore += 40
162146
} else if (a.stars < b.stars) {
163-
bScore += 30
147+
bScore += 40
164148
}
165149
}
166150
if (bScore > aScore) {

0 commit comments

Comments
 (0)