| 
									
										
										
										
											2015-06-02 20:32:10 -07:00
										 |  |  | /* eslint-disable no-process-exit */ | 
					
						
							| 
									
										
										
										
											2016-01-20 09:03:58 -08:00
										 |  |  | require('babel-register'); | 
					
						
							| 
									
										
										
										
											2015-06-02 20:32:10 -07:00
										 |  |  | require('dotenv').load(); | 
					
						
							| 
									
										
										
										
											2016-01-30 00:13:41 -08:00
										 |  |  | var adler32 = require('adler32'); | 
					
						
							| 
									
										
										
										
											2015-10-20 19:53:08 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-01 17:20:03 -08:00
										 |  |  | var Rx = require('rx'), | 
					
						
							| 
									
										
										
										
											2015-08-08 07:50:35 -07:00
										 |  |  |     _ = require('lodash'), | 
					
						
							| 
									
										
										
										
											2015-11-01 17:20:03 -08:00
										 |  |  |     getChallenges = require('./getChallenges'), | 
					
						
							| 
									
										
										
										
											2015-10-20 19:53:08 -07:00
										 |  |  |     app = require('../server/server'); | 
					
						
							| 
									
										
										
										
											2015-06-02 20:32:10 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-11 19:51:08 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-02 20:32:10 -07:00
										 |  |  | var Challenge = app.models.Challenge; | 
					
						
							| 
									
										
										
										
											2015-10-20 19:53:08 -07:00
										 |  |  | var destroy = Rx.Observable.fromNodeCallback(Challenge.destroyAll, Challenge); | 
					
						
							|  |  |  | var create = Rx.Observable.fromNodeCallback(Challenge.create, Challenge); | 
					
						
							| 
									
										
										
										
											2015-06-02 20:32:10 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-20 19:53:08 -07:00
										 |  |  | destroy() | 
					
						
							| 
									
										
										
										
											2015-11-01 17:20:03 -08:00
										 |  |  |   .flatMap(function() { return Rx.Observable.from(getChallenges()); }) | 
					
						
							|  |  |  |   .flatMap(function(challengeSpec) { | 
					
						
							| 
									
										
										
										
											2015-08-09 07:40:16 -07:00
										 |  |  |     var order = challengeSpec.order; | 
					
						
							| 
									
										
										
										
											2015-08-09 22:14:31 -07:00
										 |  |  |     var block = challengeSpec.name; | 
					
						
							| 
									
										
										
										
											2015-12-04 21:06:36 -08:00
										 |  |  |     var superBlock = challengeSpec.superBlock; | 
					
						
							| 
									
										
										
										
											2015-12-06 22:41:28 -08:00
										 |  |  |     var superOrder = challengeSpec.superOrder; | 
					
						
							| 
									
										
										
										
											2015-10-06 22:37:08 -07:00
										 |  |  |     var isBeta = !!challengeSpec.isBeta; | 
					
						
							| 
									
										
										
										
											2015-12-06 22:41:28 -08:00
										 |  |  |     var isComingSoon = !!challengeSpec.isComingSoon; | 
					
						
							| 
									
										
										
										
											2015-11-09 17:27:56 -08:00
										 |  |  |     var fileName = challengeSpec.fileName; | 
					
						
							| 
									
										
										
										
											2016-01-15 00:55:08 -08:00
										 |  |  |     var helpRoom = challengeSpec.helpRoom || 'Help'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-01 17:20:03 -08:00
										 |  |  |     console.log('parsed %s successfully', block); | 
					
						
							| 
									
										
										
										
											2015-08-09 22:14:31 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-10 12:14:38 -07:00
										 |  |  |     // challenge file has no challenges...
 | 
					
						
							|  |  |  |     if (challengeSpec.challenges.length === 0) { | 
					
						
							| 
									
										
										
										
											2015-10-20 19:53:08 -07:00
										 |  |  |       return Rx.Observable.just([{ block: 'empty ' + block }]); | 
					
						
							| 
									
										
										
										
											2015-08-10 12:14:38 -07:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-08 07:50:35 -07:00
										 |  |  |     var challenges = challengeSpec.challenges | 
					
						
							| 
									
										
										
										
											2015-08-09 07:40:16 -07:00
										 |  |  |       .map(function(challenge, index) { | 
					
						
							| 
									
										
										
										
											2016-01-11 22:47:49 -08:00
										 |  |  |         challenge.name = challenge.title.replace(/[^a-zA-Z0-9\s]/g, ''); | 
					
						
							| 
									
										
										
										
											2015-08-09 22:14:31 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-09 07:40:16 -07:00
										 |  |  |         challenge.dashedName = challenge.name | 
					
						
							|  |  |  |           .toLowerCase() | 
					
						
							|  |  |  |           .replace(/\:/g, '') | 
					
						
							|  |  |  |           .replace(/\s/g, '-'); | 
					
						
							| 
									
										
										
										
											2015-11-09 17:27:56 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-30 00:13:41 -08:00
										 |  |  |         challenge.checksum = adler32.sum( | 
					
						
							|  |  |  |           Buffer(challenge.title + | 
					
						
							|  |  |  |             JSON.stringify(challenge.description) + | 
					
						
							|  |  |  |             JSON.stringify(challenge.challengeSeed) + | 
					
						
							|  |  |  |             JSON.stringify(challenge.tests))); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-09 17:27:56 -08:00
										 |  |  |         challenge.fileName = fileName; | 
					
						
							| 
									
										
										
										
											2016-01-15 00:55:08 -08:00
										 |  |  |         challenge.helpRoom = helpRoom; | 
					
						
							| 
									
										
										
										
											2015-08-10 23:29:32 -07:00
										 |  |  |         challenge.order = order; | 
					
						
							|  |  |  |         challenge.suborder = index + 1; | 
					
						
							| 
									
										
										
										
											2015-08-09 22:14:31 -07:00
										 |  |  |         challenge.block = block; | 
					
						
							| 
									
										
										
										
											2015-10-06 22:37:08 -07:00
										 |  |  |         challenge.isBeta = challenge.isBeta || isBeta; | 
					
						
							| 
									
										
										
										
											2015-12-06 22:41:28 -08:00
										 |  |  |         challenge.isComingSoon = challenge.isComingSoon || isComingSoon; | 
					
						
							| 
									
										
										
										
											2015-10-12 22:56:10 -07:00
										 |  |  |         challenge.time = challengeSpec.time; | 
					
						
							| 
									
										
										
										
											2015-12-06 22:41:28 -08:00
										 |  |  |         challenge.superOrder = superOrder; | 
					
						
							| 
									
										
										
										
											2015-12-04 22:37:15 -08:00
										 |  |  |         challenge.superBlock = superBlock | 
					
						
							|  |  |  |           .split('-') | 
					
						
							|  |  |  |           .map(function(word) { | 
					
						
							|  |  |  |             return _.capitalize(word); | 
					
						
							|  |  |  |           }) | 
					
						
							|  |  |  |           .join(' '); | 
					
						
							| 
									
										
										
										
											2015-08-09 22:14:31 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-13 18:30:51 -07:00
										 |  |  |         return challenge; | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-20 19:53:08 -07:00
										 |  |  |     return create(challenges); | 
					
						
							|  |  |  |   }) | 
					
						
							|  |  |  |   .subscribe( | 
					
						
							|  |  |  |     function(challenges) { | 
					
						
							|  |  |  |       console.log('%s successfully saved', challenges[0].block); | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |     function(err) { throw err; }, | 
					
						
							|  |  |  |     function() { | 
					
						
							|  |  |  |       console.log('challenge seed completed'); | 
					
						
							|  |  |  |       process.exit(0); | 
					
						
							| 
									
										
										
										
											2015-06-02 20:32:10 -07:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-10-20 19:53:08 -07:00
										 |  |  |   ); |