freeCodeCamp/client/utils/buildChallenges.js

64 lines
1.8 KiB
JavaScript
Raw Normal View History

const _ = require('lodash');
2018-11-16 18:22:52 +00:00
const {
getChallengesForLang,
createChallenge,
getChallengesDirForLang
2018-11-16 18:22:52 +00:00
} = require('../../curriculum/getChallenges');
const utils = require('./');
const { locale } = require('../config/env.json');
const { blockNameify } = require('./blockNameify');
const dasherize = utils.dasherize;
const nameify = utils.nameify;
const arrToString = arr =>
Array.isArray(arr) ? arr.join('\n') : _.toString(arr);
exports.localeChallengesRootDir = getChallengesDirForLang(locale);
2018-11-16 18:22:52 +00:00
exports.replaceChallengeNode = async function replaceChallengeNode(
fullFilePath
) {
return prepareChallenge(await createChallenge(fullFilePath));
};
2018-11-16 18:22:52 +00:00
exports.buildChallenges = async function buildChallenges() {
2018-11-16 18:22:52 +00:00
const curriculum = await getChallengesForLang(locale);
const superBlocks = Object.keys(curriculum);
const blocks = superBlocks
.map(superBlock => curriculum[superBlock].blocks)
.reduce((blocks, superBlock) => {
const currentBlocks = Object.keys(superBlock).map(key => superBlock[key]);
return blocks.concat(_.flatten(currentBlocks));
}, []);
2018-11-16 18:22:52 +00:00
const builtChallenges = blocks
.filter(block => !block.isPrivate)
2018-11-18 18:16:09 +00:00
.map(({ challenges }) => challenges.map(prepareChallenge))
2018-11-16 18:22:52 +00:00
.reduce((accu, current) => accu.concat(current), []);
return builtChallenges;
};
2018-11-18 18:16:09 +00:00
function prepareChallenge(challenge) {
challenge.name = nameify(challenge.title);
if (challenge.files) {
challenge.files = _.reduce(
challenge.files,
(map, file) => {
map[file.key] = {
...file,
head: arrToString(file.head),
contents: arrToString(file.contents),
tail: arrToString(file.tail)
};
return map;
},
{}
);
}
challenge.block = dasherize(challenge.block);
challenge.superBlock = blockNameify(challenge.superBlock);
2018-11-18 18:16:09 +00:00
return challenge;
}