2018-09-30 11:37:19 +01:00
|
|
|
const _ = require('lodash');
|
|
|
|
|
2018-11-16 18:22:52 +00:00
|
|
|
const {
|
|
|
|
getChallengesForLang,
|
|
|
|
createChallenge,
|
2018-11-18 21:04:04 +03:00
|
|
|
getChallengesDirForLang
|
2018-11-16 18:22:52 +00:00
|
|
|
} = require('../../curriculum/getChallenges');
|
|
|
|
const utils = require('./');
|
2018-10-09 16:46:14 +05:30
|
|
|
const { locale } = require('../config/env.json');
|
2018-09-30 11:37:19 +01:00
|
|
|
|
|
|
|
const dasherize = utils.dasherize;
|
|
|
|
const nameify = utils.nameify;
|
|
|
|
|
|
|
|
const arrToString = arr =>
|
|
|
|
Array.isArray(arr) ? arr.join('\n') : _.toString(arr);
|
|
|
|
|
2018-11-18 21:04:04 +03:00
|
|
|
exports.localeChallengesRootDir = getChallengesDirForLang(locale);
|
2018-11-16 18:22:52 +00:00
|
|
|
|
|
|
|
exports.replaceChallengeNode = function replaceChallengeNode(fullFilePath) {
|
2018-11-18 21:04:04 +03:00
|
|
|
return createChallenge(fullFilePath);
|
2018-11-16 18:22:52 +00:00
|
|
|
};
|
|
|
|
|
2018-10-04 14:47:55 +01:00
|
|
|
exports.buildChallenges = async function buildChallenges() {
|
2018-11-16 18:22:52 +00:00
|
|
|
const curriculum = await getChallengesForLang(locale);
|
2018-10-04 14:47:55 +01:00
|
|
|
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-09-30 11:37:19 +01:00
|
|
|
|
2018-11-16 18:22:52 +00:00
|
|
|
const builtChallenges = blocks
|
|
|
|
.filter(block => !block.isPrivate)
|
|
|
|
.map(({ meta, challenges }) => {
|
|
|
|
const {
|
|
|
|
order,
|
|
|
|
time,
|
|
|
|
template,
|
|
|
|
required,
|
|
|
|
superBlock,
|
|
|
|
superOrder,
|
|
|
|
isPrivate,
|
|
|
|
dashedName: blockDashedName,
|
|
|
|
fileName
|
|
|
|
} = meta;
|
2018-09-30 11:37:19 +01:00
|
|
|
|
2018-11-16 18:22:52 +00:00
|
|
|
return challenges.map(challenge => {
|
|
|
|
challenge.name = nameify(challenge.title);
|
2018-09-30 11:37:19 +01:00
|
|
|
|
2018-11-16 18:22:52 +00:00
|
|
|
challenge.dashedName = dasherize(challenge.name);
|
2018-09-30 11:37:19 +01:00
|
|
|
|
2018-11-16 18:22:52 +00:00
|
|
|
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.fileName = fileName;
|
|
|
|
challenge.order = order;
|
|
|
|
challenge.block = blockDashedName;
|
|
|
|
challenge.isPrivate = challenge.isPrivate || isPrivate;
|
|
|
|
challenge.isRequired = !!challenge.isRequired;
|
|
|
|
challenge.time = time;
|
|
|
|
challenge.superOrder = superOrder;
|
|
|
|
challenge.superBlock = superBlock
|
|
|
|
.split('-')
|
|
|
|
.map(word => _.capitalize(word))
|
|
|
|
.join(' ');
|
|
|
|
challenge.required = required;
|
|
|
|
challenge.template = template;
|
|
|
|
return challenge;
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.reduce((accu, current) => accu.concat(current), []);
|
2018-10-04 14:47:55 +01:00
|
|
|
return builtChallenges;
|
2018-09-30 11:37:19 +01:00
|
|
|
};
|