diff --git a/client/plugins/fcc-source-challenges/gatsby-node.js b/client/plugins/fcc-source-challenges/gatsby-node.js index 0f4c71b68d..fb305ba6ea 100644 --- a/client/plugins/fcc-source-challenges/gatsby-node.js +++ b/client/plugins/fcc-source-challenges/gatsby-node.js @@ -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)) diff --git a/client/utils/buildChallenges.js b/client/utils/buildChallenges.js index badab86bfa..c704a5f1db 100644 --- a/client/utils/buildChallenges.js +++ b/client/utils/buildChallenges.js @@ -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 + ); }; };