fix(client): handle challenge creation (#42272)

This lets us create new challenges/steps without having to restart the
client
pull/42652/head
Oliver Eyton-Williams 2021-06-28 10:08:11 +02:00 committed by GitHub
parent 052173e502
commit 1db9a123ae
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 56 additions and 1 deletions

View File

@ -1,4 +1,6 @@
const path = require('path');
const chokidar = require('chokidar');
const readdirp = require('readdirp');
const { createChallengeNode } = require('./create-challenge-nodes');
@ -28,6 +30,7 @@ exports.sourceNodes = function sourceChallengesSourceNodes(
const { createNode } = actions;
const watcher = chokidar.watch(curriculumPath, {
ignored: /(^|[\/\\])\../,
ignoreInitial: true,
persistent: true,
usePolling: true,
cwd: curriculumPath
@ -55,6 +58,43 @@ File changed at ${filePath}, replacing challengeNode id ${challenge.id}
: null
);
// if a file is added, that might change the order of the challenges in the
// containing block, so we recreate them all
watcher.on('add', filePath => {
if (/\.md?$/.test(filePath)) {
const blockPath = path.dirname(filePath);
const fullBlockPath = path.join(
__dirname,
'../../../curriculum/challenges/english/',
blockPath
);
readdirp(fullBlockPath, { fileFilter: '*.md' })
.on('data', entry => {
const { path: siblingPath } = entry;
const relativePath = path.join(blockPath, siblingPath);
onSourceChange(relativePath)
.then(challenge => {
reporter.info(
`
File changed at ${relativePath}, replacing challengeNode id ${challenge.id}
`
);
createVisibleChallenge(challenge);
})
.catch(e =>
reporter.error(`fcc-replace-challenge
attempting to replace ${relativePath}
${e.message}
`)
);
})
.on('warn', error => console.error('non-fatal error', error))
.on('error', error => console.error('fatal error', error));
}
});
function sourceAndCreateNodes() {
return source()
.then(challenges => Promise.all(challenges))

View File

@ -1,4 +1,5 @@
const _ = require('lodash');
const path = require('path');
const {
getChallengesForLang,
@ -14,7 +15,21 @@ exports.localeChallengesRootDir = getChallengesDirForLang(curriculumLocale);
exports.replaceChallengeNode = () => {
return async function replaceChallengeNode(filePath) {
return await createChallenge(challengesDir, filePath, curriculumLocale);
// get the meta so that challengeOrder is accurate
const blockNameRe = /\d\d-[-\w]+\/([^/]+)\//;
const blockName = filePath.match(blockNameRe)[1];
const metaPath = path.resolve(
__dirname,
`../../curriculum/challenges/_meta/${blockName}/meta.json`
);
delete require.cache[require.resolve(metaPath)];
const meta = require(metaPath);
return await createChallenge(
challengesDir,
filePath,
curriculumLocale,
meta
);
};
};