| 
									
										
										
										
											2015-12-06 21:44:34 -08:00
										 |  |  | /* eslint-disable no-self-compare */ | 
					
						
							| 
									
										
										
										
											2017-01-18 15:16:23 -08:00
										 |  |  | // no import here as this runs without babel
 | 
					
						
							|  |  |  | const fs = require('fs'); | 
					
						
							|  |  |  | const path = require('path'); | 
					
						
							| 
									
										
										
										
											2015-11-01 17:20:03 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-05 13:41:25 +07:00
										 |  |  | const hiddenFile = /(^(\.|\/\.))|(.md$)/g; | 
					
						
							| 
									
										
										
										
											2018-01-19 14:03:17 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-01 17:20:03 -08:00
										 |  |  | function getFilesFor(dir) { | 
					
						
							| 
									
										
										
										
											2018-01-19 14:03:17 -05:00
										 |  |  |   let targetDir = path.join(__dirname, dir); | 
					
						
							|  |  |  |   return fs.readdirSync(targetDir) | 
					
						
							| 
									
										
										
										
											2017-01-18 15:16:23 -08:00
										 |  |  |     .filter(file => !hiddenFile.test(file)) | 
					
						
							| 
									
										
										
										
											2015-12-04 20:55:12 -08:00
										 |  |  |     .map(function(file) { | 
					
						
							| 
									
										
										
										
											2015-12-04 21:06:36 -08:00
										 |  |  |       let superBlock; | 
					
						
							| 
									
										
										
										
											2018-01-19 14:03:17 -05:00
										 |  |  |       if (fs.statSync(path.join(targetDir, file)).isFile()) { | 
					
						
							|  |  |  |         return {file: file}; | 
					
						
							| 
									
										
										
										
											2015-12-04 20:55:12 -08:00
										 |  |  |       } | 
					
						
							| 
									
										
										
										
											2015-12-04 21:06:36 -08:00
										 |  |  |       superBlock = file; | 
					
						
							| 
									
										
										
										
											2018-01-19 14:03:17 -05:00
										 |  |  |       return getFilesFor(path.join(dir, superBlock)) | 
					
						
							| 
									
										
										
										
											2015-12-04 21:06:36 -08:00
										 |  |  |         .map(function(data) { | 
					
						
							|  |  |  |           return { | 
					
						
							| 
									
										
										
										
											2018-01-19 14:03:17 -05:00
										 |  |  |             file: path.join(superBlock, data.file), | 
					
						
							| 
									
										
										
										
											2015-12-04 21:06:36 -08:00
										 |  |  |             superBlock: superBlock | 
					
						
							|  |  |  |           }; | 
					
						
							| 
									
										
										
										
											2015-12-04 20:55:12 -08:00
										 |  |  |         }); | 
					
						
							|  |  |  |     }) | 
					
						
							| 
									
										
										
										
											2018-01-19 14:03:17 -05:00
										 |  |  |     .reduce(function(files, entry) { | 
					
						
							|  |  |  |       return files.concat(entry); | 
					
						
							| 
									
										
										
										
											2015-12-04 20:55:12 -08:00
										 |  |  |     }, []); | 
					
						
							| 
									
										
										
										
											2015-11-01 17:20:03 -08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-19 14:03:17 -05:00
										 |  |  | function superblockInfo(filePath) { | 
					
						
							|  |  |  |   let parts = (filePath || '').split('-'); | 
					
						
							|  |  |  |   let order = parseInt(parts[0], 10); | 
					
						
							|  |  |  |   if (isNaN(order)) { | 
					
						
							|  |  |  |     return {order: 0, name: filePath}; | 
					
						
							|  |  |  |   } else { | 
					
						
							|  |  |  |     return { | 
					
						
							|  |  |  |       order: order, | 
					
						
							|  |  |  |       name: parts.splice(1).join('-') | 
					
						
							|  |  |  |     }; | 
					
						
							| 
									
										
										
										
											2015-12-06 21:44:34 -08:00
										 |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-19 14:03:17 -05:00
										 |  |  | module.exports = function getChallenges(challengesDir) { | 
					
						
							|  |  |  |   if (!challengesDir) { | 
					
						
							|  |  |  |     challengesDir = 'challenges'; | 
					
						
							| 
									
										
										
										
											2015-11-01 17:20:03 -08:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2018-01-19 14:03:17 -05:00
										 |  |  |   return getFilesFor(challengesDir) | 
					
						
							|  |  |  |     .map(function(data) { | 
					
						
							|  |  |  |       const challengeSpec = require('./' + challengesDir + '/' + data.file); | 
					
						
							|  |  |  |       let superInfo = superblockInfo(data.superBlock); | 
					
						
							|  |  |  |       challengeSpec.fileName = data.file; | 
					
						
							|  |  |  |       challengeSpec.superBlock = superInfo.name; | 
					
						
							|  |  |  |       challengeSpec.superOrder = superInfo.order; | 
					
						
							|  |  |  |       return challengeSpec; | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2015-11-01 17:20:03 -08:00
										 |  |  | }; |