feat: watch challenges (#34321)

This commit is contained in:
Stuart Taylor
2018-11-16 18:22:52 +00:00
committed by mrugesh mohapatra
parent 82ec250c75
commit cee98aef43
12 changed files with 173 additions and 87 deletions

View File

@@ -1,6 +1,10 @@
const path = require('path');
const { buildChallenges } = require('./utils/buildChallenges');
const {
buildChallenges,
replaceChallengeNode,
localeChallengesRootDir
} = require('./utils/buildChallenges');
const { NODE_ENV: env, LOCALE: locale = 'english' } = process.env;
@@ -36,7 +40,9 @@ module.exports = {
resolve: 'fcc-source-challenges',
options: {
name: 'challenges',
source: buildChallenges
source: buildChallenges,
onSourceChange: replaceChallengeNode,
curriculumPath: localeChallengesRootDir
}
},
{

View File

@@ -13,7 +13,6 @@
"@fortawesome/free-regular-svg-icons": "^5.2.0",
"@fortawesome/free-solid-svg-icons": "^5.2.0",
"@fortawesome/react-fontawesome": "0.0.20",
"@freecodecamp/curriculum": "0.0.0-next.4",
"@freecodecamp/react-bootstrap": "^0.32.3",
"@reach/router": "^1.1.1",
"axios": "^0.18.0",
@@ -82,6 +81,7 @@
},
"devDependencies": {
"babel-plugin-transform-imports": "^1.5.0",
"chokidar": "^2.0.4",
"eslint": "^5.5.0",
"eslint-config-freecodecamp": "^1.1.1",
"jest": "^23.6.0",

View File

@@ -1,6 +1,6 @@
const crypto = require('crypto');
function createChallengeNodes(challenge, reporter) {
function createChallengeNode(challenge, reporter) {
if (typeof challenge.description[0] !== 'string') {
reporter.warn(`
@@ -35,4 +35,4 @@ function createChallengeNodes(challenge, reporter) {
);
}
exports.createChallengeNodes = createChallengeNodes;
exports.createChallengeNode = createChallengeNode;

View File

@@ -1,31 +1,70 @@
const { createChallengeNodes } = require('./create-Challenge-nodes');
const chokidar = require('chokidar');
const { createChallengeNode } = require('./create-Challenge-nodes');
exports.sourceNodes = function sourceChallengesSourceNodes(
{ actions, reporter },
pluginOptions
) {
if (typeof pluginOptions.source !== 'function') {
const { source, onSourceChange, curriculumPath } = pluginOptions;
if (typeof source !== 'function') {
reporter.panic(`
"source" is a required option for fcc-source-challenges. It must be a function
that delivers challenge files to the plugin
`);
"source" is a required option for fcc-source-challenges. It must be a
function that delivers challenge objects to the plugin
`);
}
if (typeof onSourceChange !== 'function') {
reporter.panic(`
"onSourceChange" is a required option for fcc-source-challenges. It must be
a function that delivers a new challenge object to the plugin
`);
}
if (typeof curriculumPath !== 'string') {
reporter.panic(`
"curriculumPath" is a required option for fcc-source-challenges. It must be
a path to a curriculum directory
`);
}
// TODO: Add live seed updates
const { createNode } = actions;
const watcher = chokidar.watch(curriculumPath, {
ignored: /(^|[\/\\])\../,
persistent: true
});
const { source } = pluginOptions;
return source()
.then(challenges =>
challenges
.filter(challenge => challenge.superBlock !== 'Certificates')
.map(challenge => createChallengeNodes(challenge, reporter))
.map(node => createNode(node))
)
.catch(e =>
reporter.panic(`fcc-source-challenges
watcher.on('ready', sourceAndCreateNodes).on(
'change',
filePath =>
(/\.md$/).test(filePath)
? onSourceChange(filePath)
.then(challenge => {
reporter.info(
`File changed at ${filePath}, replacing challengeNode id ${
challenge.id
}`
);
return createChallengeNode(challenge, reporter);
})
.then(createNode)
: null
);
function sourceAndCreateNodes() {
return source()
.then(challenges => Promise.all(challenges))
.then(challenges =>
challenges
.filter(
challenge => challenge.superBlock.toLowerCase() !== 'certificates'
)
.map(challenge => createChallengeNode(challenge, reporter))
.map(node => createNode(node))
)
.catch(e =>
reporter.panic(`fcc-source-challenges
${e.message}
`)
);
);
}
};

View File

@@ -1,7 +1,12 @@
const { getChallengesForLang } = require('@freecodecamp/curriculum');
const path = require('path');
const _ = require('lodash');
const utils = require('../utils');
const {
getChallengesForLang,
createChallenge,
localeChallengesRootDir
} = require('../../curriculum/getChallenges');
const utils = require('./');
const { locale } = require('../config/env.json');
const dasherize = utils.dasherize;
@@ -10,8 +15,18 @@ const nameify = utils.nameify;
const arrToString = arr =>
Array.isArray(arr) ? arr.join('\n') : _.toString(arr);
exports.localeChallengesRootDir = localeChallengesRootDir;
exports.replaceChallengeNode = function replaceChallengeNode(fullFilePath) {
const relativeChallengePath = fullFilePath.replace(
localeChallengesRootDir + path.sep,
''
);
return createChallenge(relativeChallengePath);
};
exports.buildChallenges = async function buildChallenges() {
const curriculum = await getChallengesForLang( locale );
const curriculum = await getChallengesForLang(locale);
const superBlocks = Object.keys(curriculum);
const blocks = superBlocks
.map(superBlock => curriculum[superBlock].blocks)
@@ -20,54 +35,57 @@ exports.buildChallenges = async function buildChallenges() {
return blocks.concat(_.flatten(currentBlocks));
}, []);
const builtChallenges = blocks.filter(block => !block.isPrivate).map(({ meta, challenges }) => {
const {
order,
time,
template,
required,
superBlock,
superOrder,
isPrivate,
dashedName: blockDashedName,
fileName
} = meta;
const builtChallenges = blocks
.filter(block => !block.isPrivate)
.map(({ meta, challenges }) => {
const {
order,
time,
template,
required,
superBlock,
superOrder,
isPrivate,
dashedName: blockDashedName,
fileName
} = meta;
return challenges.map(challenge => {
challenge.name = nameify(challenge.title);
return challenges.map(challenge => {
challenge.name = nameify(challenge.title);
challenge.dashedName = dasherize(challenge.name);
challenge.dashedName = dasherize(challenge.name);
if (challenge.files) {
challenge.files = _.reduce(
challenge.files,
(map, file) => {
map[file.key] = {
...file,
head: arrToString(file.head),
contents: arrToString(file.contents),
tail: arrToString(file.tail)
};
return map;
},
{}
);
}
challenge.fileName = fileName;
challenge.order = order;
challenge.block = blockDashedName;
challenge.isPrivate = challenge.isPrivate || isPrivate;
challenge.isRequired = !!challenge.isRequired;
challenge.time = time;
challenge.superOrder = superOrder;
challenge.superBlock = superBlock
.split('-')
.map(word => _.capitalize(word))
.join(' ');
challenge.required = required;
challenge.template = template;
return challenge;
});
}).reduce((accu, current) => accu.concat(current), [])
if (challenge.files) {
challenge.files = _.reduce(
challenge.files,
(map, file) => {
map[file.key] = {
...file,
head: arrToString(file.head),
contents: arrToString(file.contents),
tail: arrToString(file.tail)
};
return map;
},
{}
);
}
challenge.fileName = fileName;
challenge.order = order;
challenge.block = blockDashedName;
challenge.isPrivate = challenge.isPrivate || isPrivate;
challenge.isRequired = !!challenge.isRequired;
challenge.time = time;
challenge.superOrder = superOrder;
challenge.superBlock = superBlock
.split('-')
.map(word => _.capitalize(word))
.join(' ');
challenge.required = required;
challenge.template = template;
return challenge;
});
})
.reduce((accu, current) => accu.concat(current), []);
return builtChallenges;
};