mirror of
https://github.com/freeCodeCamp/freeCodeCamp.git
synced 2026-05-01 01:00:36 -04:00
refactor(client): clean up gatsby-node (#55640)
Co-authored-by: Shaun Hamilton <shauhami020@gmail.com>
This commit is contained in:
committed by
GitHub
parent
149d6b52fa
commit
97279f5bc6
@@ -32,7 +32,11 @@ exports.onCreateNode = function onCreateNode({ node, actions, getNode }) {
|
||||
}
|
||||
};
|
||||
|
||||
exports.createPages = function createPages({ graphql, actions, reporter }) {
|
||||
exports.createPages = async function createPages({
|
||||
graphql,
|
||||
actions,
|
||||
reporter
|
||||
}) {
|
||||
if (!env.algoliaAPIKey || !env.algoliaAppId) {
|
||||
if (process.env.FREECODECAMP_NODE_ENV === 'production') {
|
||||
throw new Error(
|
||||
@@ -57,201 +61,140 @@ exports.createPages = function createPages({ graphql, actions, reporter }) {
|
||||
|
||||
const { createPage } = actions;
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
// Query for all markdown 'nodes' and for the slug we previously created.
|
||||
resolve(
|
||||
graphql(`
|
||||
{
|
||||
allChallengeNode(
|
||||
sort: {
|
||||
fields: [
|
||||
challenge___superOrder
|
||||
challenge___order
|
||||
challenge___challengeOrder
|
||||
]
|
||||
}
|
||||
) {
|
||||
edges {
|
||||
node {
|
||||
challenge {
|
||||
block
|
||||
certification
|
||||
challengeType
|
||||
dashedName
|
||||
disableLoopProtectTests
|
||||
disableLoopProtectPreview
|
||||
fields {
|
||||
slug
|
||||
blockHashSlug
|
||||
}
|
||||
fillInTheBlank {
|
||||
sentence
|
||||
blanks {
|
||||
answer
|
||||
feedback
|
||||
}
|
||||
}
|
||||
hasEditableBoundaries
|
||||
id
|
||||
msTrophyId
|
||||
order
|
||||
prerequisites {
|
||||
id
|
||||
title
|
||||
}
|
||||
required {
|
||||
link
|
||||
src
|
||||
}
|
||||
challengeOrder
|
||||
challengeFiles {
|
||||
name
|
||||
ext
|
||||
contents
|
||||
head
|
||||
tail
|
||||
history
|
||||
fileKey
|
||||
}
|
||||
solutions {
|
||||
contents
|
||||
ext
|
||||
history
|
||||
}
|
||||
superBlock
|
||||
superOrder
|
||||
template
|
||||
usesMultifileEditor
|
||||
scene {
|
||||
setup {
|
||||
background
|
||||
characters {
|
||||
character
|
||||
position {
|
||||
x
|
||||
y
|
||||
z
|
||||
}
|
||||
}
|
||||
audio {
|
||||
filename
|
||||
startTime
|
||||
startTimestamp
|
||||
finishTimestamp
|
||||
}
|
||||
alwaysShowDialogue
|
||||
}
|
||||
commands {
|
||||
background
|
||||
character
|
||||
position {
|
||||
x
|
||||
y
|
||||
z
|
||||
}
|
||||
startTime
|
||||
finishTime
|
||||
dialogue {
|
||||
text
|
||||
align
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
const result = await graphql(`
|
||||
{
|
||||
allChallengeNode(
|
||||
sort: {
|
||||
fields: [
|
||||
challenge___superOrder
|
||||
challenge___order
|
||||
challenge___challengeOrder
|
||||
]
|
||||
}
|
||||
) {
|
||||
edges {
|
||||
node {
|
||||
challenge {
|
||||
block
|
||||
certification
|
||||
challengeType
|
||||
dashedName
|
||||
disableLoopProtectTests
|
||||
disableLoopProtectPreview
|
||||
fields {
|
||||
slug
|
||||
blockHashSlug
|
||||
}
|
||||
}
|
||||
}
|
||||
allMarkdownRemark {
|
||||
edges {
|
||||
node {
|
||||
fields {
|
||||
slug
|
||||
nodeIdentity
|
||||
component
|
||||
}
|
||||
frontmatter {
|
||||
certification
|
||||
block
|
||||
superBlock
|
||||
title
|
||||
}
|
||||
htmlAst
|
||||
id
|
||||
excerpt
|
||||
id
|
||||
order
|
||||
required {
|
||||
link
|
||||
src
|
||||
}
|
||||
challengeOrder
|
||||
challengeFiles {
|
||||
name
|
||||
ext
|
||||
contents
|
||||
head
|
||||
tail
|
||||
history
|
||||
fileKey
|
||||
}
|
||||
solutions {
|
||||
contents
|
||||
ext
|
||||
history
|
||||
}
|
||||
superBlock
|
||||
superOrder
|
||||
template
|
||||
usesMultifileEditor
|
||||
}
|
||||
}
|
||||
}
|
||||
`).then(result => {
|
||||
if (result.errors) {
|
||||
console.log(result.errors);
|
||||
return reject(result.errors);
|
||||
}
|
||||
|
||||
// Create challenge pages.
|
||||
result.data.allChallengeNode.edges.forEach(
|
||||
createChallengePages(createPage)
|
||||
);
|
||||
|
||||
const blocks = uniq(
|
||||
result.data.allChallengeNode.edges.map(
|
||||
({
|
||||
node: {
|
||||
challenge: { block }
|
||||
}
|
||||
}) => block
|
||||
)
|
||||
);
|
||||
|
||||
const superBlocks = uniq(
|
||||
result.data.allChallengeNode.edges.map(
|
||||
({
|
||||
node: {
|
||||
challenge: { superBlock }
|
||||
}
|
||||
}) => superBlock
|
||||
)
|
||||
);
|
||||
|
||||
// Create intro pages
|
||||
// TODO: Remove allMarkdownRemark (populate from elsewhere)
|
||||
result.data.allMarkdownRemark.edges.forEach(edge => {
|
||||
const {
|
||||
node: { frontmatter, fields }
|
||||
} = edge;
|
||||
|
||||
if (!fields) {
|
||||
return;
|
||||
}
|
||||
const { slug, nodeIdentity } = fields;
|
||||
if (slug.includes('LICENCE')) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
if (nodeIdentity === 'blockIntroMarkdown') {
|
||||
if (!blocks.includes(frontmatter.block)) {
|
||||
return;
|
||||
}
|
||||
} else if (!superBlocks.includes(frontmatter.superBlock)) {
|
||||
return;
|
||||
}
|
||||
allMarkdownRemark {
|
||||
edges {
|
||||
node {
|
||||
fields {
|
||||
slug
|
||||
nodeIdentity
|
||||
component
|
||||
}
|
||||
const pageBuilder = createByIdentityMap[nodeIdentity](createPage);
|
||||
pageBuilder(edge);
|
||||
} catch (e) {
|
||||
console.log(e);
|
||||
console.log(`
|
||||
frontmatter {
|
||||
certification
|
||||
block
|
||||
superBlock
|
||||
title
|
||||
}
|
||||
htmlAst
|
||||
id
|
||||
excerpt
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
`);
|
||||
|
||||
// Create challenge pages.
|
||||
result.data.allChallengeNode.edges.forEach(createChallengePages(createPage));
|
||||
|
||||
const blocks = uniq(
|
||||
result.data.allChallengeNode.edges.map(
|
||||
({
|
||||
node: {
|
||||
challenge: { block }
|
||||
}
|
||||
}) => block
|
||||
)
|
||||
);
|
||||
|
||||
const superBlocks = uniq(
|
||||
result.data.allChallengeNode.edges.map(
|
||||
({
|
||||
node: {
|
||||
challenge: { superBlock }
|
||||
}
|
||||
}) => superBlock
|
||||
)
|
||||
);
|
||||
|
||||
// Create intro pages
|
||||
// TODO: Remove allMarkdownRemark (populate from elsewhere)
|
||||
result.data.allMarkdownRemark.edges.forEach(edge => {
|
||||
const {
|
||||
node: { frontmatter, fields }
|
||||
} = edge;
|
||||
|
||||
if (!fields) {
|
||||
return;
|
||||
}
|
||||
const { slug, nodeIdentity } = fields;
|
||||
if (slug.includes('LICENCE')) {
|
||||
return;
|
||||
}
|
||||
if (nodeIdentity === 'blockIntroMarkdown') {
|
||||
if (!blocks.includes(frontmatter.block)) {
|
||||
return;
|
||||
}
|
||||
} else if (!superBlocks.includes(frontmatter.superBlock)) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const pageBuilder = createByIdentityMap[nodeIdentity](createPage);
|
||||
pageBuilder(edge);
|
||||
} catch (e) {
|
||||
console.log(e);
|
||||
console.log(`
|
||||
ident: ${nodeIdentity} does not belong to a function
|
||||
|
||||
${frontmatter ? JSON.stringify(edge.node) : 'no frontmatter'}
|
||||
|
||||
|
||||
`);
|
||||
}
|
||||
});
|
||||
|
||||
return null;
|
||||
})
|
||||
);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
@@ -146,6 +146,9 @@ exports.createChallengePages = function (createPage) {
|
||||
};
|
||||
};
|
||||
|
||||
// TODO: figure out a cleaner way to get the last challenge in a block. Create
|
||||
// it during the curriculum build process and attach it to the first challenge?
|
||||
// That would remove the need to analyse allChallengeEdges.
|
||||
function getProjectPreviewConfig(challenge, allChallengeEdges) {
|
||||
const { block, challengeOrder, challengeType, usesMultifileEditor } =
|
||||
challenge;
|
||||
|
||||
Reference in New Issue
Block a user