fix(client): ensure validation works during watch (#38936)

This commit is contained in:
Oliver Eyton-Williams
2020-05-28 17:24:29 +02:00
committed by GitHub
parent 3567813c51
commit d233cb35a3
3 changed files with 10 additions and 11 deletions

View File

@ -34,7 +34,7 @@ module.exports = {
options: { options: {
name: 'challenges', name: 'challenges',
source: buildChallenges, source: buildChallenges,
onSourceChange: replaceChallengeNode, onSourceChange: replaceChallengeNode(config.locale),
curriculumPath: localeChallengesRootDir curriculumPath: localeChallengesRootDir
} }
}, },

View File

@ -14,10 +14,10 @@ const arrToString = arr =>
exports.localeChallengesRootDir = getChallengesDirForLang(locale); exports.localeChallengesRootDir = getChallengesDirForLang(locale);
exports.replaceChallengeNode = async function replaceChallengeNode( exports.replaceChallengeNode = locale => {
fullFilePath return async function replaceChallengeNode(fullFilePath) {
) { return prepareChallenge(await createChallenge(fullFilePath, null, locale));
return prepareChallenge(await createChallenge(fullFilePath)); };
}; };
exports.buildChallenges = async function buildChallenges() { exports.buildChallenges = async function buildChallenges() {

View File

@ -28,7 +28,6 @@ exports.getMetaForBlock = getMetaForBlock;
exports.getChallengesForLang = function getChallengesForLang(lang) { exports.getChallengesForLang = function getChallengesForLang(lang) {
let curriculum = {}; let curriculum = {};
const validate = challengeSchemaValidator(lang);
return new Promise(resolve => { return new Promise(resolve => {
let running = 1; let running = 1;
function done() { function done() {
@ -39,13 +38,13 @@ exports.getChallengesForLang = function getChallengesForLang(lang) {
readDirP({ root: getChallengesDirForLang(lang) }) readDirP({ root: getChallengesDirForLang(lang) })
.on('data', file => { .on('data', file => {
running++; running++;
buildCurriculum(file, curriculum, validate).then(done); buildCurriculum(file, curriculum, lang).then(done);
}) })
.on('end', done); .on('end', done);
}); });
}; };
async function buildCurriculum(file, curriculum, validate) { async function buildCurriculum(file, curriculum, lang) {
const { name, depth, path: filePath, fullPath, stat } = file; const { name, depth, path: filePath, fullPath, stat } = file;
if (depth === 1 && stat.isDirectory()) { if (depth === 1 && stat.isDirectory()) {
// extract the superBlock info // extract the superBlock info
@ -81,12 +80,12 @@ async function buildCurriculum(file, curriculum, validate) {
} }
const { meta } = challengeBlock; const { meta } = challengeBlock;
const challenge = await createChallenge(fullPath, meta, validate); const challenge = await createChallenge(fullPath, meta, lang);
challengeBlock.challenges = [...challengeBlock.challenges, challenge]; challengeBlock.challenges = [...challengeBlock.challenges, challenge];
} }
async function createChallenge(fullPath, maybeMeta, validate) { async function createChallenge(fullPath, maybeMeta, lang) {
let meta; let meta;
if (maybeMeta) { if (maybeMeta) {
meta = maybeMeta; meta = maybeMeta;
@ -99,7 +98,7 @@ async function createChallenge(fullPath, maybeMeta, validate) {
} }
const { name: superBlock } = superBlockInfoFromFullPath(fullPath); const { name: superBlock } = superBlockInfoFromFullPath(fullPath);
const challenge = await parseMarkdown(fullPath); const challenge = await parseMarkdown(fullPath);
const result = validate(challenge); const result = challengeSchemaValidator(lang)(challenge);
if (result.error) { if (result.error) {
console.log(result.value); console.log(result.value);
throw new Error(result.error); throw new Error(result.error);