| 
									
										
										
										
											2018-10-06 02:25:11 +01:00
										 |  |  | const path = require('path'); | 
					
						
							| 
									
										
										
										
											2018-10-07 08:51:58 +01:00
										 |  |  | const fs = require('fs'); | 
					
						
							| 
									
										
										
										
											2018-10-11 20:41:05 -06:00
										 |  |  | require('dotenv').config({ path: path.resolve(__dirname, '../../../.env') }); | 
					
						
							| 
									
										
										
										
											2018-10-28 06:18:13 +00:00
										 |  |  | const { MongoClient, ObjectID } = require('mongodb'); | 
					
						
							| 
									
										
										
										
											2018-10-06 02:25:11 +01:00
										 |  |  | const { flatten } = require('lodash'); | 
					
						
							| 
									
										
										
										
											2018-10-07 08:51:58 +01:00
										 |  |  | const debug = require('debug'); | 
					
						
							| 
									
										
										
										
											2018-10-06 02:25:11 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-16 18:22:52 +00:00
										 |  |  | const { getChallengesForLang } = require('../../../curriculum/getChallenges'); | 
					
						
							| 
									
										
										
										
											2018-10-07 08:51:58 +01:00
										 |  |  | const { createPathMigrationMap } = require('./createPathMigrationMap'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const log = debug('fcc:tools:seedChallenges'); | 
					
						
							| 
									
										
										
										
											2018-10-28 06:18:13 +00:00
										 |  |  | const { MONGOHQ_URL, LOCALE: lang = 'english' } = process.env; | 
					
						
							| 
									
										
										
										
											2018-10-06 02:25:11 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | function handleError(err, client) { | 
					
						
							|  |  |  |   if (err) { | 
					
						
							|  |  |  |     console.error('Oh noes!!'); | 
					
						
							|  |  |  |     console.error(err); | 
					
						
							|  |  |  |     try { | 
					
						
							|  |  |  |       client.close(); | 
					
						
							|  |  |  |     } catch (e) { | 
					
						
							|  |  |  |       // no-op
 | 
					
						
							|  |  |  |     } finally { | 
					
						
							|  |  |  |       /* eslint-disable-next-line no-process-exit */ | 
					
						
							|  |  |  |       process.exit(1); | 
					
						
							| 
									
										
										
										
											2015-08-10 12:14:38 -07:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-10-06 02:25:11 +01:00
										 |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | MongoClient.connect( | 
					
						
							|  |  |  |   MONGOHQ_URL, | 
					
						
							|  |  |  |   { useNewUrlParser: true }, | 
					
						
							|  |  |  |   function(err, client) { | 
					
						
							|  |  |  |     handleError(err, client); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-28 06:18:13 +00:00
										 |  |  |     log('Connected successfully to mongo at %s', MONGOHQ_URL); | 
					
						
							| 
									
										
										
										
											2018-10-06 02:25:11 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     const db = client.db('freecodecamp'); | 
					
						
							| 
									
										
										
										
											2018-10-28 06:18:13 +00:00
										 |  |  |     const challengeCollection = db.collection('challenge'); | 
					
						
							| 
									
										
										
										
											2018-10-06 02:25:11 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-16 21:23:02 +00:00
										 |  |  |     challengeCollection.deleteMany({}, async err => { | 
					
						
							| 
									
										
										
										
											2018-10-06 02:25:11 +01:00
										 |  |  |       handleError(err, client); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-28 06:18:13 +00:00
										 |  |  |       log('deleted all the challenges'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-16 21:23:02 +00:00
										 |  |  |       const curriculum = await getChallengesForLang(lang); | 
					
						
							| 
									
										
										
										
											2018-10-06 02:25:11 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |       const allChallenges = Object.keys(curriculum) | 
					
						
							|  |  |  |         .map(key => curriculum[key].blocks) | 
					
						
							|  |  |  |         .reduce((challengeArray, superBlock) => { | 
					
						
							|  |  |  |           const challengesForBlock = Object.keys(superBlock).map( | 
					
						
							|  |  |  |             key => superBlock[key].challenges | 
					
						
							|  |  |  |           ); | 
					
						
							|  |  |  |           return [...challengeArray, ...flatten(challengesForBlock)]; | 
					
						
							| 
									
										
										
										
											2018-10-12 11:00:34 +01:00
										 |  |  |         }, []) | 
					
						
							|  |  |  |         .map(challenge => { | 
					
						
							| 
									
										
										
										
											2018-10-28 06:18:13 +00:00
										 |  |  |           const currentId = challenge.id.slice(0); | 
					
						
							|  |  |  |           challenge._id = ObjectID(currentId); | 
					
						
							| 
									
										
										
										
											2018-10-12 11:00:34 +01:00
										 |  |  |           delete challenge.id; | 
					
						
							|  |  |  |           return challenge; | 
					
						
							|  |  |  |         }); | 
					
						
							| 
									
										
										
										
											2018-10-06 02:25:11 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |       try { | 
					
						
							| 
									
										
										
										
											2018-10-28 06:18:13 +00:00
										 |  |  |         challengeCollection.insertMany( | 
					
						
							|  |  |  |           allChallenges, | 
					
						
							|  |  |  |           { ordered: false }, | 
					
						
							|  |  |  |           err => { | 
					
						
							|  |  |  |             handleError(err, client); | 
					
						
							|  |  |  |             log('challenge seed complete'); | 
					
						
							|  |  |  |             client.close(); | 
					
						
							|  |  |  |           } | 
					
						
							|  |  |  |         ); | 
					
						
							| 
									
										
										
										
											2018-10-06 02:25:11 +01:00
										 |  |  |       } catch (e) { | 
					
						
							|  |  |  |         handleError(e, client); | 
					
						
							|  |  |  |       } finally { | 
					
						
							| 
									
										
										
										
											2018-10-07 08:51:58 +01:00
										 |  |  |         log('generating path migration map'); | 
					
						
							|  |  |  |         const pathMap = createPathMigrationMap(curriculum); | 
					
						
							|  |  |  |         const outputDir = path.resolve( | 
					
						
							|  |  |  |           __dirname, | 
					
						
							|  |  |  |           '../../../api-server/server/resources/pathMigration.json' | 
					
						
							|  |  |  |         ); | 
					
						
							|  |  |  |         fs.writeFile(outputDir, JSON.stringify(pathMap), err => { | 
					
						
							|  |  |  |           if (err) { | 
					
						
							|  |  |  |             console.error('Oh noes!!'); | 
					
						
							|  |  |  |             console.error(err); | 
					
						
							|  |  |  |           } | 
					
						
							|  |  |  |           log('path migration map generated'); | 
					
						
							|  |  |  |         }); | 
					
						
							| 
									
										
										
										
											2018-10-06 02:25:11 +01:00
										 |  |  |       } | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | ); |