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,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}
`)
);
);
}
};