Skip to content
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

[www/starters] tweaks to fix DX for no GH token on www #8718

Merged
merged 1 commit into from
Oct 2, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
172 changes: 86 additions & 86 deletions www/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -444,18 +444,18 @@ exports.onCreateNode = ({ node, actions, getNode, getNodes }) => {
// Default fields are to avoid graphql errors.
const { owner, name: repoStub } = parseGHUrl(node.repo)
const defaultFields = {
slug: ``,
stub: ``,
slug: `/${repoStub}/`,
stub: repoStub,
name: ``,
description: ``,
stars: 0,
lastUpdated: ``,
owner: ``,
githubFullName: ``,
gatsbyMajorVersion: ``,
allDependencies: [],
gatsbyDependencies: [],
miscDependencies: [],
gatsbyMajorVersion: [[`no data`, `0`]],
allDependencies: [[`no data`, `0`]],
gatsbyDependencies: [[`no data`, `0`]],
miscDependencies: [[`no data`, `0`]],
}

if (!process.env.GITHUB_API_TOKEN) {
Expand All @@ -466,92 +466,92 @@ exports.onCreateNode = ({ node, actions, getNode, getNodes }) => {
...defaultFields,
},
})
}

Promise.all([
getpkgjson(node.repo),
githubApiClient.request(`
query {
repository(owner:"${owner}", name:"${repoStub}") {
name
stargazers {
totalCount
}
createdAt
updatedAt
owner {
login
} else {
Promise.all([
getpkgjson(node.repo),
githubApiClient.request(`
query {
repository(owner:"${owner}", name:"${repoStub}") {
name
stargazers {
totalCount
}
createdAt
updatedAt
owner {
login
}
nameWithOwner
}
nameWithOwner
}
`),
])
.then(results => {
const [pkgjson, githubData] = results
const {
stargazers: { totalCount: stars },
updatedAt: lastUpdated,
owner: { login: owner },
name,
nameWithOwner: githubFullName,
} = githubData.repository

const { dependencies = [], devDependencies = [] } = pkgjson
const allDependencies = Object.entries(dependencies).concat(
Object.entries(devDependencies)
)

const gatsbyMajorVersion = allDependencies
.filter(([key, _]) => key === `gatsby`)
.map(version => {
let [gatsby, versionNum] = version
if (versionNum === `latest` || versionNum === `next`) {
return [gatsby, `2`]
}
return [gatsby, versionNum.replace(/\D/g, ``).charAt(0)]
})

// If a new field is added here, make sure a corresponding
// change is made to "defaultFields" to not break DX
const starterShowcaseFields = {
slug: `/${repoStub}/`,
stub: repoStub,
name,
description: pkgjson.description,
stars,
lastUpdated,
owner,
githubFullName,
gatsbyMajorVersion,
allDependencies,
gatsbyDependencies: allDependencies
.filter(
([key, _]) => ![`gatsby-cli`, `gatsby-link`].includes(key) // remove stuff everyone has
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: but the second arg (_) isn't used here at all, right?

)
.filter(([key, _]) => key.includes(`gatsby`)),
miscDependencies: allDependencies.filter(
([key, _]) => !key.includes(`gatsby`)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same in these other spots.

),
}
`),
])
.then(results => {
const [pkgjson, githubData] = results
const {
stargazers: { totalCount: stars },
updatedAt: lastUpdated,
owner: { login: owner },
name,
nameWithOwner: githubFullName,
} = githubData.repository

const { dependencies = [], devDependencies = [] } = pkgjson
const allDependencies = Object.entries(dependencies).concat(
Object.entries(devDependencies)
)

const gatsbyMajorVersion = allDependencies
.filter(([key, _]) => key === `gatsby`)
.map(version => {
let [gatsby, versionNum] = version
if (versionNum === `latest` || versionNum === `next`) {
return [gatsby, `2`]
}
return [gatsby, versionNum.replace(/\D/g, ``).charAt(0)]
createNodeField({
node,
name: `starterShowcase`,
value: starterShowcaseFields,
})

// If a new field is added here, make sure a corresponding
// change is made to "defaultFields" to not break DX
const starterShowcaseFields = {
slug: `/${repoStub}/`,
stub: repoStub,
name,
description: pkgjson.description,
stars,
lastUpdated,
owner,
githubFullName,
gatsbyMajorVersion,
allDependencies,
gatsbyDependencies: allDependencies
.filter(
([key, _]) => ![`gatsby-cli`, `gatsby-link`].includes(key) // remove stuff everyone has
)
.filter(([key, _]) => key.includes(`gatsby`)),
miscDependencies: allDependencies.filter(
([key, _]) => !key.includes(`gatsby`)
),
}
createNodeField({
node,
name: `starterShowcase`,
value: starterShowcaseFields,
})
})
.catch(err => {
console.log(
`\nError getting repo data. Your GitHub token may be invalid`
)
return createNodeField({
node,
name: `starterShowcase`,
value: {
...defaultFields,
},
.catch(err => {
console.log(
`\nError getting repo data. Your GitHub token may be invalid`
)
return createNodeField({
node,
name: `starterShowcase`,
value: {
...defaultFields,
},
})
})
})
}
}

// Community/Creators Pages
Expand Down
10 changes: 1 addition & 9 deletions www/src/views/starter-library/starter-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,7 @@ const StartersList = ({ urlState, starters, count, sortRecent }) => {
stars,
stub,
} = starter.fields.starterShowcase
const { url: demoUrl, repo: repoUrl } = starter
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! Removed some client side code in favor of injecting it, correct? 🎉

const gatsbyVersion = gatsbyDependencies.find(
([k, v]) => k === `gatsby`
)[1]
const match = gatsbyVersion.match(/([0-9]+)([.])([0-9]+)/) // we just want x.x
const minorVersion = match ? match[0] : gatsbyVersion // default to version if no match
const isGatsbyVersionWarning = !/(2..+|next|latest)/g.test(
minorVersion
) // either 2.x or next or latest
const { url: demoUrl } = starter

return (
starter.fields && ( // have to filter out null fields from bad data
Expand Down