Skip to content

Commit a7c67f6

Browse files
committed
Move all libraries to data.json
1 parent 2dce624 commit a7c67f6

File tree

10 files changed

+906
-885
lines changed

10 files changed

+906
-885
lines changed

data/code.json

Lines changed: 499 additions & 28 deletions
Large diffs are not rendered by default.

gatsby-node.js

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,25 @@ exports.onCreatePage = async ({ page, actions }) => {
1010
sourcePath: path.relative(__dirname, page.componentPath),
1111
}
1212
if (page.path === "/code" || page.path === "/code/") {
13-
const [jsGraphQLClients, jsServerLibraries, rubyServerLibraries, tools] = await Promise.all([
14-
sortLibs(
15-
JSON.parse(readFileSync("./data/js-graphql-clients.json", "utf8"))
16-
),
17-
sortLibs(
18-
JSON.parse(readFileSync("./data/js-server-libraries.json", "utf8"))
19-
),
20-
sortLibs(
21-
JSON.parse(readFileSync("./data/ruby-server-libraries.json", "utf8"))
22-
),
23-
sortLibs(JSON.parse(readFileSync("./data/tools.json", "utf8"))),
13+
const codeData = JSON.parse(readFileSync("./data/code.json", "utf8"));
14+
await Promise.all([
15+
Promise.all(Object.keys(codeData.Libraries).map(async languageName => {
16+
const libraryCategoryMap = codeData.Libraries[languageName];
17+
await Promise.all(
18+
Object.keys(libraryCategoryMap).map(async libraryCategoryName => {
19+
const libraries = libraryCategoryMap[libraryCategoryName]
20+
libraryCategoryMap[libraryCategoryName] = await sortLibs(libraries)
21+
})
22+
)
23+
})),
24+
sortLibs(codeData.Tools).then(sortedTools => {
25+
codeData.Tools = sortedTools;
26+
}),
2427
])
2528

2629
context = {
2730
...context,
28-
jsGraphQLClients,
29-
jsServerLibraries,
30-
rubyServerLibraries,
31-
tools,
31+
codeData,
3232
}
3333
}
3434
createPage({

scripts/sort-libraries.js

Lines changed: 39 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ const fetch = require(`node-fetch`)
22

33
const getGitHubStats = async githubRepo => {
44
const [owner, repoName] = githubRepo.split("/")
5-
const accessToken = process.env.GITHUB_ACCESS_TOKEN;
5+
const accessToken = process.env.GITHUB_ACCESS_TOKEN
66
if (!accessToken) {
7-
throw new Error(`You must have GITHUB_ACCESS_TOKEN env variable defined!`);
7+
throw new Error(`You must have GITHUB_ACCESS_TOKEN env variable defined!`)
88
}
99
const query = /* GraphQL */ `
1010
fragment defaultBranchRef on Ref {
@@ -35,6 +35,9 @@ const getGitHubStats = async githubRepo => {
3535
masterRef: ref(qualifiedName: "master") {
3636
...defaultBranchRef
3737
}
38+
developRef: ref(qualifiedName: "develop") {
39+
...defaultBranchRef
40+
}
3841
stargazers {
3942
totalCount
4043
}
@@ -48,8 +51,8 @@ const getGitHubStats = async githubRepo => {
4851
}
4952
}
5053
`
51-
const lastMonth = new Date();
52-
lastMonth.setMonth(lastMonth.getMonth() - 1);
54+
const lastMonth = new Date()
55+
lastMonth.setMonth(lastMonth.getMonth() - 1)
5356
const response = await fetch("https://api.github.com/graphql", {
5457
method: "POST",
5558
body: JSON.stringify({
@@ -62,11 +65,26 @@ const getGitHubStats = async githubRepo => {
6265
},
6366
})
6467
const responseJson = await response.json()
65-
const repo = responseJson.data.repositoryOwner.repository
68+
if (!responseJson?.data) {
69+
throw `GitHub returned empty response for ${owner}/${repoName}`
70+
}
71+
const { repositoryOwner } = responseJson.data
72+
if (!repositoryOwner) {
73+
throw `No GitHub user found for ${owner}/${repoName}`
74+
}
75+
const { repository: repo } = repositoryOwner
76+
if (!repo) {
77+
throw `No GitHub repo found ${owner}/${repoName}`
78+
}
6679
const stars = repo.stargazers.totalCount
67-
const commitHistory = (repo.mainRef || repo.sourceRef || repo.masterRef)
68-
.target.history.edges
69-
let commitCount = 0, daysWithCommitSet = new Set(), finalUpdatedTime;
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()
7088
commitHistory.forEach(commit => {
7189
if (!commit.node.author.name.match(/bot/i)) {
7290
commitCount++
@@ -92,16 +110,21 @@ const getNpmStats = async packageName => {
92110
}
93111

94112
const getGemStats = async packageName => {
95-
const response = await fetch(`https://rubygems.org/api/v1/gems/${encodeURIComponent(packageName)}.json`);
113+
const response = await fetch(
114+
`https://rubygems.org/api/v1/gems/${encodeURIComponent(packageName)}.json`
115+
)
96116
const responseJson = await response.json()
97117
const downloadCount = responseJson.downloads
98118
return { downloadCount }
99119
}
100120

101121
const sortLibs = async libs => {
122+
if (libs.length === 1) {
123+
return libs;
124+
}
102125
const libsWithScores = await Promise.all(
103126
libs.map(async lib => {
104-
const [npmStats = {}, githubStats = {}] = await Promise.all([
127+
const [npmStats = {}, gemStars = {}, githubStats = {}] = await Promise.all([
105128
lib.npm && getNpmStats(lib.npm),
106129
lib.gem && getGemStats(lib.gem),
107130
lib.github && getGitHubStats(lib.github),
@@ -118,9 +141,9 @@ const sortLibs = async libs => {
118141
bScore = 0
119142
if (a.npm && b.npm) {
120143
if (a.downloadCount > b.downloadCount) {
121-
aScore += 40;
144+
aScore += 40
122145
} else if (b.downloadCount > a.downloadCount) {
123-
bScore += 40;
146+
bScore += 40
124147
}
125148
}
126149
if (a.github && b.github) {
@@ -130,14 +153,14 @@ const sortLibs = async libs => {
130153
bScore += 20
131154
}
132155
if (a.commitCount > b.commitCount) {
133-
aScore += 20;
156+
aScore += 20
134157
} else if (a.commitCount < b.commitCount) {
135-
bScore += 20;
158+
bScore += 20
136159
}
137160
if (a.stars > b.stars) {
138-
aScore += 30;
161+
aScore += 30
139162
} else if (a.stars < b.stars) {
140-
bScore += 30;
163+
bScore += 30
141164
}
142165
}
143166
if (bScore > aScore) {

0 commit comments

Comments
 (0)