freeCodeCamp/seed/getChallenges.js

65 lines
1.6 KiB
JavaScript
Raw Normal View History

2015-12-07 05:44:34 +00:00
/* eslint-disable no-self-compare */
2015-11-02 01:20:03 +00:00
var fs = require('fs');
var path = require('path');
function getFilesFor(dir) {
2015-12-05 04:55:12 +00:00
return fs.readdirSync(path.join(__dirname, '/' + dir))
.map(function(file) {
2015-12-05 05:06:36 +00:00
let superBlock;
2015-12-05 04:55:12 +00:00
if (fs.statSync(path.join(__dirname, dir + '/' + file)).isFile()) {
2015-12-05 05:06:36 +00:00
return { file: file };
2015-12-05 04:55:12 +00:00
}
2015-12-05 05:06:36 +00:00
superBlock = file;
return getFilesFor(dir + '/' + superBlock)
.map(function(data) {
return {
file: superBlock + '/' + data.file,
superBlock: superBlock
};
2015-12-05 04:55:12 +00:00
});
})
.reduce(function(files, file) {
if (!Array.isArray(file)) {
files.push(file);
return files;
}
return files.concat(file);
}, []);
2015-11-02 01:20:03 +00:00
}
2015-12-07 05:44:34 +00:00
function getSupOrder(filePath) {
var order = parseInt((filePath || '').split('-')[0], 10);
// check for NaN
if (order !== order) {
return 0;
}
return order;
}
function getSupName(filePath) {
var order = parseInt((filePath || '').split('-')[0], 10);
// check for NaN
if (order !== order) {
return filePath;
}
return (filePath || '').split('-').splice(1).join('-');
}
2015-11-02 01:20:03 +00:00
module.exports = function getChallenges() {
try {
return getFilesFor('challenges')
2015-12-05 05:06:36 +00:00
.map(function(data) {
var challengeSpec = require('./challenges/' + data.file);
challengeSpec.fileName = data.file;
2015-12-07 05:44:34 +00:00
challengeSpec.superBlock = getSupName(data.superBlock);
2015-12-07 06:41:28 +00:00
challengeSpec.superOrder = getSupOrder(data.superBlock);
return challengeSpec;
2015-11-02 01:20:03 +00:00
});
} catch (e) {
console.log('error', e);
return [];
}
};