fix(client): handle challenge creation (#42272)
This lets us create new challenges/steps without having to restart the client
This commit is contained in:
committed by
GitHub
parent
052173e502
commit
1db9a123ae
@ -1,4 +1,6 @@
|
|||||||
|
const path = require('path');
|
||||||
const chokidar = require('chokidar');
|
const chokidar = require('chokidar');
|
||||||
|
const readdirp = require('readdirp');
|
||||||
|
|
||||||
const { createChallengeNode } = require('./create-challenge-nodes');
|
const { createChallengeNode } = require('./create-challenge-nodes');
|
||||||
|
|
||||||
@ -28,6 +30,7 @@ exports.sourceNodes = function sourceChallengesSourceNodes(
|
|||||||
const { createNode } = actions;
|
const { createNode } = actions;
|
||||||
const watcher = chokidar.watch(curriculumPath, {
|
const watcher = chokidar.watch(curriculumPath, {
|
||||||
ignored: /(^|[\/\\])\../,
|
ignored: /(^|[\/\\])\../,
|
||||||
|
ignoreInitial: true,
|
||||||
persistent: true,
|
persistent: true,
|
||||||
usePolling: true,
|
usePolling: true,
|
||||||
cwd: curriculumPath
|
cwd: curriculumPath
|
||||||
@ -55,6 +58,43 @@ File changed at ${filePath}, replacing challengeNode id ${challenge.id}
|
|||||||
: null
|
: 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() {
|
function sourceAndCreateNodes() {
|
||||||
return source()
|
return source()
|
||||||
.then(challenges => Promise.all(challenges))
|
.then(challenges => Promise.all(challenges))
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
const _ = require('lodash');
|
const _ = require('lodash');
|
||||||
|
const path = require('path');
|
||||||
|
|
||||||
const {
|
const {
|
||||||
getChallengesForLang,
|
getChallengesForLang,
|
||||||
@ -14,7 +15,21 @@ exports.localeChallengesRootDir = getChallengesDirForLang(curriculumLocale);
|
|||||||
|
|
||||||
exports.replaceChallengeNode = () => {
|
exports.replaceChallengeNode = () => {
|
||||||
return async function replaceChallengeNode(filePath) {
|
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
|
||||||
|
);
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user