-
Notifications
You must be signed in to change notification settings - Fork 1.5k
[Gatsby] New Code Page #936
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
1020b14
Redesign Code Page
ardatan c2efe52
New code.json
ardatan 8729d1d
Move all libraries to data.json
ardatan 0194af4
Do not use new syntax
ardatan 23c2bc9
Move Tools under Languages
ardatan cbd1b42
Add Tools back
ardatan c8ffd94
Fix Tools link
ardatan e368125
Fix Tools link
ardatan 9b2aa58
Give a custom slug for generic tools section
ardatan d0af7cf
Fix blogs page
ardatan 0a7d6fb
Add codegen back to JS tools
ardatan eaf5ec8
Fix github links
ardatan ef0191f
Enhance algorithm
ardatan 5486956
Design improvements
ardatan 59472bb
Pixelperfect
ardatan 7b0b8e4
Fix language boxes
ardatan 92d31af
Sort languages and small fixes
ardatan fd82624
Fix GitHub links
ardatan 55beff2
Add AnchorLink for smooth scrolling
ardatan 5e83928
Do not use new JS syntax
ardatan 19b1f88
Add more libs to Code page
ardatan b9b2e0f
Use frontmatter instead of JSON file
ardatan 97d5b60
Expansible code examples
ardatan c584a57
Improve CSS
ardatan 7b94494
More CSS
ardatan 135d93b
Remove 'stuff', add mobile support, some css tweaks and only allows e…
99aedfa
Small fixes
ardatan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,209 @@ | ||
const fetch = require(`node-fetch`); | ||
const numbro = require("numbro"); | ||
const timeago = require('timeago.js'); | ||
|
||
const getGitHubStats = async githubRepo => { | ||
const [owner, repoName] = githubRepo.split("/") | ||
const accessToken = process.env.GITHUB_ACCESS_TOKEN | ||
if (!accessToken) { | ||
return {}; | ||
} | ||
const query = /* GraphQL */ ` | ||
fragment defaultBranchRefFragment on Ref { | ||
target { | ||
... on Commit { | ||
history(since: $since) { | ||
edges { | ||
node { | ||
author { | ||
name | ||
} | ||
pushedDate | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
query($owner: String!, $repoName: String!, $since: GitTimestamp!) { | ||
repositoryOwner(login: $owner) { | ||
repository(name: $repoName) { | ||
defaultBranchRef { | ||
...defaultBranchRefFragment | ||
} | ||
stargazers { | ||
totalCount | ||
} | ||
updatedAt | ||
forkCount | ||
pullRequests { | ||
totalCount | ||
} | ||
description | ||
licenseInfo { | ||
name | ||
} | ||
releases(last: 1) { | ||
nodes { | ||
publishedAt | ||
} | ||
} | ||
tags: refs(refPrefix: "refs/tags/", first: 1, orderBy: {field: TAG_COMMIT_DATE, direction: DESC}) { | ||
nodes { | ||
name | ||
target { | ||
... on Tag { | ||
target { | ||
... on Commit { | ||
pushedDate | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
` | ||
const lastMonth = new Date() | ||
lastMonth.setMonth(lastMonth.getMonth() - 3) | ||
const response = await fetch("https://api.github.com/graphql", { | ||
method: "POST", | ||
body: JSON.stringify({ | ||
query, | ||
variables: { owner, repoName, since: lastMonth }, | ||
}), | ||
headers: { | ||
Authorization: `Bearer ${accessToken}`, | ||
"Content-Type": "application/json", | ||
}, | ||
}) | ||
const responseJson = await response.json() | ||
if (responseJson && responseJson.errors) { | ||
throw JSON.stringify(responseJson.errors); | ||
} | ||
if (!responseJson || !responseJson.data) { | ||
throw `GitHub returned empty response for ${owner}/${repoName}` | ||
} | ||
const { repositoryOwner } = responseJson.data | ||
if (!repositoryOwner) { | ||
throw `No GitHub user found for ${owner}/${repoName}` | ||
} | ||
const { repository: repo } = repositoryOwner | ||
if (!repo) { | ||
throw `No GitHub repo found ${owner}/${repoName}` | ||
} | ||
const stars = repo.stargazers.totalCount | ||
const commitHistory = repo.defaultBranchRef.target.history.edges | ||
|
||
let hasCommitsInLast3Months = false; | ||
commitHistory.forEach(commit => { | ||
if (!commit.node.author.name.match(/bot/i)) { | ||
hasCommitsInLast3Months = true; | ||
} | ||
}) | ||
const formattedStars = numbro(stars).format({ | ||
average: true, | ||
}); | ||
|
||
const releases = []; | ||
if (repo.tags && repo.tags.nodes && repo.tags.nodes.length && repo.tags.nodes[0].target.target && repo.tags.nodes[0].target.target.pushedDate) { | ||
releases.push(repo.tags.nodes[0].target.target.pushedDate); | ||
} | ||
if (repo.releases && repo.releases.nodes && repo.releases.nodes.length) { | ||
releases.push(repo.releases.nodes[0].publishedAt) | ||
} | ||
if(owner.includes("graphql")) { | ||
console.log({ releases, repoName }) | ||
} | ||
|
||
const lastRelease = releases.filter(Boolean).sort().reverse()[0] | ||
|
||
return { | ||
hasCommitsInLast3Months, | ||
stars, | ||
formattedStars, | ||
license: repo.licenseInfo && repo.licenseInfo.name, | ||
lastRelease, | ||
formattedLastRelease: lastRelease && timeago.format(lastRelease), | ||
} | ||
} | ||
|
||
const getNpmStats = async packageName => { | ||
const response = await fetch( | ||
`https://api.npmjs.org/downloads/point/last-week/${encodeURIComponent( | ||
packageName | ||
)}` | ||
) | ||
const responseJson = await response.json() | ||
const downloadCount = responseJson.downloads | ||
return { downloadCount } | ||
} | ||
|
||
const getGemStats = async packageName => { | ||
const response = await fetch( | ||
`https://rubygems.org/api/v1/gems/${encodeURIComponent(packageName)}.json` | ||
) | ||
const responseJson = await response.json() | ||
const downloadCount = responseJson.downloads | ||
return { downloadCount } | ||
} | ||
|
||
const sortLibs = async libs => { | ||
let totalStars = 0; | ||
const libsWithScores = await Promise.all( | ||
libs.map(async lib => { | ||
const [ | ||
npmStats = {}, | ||
gemStars = {}, | ||
githubStats = {}, | ||
] = await Promise.all([ | ||
lib.npm && getNpmStats(lib.npm), | ||
lib.gem && getGemStats(lib.gem), | ||
lib.github && getGitHubStats(lib.github), | ||
]) | ||
const result = { | ||
...lib, | ||
...npmStats, | ||
...gemStars, | ||
...githubStats, | ||
} | ||
totalStars += result.stars || 0; | ||
return result; | ||
}) | ||
) | ||
const sortedLibs = libsWithScores.sort((a, b) => { | ||
let aScore = 0, | ||
bScore = 0 | ||
if ("downloadCount" in a && 'downloadCount' in b) { | ||
if (a.downloadCount > b.downloadCount) { | ||
aScore += 40 | ||
} else if (b.downloadCount > a.downloadCount) { | ||
bScore += 40 | ||
} | ||
} | ||
if ("hasCommitsInLast3Months" in a && a.hasCommitsInLast3Months) { | ||
aScore += 30 | ||
} | ||
if ("hasCommitsInLast3Months" in b && b.hasCommitsInLast3Months) { | ||
bScore += 30 | ||
} | ||
if ('stars' in a && 'stars' in b) { | ||
if (a.stars > b.stars) { | ||
aScore += 40 | ||
} else if (a.stars < b.stars) { | ||
bScore += 40 | ||
} | ||
} | ||
if (bScore > aScore) { | ||
return 1 | ||
} else if (bScore < aScore) { | ||
return -1 | ||
} | ||
return 0 | ||
}) | ||
return { sortedLibs, totalStars } | ||
} | ||
|
||
module.exports = sortLibs |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This section again is continuing to get chunks of hard to grok code without the reasoning for why. I think this should probably have comments - and more likely I think that async could probably just be moved to a bootstrapping phrase.