fix: replace localeChallengesRootDir to getChallengesDirForLang

This commit is contained in:
Valeriy
2018-11-18 21:04:04 +03:00
committed by Stuart Taylor
parent b7d4fd7349
commit 4c1b8ce52c
2 changed files with 23 additions and 21 deletions

View File

@ -4,26 +4,29 @@ const readDirP = require('readdirp-walk');
const { parseMarkdown } = require('@freecodecamp/challenge-md-parser');
const { dasherize } = require('./utils');
const { locale } = require('../config/env.json');
const challengesDir = path.resolve(__dirname, './challenges');
const localeChallengesRootDir = path.resolve(challengesDir, `./${locale}`);
const metaDir = path.resolve(challengesDir, '_meta');
exports.challengesDir = challengesDir;
exports.localeChallengesRootDir = localeChallengesRootDir;
exports.metaDir = metaDir;
function getChallengesDirForLang(lang) {
return path.resolve(challengesDir, `./${lang}`);
}
exports.getChallengesDirForLang = getChallengesDirForLang;
exports.getChallengesForLang = function getChallengesForLang(lang) {
let curriculum = {};
return new Promise(resolve =>
readDirP({ root: path.resolve(challengesDir, `./${lang}`) })
readDirP({ root: getChallengesDirForLang(lang) })
.on('data', file => buildCurriculum(file, curriculum))
.on('end', () => resolve(curriculum))
);
};
async function buildCurriculum(file, curriculum) {
const { name, depth, path: filePath, stat } = file;
const { name, depth, path: filePath, fullPath, stat } = file;
if (depth === 1 && stat.isDirectory()) {
// extract the superBlock info
const { order, name: superBlock } = superBlockInfo(name);
@ -58,24 +61,23 @@ async function buildCurriculum(file, curriculum) {
}
const { meta } = challengeBlock;
const challenge = await createChallenge(filePath, meta);
const challenge = await createChallenge(fullPath, 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`
);
async function createChallenge(fullPath, maybeMeta) {
let meta;
if (maybeMeta) {
meta = maybeMeta;
} else {
const metaPath = path.resolve(
metaDir,
`./${getBlockNameFromFullPath(fullPath)}/meta.json`
);
meta = require(metaPath);
}
const { name: superBlock } = superBlockInfoFromPath(challengeFilePath);
const { name: superBlock } = superBlockInfoFromFullPath(fullPath);
const challenge = await parseMarkdown(fullPath);
const challengeOrder = findIndex(
meta.challengeOrder,
@ -99,6 +101,11 @@ function superBlockInfoFromPath(filePath) {
return superBlockInfo(maybeSuper);
}
function superBlockInfoFromFullPath(fullFilePath) {
const [,, maybeSuper] = fullFilePath.split(path.sep).reverse();
return superBlockInfo(maybeSuper);
}
function superBlockInfo(fileName) {
const [maybeOrder, ...superBlock] = fileName.split('-');
let order = parseInt(maybeOrder, 10);