forked from freeCodeCamp/freeCodeCamp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
getChallenges.js
151 lines (134 loc) · 4.17 KB
/
getChallenges.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
const path = require('path');
const { findIndex } = require('lodash');
const readDirP = require('readdirp-walk');
const { parseMarkdown } = require('@freecodecamp/challenge-md-parser');
const { dasherize } = require('./utils');
const challengesDir = path.resolve(__dirname, './challenges');
const metaDir = path.resolve(challengesDir, '_meta');
exports.challengesDir = challengesDir;
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 => {
let running = 1;
function done() {
if (--running === 0) {
resolve(curriculum);
}
}
readDirP({ root: getChallengesDirForLang(lang) })
.on('data', file => {
running++;
buildCurriculum(file, curriculum).then(done);
})
.on('end', done);
});
};
async function buildCurriculum(file, curriculum) {
const { name, depth, path: filePath, fullPath, 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;
}
if (name === 'meta.json' || name === '.DS_Store') {
return;
}
const block = getBlockNameFromPath(filePath);
const { name: superBlock } = superBlockInfoFromPath(filePath);
let challengeBlock;
try {
challengeBlock = curriculum[superBlock].blocks[block];
} catch (e) {
console.log(superBlock, block);
// eslint-disable-next-line no-process-exit
process.exit(0);
}
const { meta } = challengeBlock;
const challenge = await createChallenge(fullPath, meta);
challengeBlock.challenges = [...challengeBlock.challenges, challenge];
}
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 } = superBlockInfoFromFullPath(fullPath);
const challenge = await parseMarkdown(fullPath);
const challengeOrder = findIndex(
meta.challengeOrder,
([id]) => id === challenge.id
);
const {
name: blockName,
order,
superOrder,
isPrivate,
required = [],
template,
time
} = meta;
challenge.block = blockName;
challenge.dashedName = dasherize(challenge.title);
challenge.order = order;
challenge.superOrder = superOrder;
challenge.superBlock = superBlock;
challenge.challengeOrder = challengeOrder;
challenge.isPrivate = challenge.isPrivate || isPrivate;
challenge.required = required.concat(challenge.required || []);
challenge.template = template;
challenge.time = time;
return challenge;
}
exports.createChallenge = createChallenge;
function superBlockInfoFromPath(filePath) {
const [maybeSuper] = filePath.split(path.sep);
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);
if (isNaN(order)) {
return { order: 0, name: fileName };
} else {
return {
order: order,
name: superBlock.join('-')
};
}
}
function getBlockNameFromPath(filePath) {
const [, block] = filePath.split(path.sep);
return block;
}
function getBlockNameFromFullPath(fullFilePath) {
const [, block] = fullFilePath.split(path.sep).reverse();
return block;
}