freeCodeCamp/client/utils/build-challenges.js

53 lines
1.7 KiB
JavaScript
Raw Normal View History

const path = require('path');
const _ = require('lodash');
const envData = require('../../config/env.json');
2018-11-16 18:22:52 +00:00
const {
getChallengesForLang,
generateChallengeCreator,
CHALLENGES_DIR,
META_DIR,
getChallengesDirForLang
2018-11-16 18:22:52 +00:00
} = require('../../curriculum/getChallenges');
2022-03-22 06:05:38 +00:00
const { curriculumLocale } = envData;
exports.localeChallengesRootDir = getChallengesDirForLang(curriculumLocale);
2018-11-16 18:22:52 +00:00
2020-09-02 09:31:10 +00:00
exports.replaceChallengeNode = () => {
return async function replaceChallengeNode(filePath) {
// get the meta so that challengeOrder is accurate
const blockNameRe = /\d\d-[-\w]+\/([^/]+)\//;
const posix = path.normalize(filePath).split(path.sep).join(path.posix.sep);
const blockName = posix.match(blockNameRe)[1];
2022-05-06 11:44:14 +00:00
const metaPath = path.resolve(META_DIR, `${blockName}/meta.json`);
delete require.cache[require.resolve(metaPath)];
const meta = require(metaPath);
// TODO: reimplement hot-reloading of certifications
return await createChallenge(filePath, meta);
};
};
2018-11-16 18:22:52 +00:00
const createChallenge = generateChallengeCreator(
CHALLENGES_DIR,
curriculumLocale
);
exports.buildChallenges = async function buildChallenges() {
const curriculum = await getChallengesForLang(curriculumLocale);
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)
.map(({ challenges }) => challenges)
2018-11-16 18:22:52 +00:00
.reduce((accu, current) => accu.concat(current), []);
return builtChallenges;
};