fix: replace localeChallengesRootDir to getChallengesDirForLang
This commit is contained in:
@ -1,10 +1,9 @@
|
||||
const path = require('path');
|
||||
const _ = require('lodash');
|
||||
|
||||
const {
|
||||
getChallengesForLang,
|
||||
createChallenge,
|
||||
localeChallengesRootDir
|
||||
getChallengesDirForLang
|
||||
} = require('../../curriculum/getChallenges');
|
||||
const utils = require('./');
|
||||
const { locale } = require('../config/env.json');
|
||||
@ -15,14 +14,10 @@ const nameify = utils.nameify;
|
||||
const arrToString = arr =>
|
||||
Array.isArray(arr) ? arr.join('\n') : _.toString(arr);
|
||||
|
||||
exports.localeChallengesRootDir = localeChallengesRootDir;
|
||||
exports.localeChallengesRootDir = getChallengesDirForLang(locale);
|
||||
|
||||
exports.replaceChallengeNode = function replaceChallengeNode(fullFilePath) {
|
||||
const relativeChallengePath = fullFilePath.replace(
|
||||
localeChallengesRootDir + path.sep,
|
||||
''
|
||||
);
|
||||
return createChallenge(relativeChallengePath);
|
||||
return createChallenge(fullFilePath);
|
||||
};
|
||||
|
||||
exports.buildChallenges = async function buildChallenges() {
|
||||
|
@ -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);
|
||||
|
Reference in New Issue
Block a user