freeCodeCamp/client/plugins/fcc-source-challenges/create-challenge-nodes.js

50 lines
1.3 KiB
JavaScript
Raw Normal View History

2018-03-26 12:01:24 +00:00
const crypto = require('crypto');
2018-11-16 18:22:52 +00:00
function createChallengeNode(challenge, reporter) {
// challengeType 11 is for video challenges (they only have instructions)
// challengeType 7 is for certificates (they only have tests)
// TODO: either handle empty descriptions inside Gatsby OR ensure that
// description defaults to '' when creating challenges.
// ditto for seeds and instructions.
// create-md should, then, not create empty seed, description or instruction
// sections.
if (
typeof challenge.description !== 'string' &&
challenge.challengeType !== 11 &&
challenge.challengeType !== 7
) {
reporter.warn(`
2018-03-26 12:01:24 +00:00
2018-04-06 13:51:52 +00:00
${challenge.title} has a description that will break things!
`);
2018-03-26 12:01:24 +00:00
}
2018-04-06 13:51:52 +00:00
const contentDigest = crypto
.createHash('md5')
.update(JSON.stringify(challenge))
.digest('hex');
const internal = {
contentDigest,
type: challenge.challengeType === 7 ? 'CertificateNode' : 'ChallengeNode'
2018-04-06 13:51:52 +00:00
};
/* eslint-disable prefer-object-spread/prefer-object-spread */
return JSON.parse(
JSON.stringify(
Object.assign(
{},
{
children: [],
parent: null,
internal,
sourceInstanceName: 'challenge'
},
challenge
2018-04-06 13:51:52 +00:00
)
)
);
}
2018-03-26 12:01:24 +00:00
2018-11-16 18:22:52 +00:00
exports.createChallengeNode = createChallengeNode;