| 
									
										
										
										
											2018-11-16 18:22:52 +00:00
										 |  |  | const chokidar = require('chokidar'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const { createChallengeNode } = require('./create-Challenge-nodes'); | 
					
						
							| 
									
										
										
										
											2018-09-30 11:37:19 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-23 14:18:46 +01:00
										 |  |  | exports.sourceNodes = function sourceChallengesSourceNodes( | 
					
						
							|  |  |  |   { actions, reporter }, | 
					
						
							|  |  |  |   pluginOptions | 
					
						
							|  |  |  | ) { | 
					
						
							| 
									
										
										
										
											2018-11-16 18:22:52 +00:00
										 |  |  |   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 objects to the plugin | 
					
						
							|  |  |  |     `);
 | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   if (typeof onSourceChange !== 'function') { | 
					
						
							| 
									
										
										
										
											2018-09-30 11:37:19 +01:00
										 |  |  |     reporter.panic(`
 | 
					
						
							| 
									
										
										
										
											2018-11-16 18:22:52 +00:00
										 |  |  |     "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 | 
					
						
							|  |  |  |     `);
 | 
					
						
							| 
									
										
										
										
											2018-09-30 11:37:19 +01:00
										 |  |  |   } | 
					
						
							|  |  |  |   const { createNode } = actions; | 
					
						
							| 
									
										
										
										
											2018-11-16 18:22:52 +00:00
										 |  |  |   const watcher = chokidar.watch(curriculumPath, { | 
					
						
							|  |  |  |     ignored: /(^|[\/\\])\../, | 
					
						
							|  |  |  |     persistent: true | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   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 | 
					
						
							|  |  |  |   ); | 
					
						
							| 
									
										
										
										
											2018-09-30 11:37:19 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-16 18:22:52 +00:00
										 |  |  |   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
 | 
					
						
							| 
									
										
										
										
											2018-09-30 11:37:19 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-04 14:47:55 +01:00
										 |  |  |   ${e.message} | 
					
						
							| 
									
										
										
										
											2018-09-30 11:37:19 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-04 14:47:55 +01:00
										 |  |  |   `)
 | 
					
						
							| 
									
										
										
										
											2018-11-16 18:22:52 +00:00
										 |  |  |       ); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2018-09-30 11:37:19 +01:00
										 |  |  | }; |