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

@ -1,10 +1,9 @@
const path = require('path');
const _ = require('lodash'); const _ = require('lodash');
const { const {
getChallengesForLang, getChallengesForLang,
createChallenge, createChallenge,
localeChallengesRootDir getChallengesDirForLang
} = require('../../curriculum/getChallenges'); } = require('../../curriculum/getChallenges');
const utils = require('./'); const utils = require('./');
const { locale } = require('../config/env.json'); const { locale } = require('../config/env.json');
@ -15,14 +14,10 @@ const nameify = utils.nameify;
const arrToString = arr => const arrToString = arr =>
Array.isArray(arr) ? arr.join('\n') : _.toString(arr); Array.isArray(arr) ? arr.join('\n') : _.toString(arr);
exports.localeChallengesRootDir = localeChallengesRootDir; exports.localeChallengesRootDir = getChallengesDirForLang(locale);
exports.replaceChallengeNode = function replaceChallengeNode(fullFilePath) { exports.replaceChallengeNode = function replaceChallengeNode(fullFilePath) {
const relativeChallengePath = fullFilePath.replace( return createChallenge(fullFilePath);
localeChallengesRootDir + path.sep,
''
);
return createChallenge(relativeChallengePath);
}; };
exports.buildChallenges = async function buildChallenges() { exports.buildChallenges = async function buildChallenges() {

View File

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