freeCodeCamp/curriculum/getChallenges.js

124 lines
3.7 KiB
JavaScript
Raw Normal View History

const path = require('path');
const { findIndex } = require('lodash');
const readDirP = require('readdirp-walk');
const { parseMarkdown } = require('@freecodecamp/challenge-md-parser');
const { dasherize } = require('./utils');
2018-11-16 18:22:52 +00:00
const { locale } = require('../config/env.json');
const challengesDir = path.resolve(__dirname, './challenges');
2018-11-16 18:22:52 +00:00
const localeChallengesRootDir = path.resolve(challengesDir, `./${locale}`);
const metaDir = path.resolve(challengesDir, '_meta');
exports.challengesDir = challengesDir;
exports.localeChallengesRootDir = localeChallengesRootDir;
exports.metaDir = metaDir;
exports.getChallengesForLang = function getChallengesForLang(lang) {
let curriculum = {};
return new Promise(resolve =>
readDirP({ root: path.resolve(challengesDir, `./${lang}`) })
.on('data', file => buildCurriculum(file, curriculum))
.on('end', () => resolve(curriculum))
);
};
async function buildCurriculum(file, curriculum) {
2018-11-16 18:22:52 +00:00
const { name, depth, path: filePath, stat } = file;
if (depth === 1 && stat.isDirectory()) {
// extract the superBlock info
const { order, name: superBlock } = superBlockInfo(name);
curriculum[superBlock] = { superBlock, order, blocks: {} };
return;
}
if (depth === 2 && stat.isDirectory()) {
const blockName = getBlockNameFromPath(filePath);
const metaPath = path.resolve(
__dirname,
`./challenges/_meta/${blockName}/meta.json`
);
const blockMeta = require(metaPath);
const { name: superBlock } = superBlockInfoFromPath(filePath);
const blockInfo = { meta: blockMeta, challenges: [] };
curriculum[superBlock].blocks[name] = blockInfo;
return;
}
2018-10-10 20:20:40 +00:00
if (name === 'meta.json' || name === '.DS_Store') {
return;
}
2018-11-16 18:22:52 +00:00
const block = getBlockNameFromPath(filePath);
const { name: superBlock } = superBlockInfoFromPath(filePath);
2018-10-10 20:20:40 +00:00
let challengeBlock;
try {
challengeBlock = curriculum[superBlock].blocks[block];
} catch (e) {
console.log(superBlock, block);
2018-10-25 09:54:57 +00:00
// eslint-disable-next-line no-process-exit
2018-10-10 20:20:40 +00:00
process.exit(0);
}
const { meta } = challengeBlock;
2018-11-16 18:22:52 +00:00
const challenge = await createChallenge(filePath, meta);
challengeBlock.challenges = [...challengeBlock.challenges, challenge];
}
async function createChallenge(challengeFilePath, maybeMeta) {
const fullPath = path.resolve(localeChallengesRootDir, challengeFilePath);
const metaPath = path.resolve(
metaDir,
`./${getBlockNameFromFullPath(fullPath)}/meta.json`
);
let meta;
if (maybeMeta) {
meta = maybeMeta;
} else {
meta = require(metaPath);
}
const { name: superBlock } = superBlockInfoFromPath(challengeFilePath);
const challenge = await parseMarkdown(fullPath);
const challengeOrder = findIndex(
meta.challengeOrder,
([id]) => id === challenge.id
);
const { name: blockName, order, superOrder } = meta;
challenge.block = blockName;
challenge.dashedName = dasherize(challenge.title);
challenge.order = order;
challenge.superOrder = superOrder;
challenge.superBlock = superBlock;
challenge.challengeOrder = challengeOrder;
2018-11-16 18:22:52 +00:00
return challenge;
}
2018-11-16 18:22:52 +00:00
exports.createChallenge = createChallenge;
function superBlockInfoFromPath(filePath) {
2018-10-25 09:54:57 +00:00
const [maybeSuper] = filePath.split(path.sep);
return superBlockInfo(maybeSuper);
2015-11-02 01:20:03 +00:00
}
function superBlockInfo(fileName) {
const [maybeOrder, ...superBlock] = fileName.split('-');
let order = parseInt(maybeOrder, 10);
if (isNaN(order)) {
return { order: 0, name: fileName };
} else {
return {
order: order,
name: superBlock.join('-')
};
2015-12-07 05:44:34 +00:00
}
}
function getBlockNameFromPath(filePath) {
2018-10-25 09:54:57 +00:00
const [, block] = filePath.split(path.sep);
return block;
}
2018-11-16 18:22:52 +00:00
function getBlockNameFromFullPath(fullFilePath) {
const [, block] = fullFilePath.split(path.sep).reverse();
return block;
}