From 4c1b8ce52c68615748a0a8fbb40896dade0b82ff Mon Sep 17 00:00:00 2001 From: Valeriy Date: Sun, 18 Nov 2018 21:04:04 +0300 Subject: [PATCH] fix: replace localeChallengesRootDir to getChallengesDirForLang --- client/utils/buildChallenges.js | 11 +++-------- curriculum/getChallenges.js | 33 ++++++++++++++++++++------------- 2 files changed, 23 insertions(+), 21 deletions(-) diff --git a/client/utils/buildChallenges.js b/client/utils/buildChallenges.js index 44a19873dd..1d00fb49e4 100644 --- a/client/utils/buildChallenges.js +++ b/client/utils/buildChallenges.js @@ -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() { diff --git a/curriculum/getChallenges.js b/curriculum/getChallenges.js index 0f44922ecf..0d105e5483 100644 --- a/curriculum/getChallenges.js +++ b/curriculum/getChallenges.js @@ -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);