freeCodeCamp/getChallenges.js
Alex Chaffee 590f646263 feat(seed): "unpack" and "repack" scripts
add "npm run seed" as alias to "node seed"

unpack tests and solution into HTML file; add titles and help text; style unpacked file

enable running unpacked assert tests in browser

Using browserify, compile "tape", "lodash", jQuery into "unpacked-bundle.js" for use during in-browser unpacked tests

feat(seed): diff after repacking

feat(seed): unpacked tests use Browser TAP chrome dev tool if available
2018-04-03 19:52:56 +05:30

58 lines
1.5 KiB
JavaScript

/* 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) {
let targetDir = path.join(__dirname, dir);
return fs.readdirSync(targetDir)
.filter(file => !hiddenFile.test(file))
.map(function(file) {
let superBlock;
if (fs.statSync(path.join(targetDir, file)).isFile()) {
return {file: file};
}
superBlock = file;
return getFilesFor(path.join(dir, superBlock))
.map(function(data) {
return {
file: path.join(superBlock, data.file),
superBlock: superBlock
};
});
})
.reduce(function(files, entry) {
return files.concat(entry);
}, []);
}
function superblockInfo(filePath) {
let parts = (filePath || '').split('-');
let order = parseInt(parts[0], 10);
if (isNaN(order)) {
return {order: 0, name: filePath};
} else {
return {
order: order,
name: parts.splice(1).join('-')
};
}
}
module.exports = function getChallenges(challengesDir) {
if (!challengesDir) {
challengesDir = 'challenges';
}
return getFilesFor(challengesDir)
.map(function(data) {
const challengeSpec = require('./' + challengesDir + '/' + data.file);
let superInfo = superblockInfo(data.superBlock);
challengeSpec.fileName = data.file;
challengeSpec.superBlock = superInfo.name;
challengeSpec.superOrder = superInfo.order;
return challengeSpec;
});
};