forked from freeCodeCamp/freeCodeCamp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetChallenges.js
67 lines (61 loc) · 1.71 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
/* eslint-disable no-self-compare */
// no import here as this runs without babel
const fs = require('fs');
const path = require('path');
const hiddenFile = /(^(\.|\/\.))|(.md$)/g;
function getFilesFor(dir) {
return fs.readdirSync(path.join(__dirname, '/' + dir))
.filter(file => !hiddenFile.test(file))
.map(function(file) {
let superBlock;
if (fs.statSync(path.join(__dirname, dir + '/' + file)).isFile()) {
return { file: file };
}
superBlock = file;
return getFilesFor(dir + '/' + superBlock)
.map(function(data) {
return {
file: superBlock + '/' + data.file,
superBlock: superBlock
};
});
})
.reduce(function(files, file) {
if (!Array.isArray(file)) {
files.push(file);
return files;
}
return files.concat(file);
}, []);
}
function getSupOrder(filePath) {
const order = parseInt((filePath || '').split('-')[0], 10);
// check for NaN
if (order !== order) {
return 0;
}
return order;
}
function getSupName(filePath) {
const order = parseInt((filePath || '').split('-')[0], 10);
// check for NaN
if (order !== order) {
return filePath;
}
return (filePath || '').split('-').splice(1).join('-');
}
module.exports = function getChallenges() {
try {
return getFilesFor('challenges')
.map(function(data) {
const challengeSpec = require('./challenges/' + data.file);
challengeSpec.fileName = data.file;
challengeSpec.superBlock = getSupName(data.superBlock);
challengeSpec.superOrder = getSupOrder(data.superBlock);
return challengeSpec;
});
} catch (e) {
console.error('error: ', e);
return [];
}
};