freeCodeCamp/seed/getChallenges.js

68 lines
1.7 KiB
JavaScript
Raw Normal View History

2015-12-07 05:44:34 +00:00
/* eslint-disable no-self-compare */
// no import here as this runs without babel
const fs = require('fs');
const path = require('path');
2015-11-02 01:20:03 +00:00
const hiddenFile = /^(\.|\/\.)/g;
2015-11-02 01:20:03 +00:00
function getFilesFor(dir) {
2015-12-05 04:55:12 +00:00
return fs.readdirSync(path.join(__dirname, '/' + dir))
.filter(file => !hiddenFile.test(file))
2015-12-05 04:55:12 +00:00
.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) {
const order = parseInt((filePath || '').split('-')[0], 10);
2015-12-07 05:44:34 +00:00
// check for NaN
if (order !== order) {
return 0;
}
return order;
}
function getSupName(filePath) {
const order = parseInt((filePath || '').split('-')[0], 10);
2015-12-07 05:44:34 +00:00
// 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) {
const challengeSpec = require('./challenges/' + data.file);
2015-12-05 05:06:36 +00:00
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.error('error: ', e);
2015-11-02 01:20:03 +00:00
return [];
}
};