| 
									
										
										
										
											2018-09-06 15:31:05 +01:00
										 |  |  | const fs = require('fs'); | 
					
						
							| 
									
										
										
										
											2018-09-06 15:18:48 +01:00
										 |  |  | const path = require('path'); | 
					
						
							| 
									
										
										
										
											2018-10-08 00:00:50 +01:00
										 |  |  | const debug = require('debug'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-17 05:16:35 +05:30
										 |  |  | const env = require('../../../config/env'); | 
					
						
							| 
									
										
										
										
											2019-02-04 11:42:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-17 05:16:35 +05:30
										 |  |  | const { getChallengesForLang } = require('../../../curriculum/getChallenges'); | 
					
						
							|  |  |  | const { createPathMigrationMap } = require('../seed/createPathMigrationMap'); | 
					
						
							| 
									
										
										
										
											2018-09-06 14:58:51 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-13 18:48:24 +05:30
										 |  |  | const { createRedirects } = require('./create-redirects'); | 
					
						
							| 
									
										
										
										
											2018-10-08 00:00:50 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | const log = debug('fcc:tools:ensure-env'); | 
					
						
							| 
									
										
										
										
											2018-10-09 16:46:14 +05:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-19 01:19:40 +05:30
										 |  |  | const { FREECODECAMP_NODE_ENV } = process.env; | 
					
						
							| 
									
										
										
										
											2019-02-04 11:42:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-05 22:08:37 +00:00
										 |  |  | const { | 
					
						
							|  |  |  |   apiLocation: api, | 
					
						
							|  |  |  |   forumLocation: forum, | 
					
						
							|  |  |  |   locale, | 
					
						
							|  |  |  |   newsLocation: news | 
					
						
							|  |  |  | } = env; | 
					
						
							| 
									
										
										
										
											2018-10-09 16:46:14 +05:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-17 05:16:35 +05:30
										 |  |  | const apiPath = path.resolve(__dirname, '../../../api-server'); | 
					
						
							|  |  |  | const clientPath = path.resolve(__dirname, '../../../client'); | 
					
						
							| 
									
										
										
										
											2018-10-08 00:00:50 +01:00
										 |  |  | const clientStaticPath = path.resolve(clientPath, 'static'); | 
					
						
							| 
									
										
										
										
											2019-06-17 05:16:35 +05:30
										 |  |  | const globalConfigPath = path.resolve(__dirname, '../../../config'); | 
					
						
							| 
									
										
										
										
											2018-10-08 00:00:50 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-19 01:19:40 +05:30
										 |  |  | if (FREECODECAMP_NODE_ENV === 'production') { | 
					
						
							| 
									
										
										
										
											2019-02-05 22:08:37 +00:00
										 |  |  |   const redirects = createRedirects({ api, news, forum }); | 
					
						
							| 
									
										
										
										
											2018-10-20 23:59:07 +05:30
										 |  |  |   fs.writeFile(`${clientStaticPath}/_redirects`, redirects, function(err) { | 
					
						
							|  |  |  |     if (err) { | 
					
						
							|  |  |  |       log('Error'); | 
					
						
							|  |  |  |       console.error(err); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     log('_redirects written'); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | } else { | 
					
						
							| 
									
										
										
										
											2019-08-19 01:19:40 +05:30
										 |  |  |   log(`ignoring creation of redirect file in ${FREECODECAMP_NODE_ENV}`); | 
					
						
							| 
									
										
										
										
											2018-10-20 23:59:07 +05:30
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2018-09-06 15:18:48 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-04 11:42:31 +00:00
										 |  |  | const migrationMapPath = `${apiPath}/server/resources/pathMigration.json`; | 
					
						
							|  |  |  | fs.access(migrationMapPath, err => { | 
					
						
							| 
									
										
										
										
											2019-08-19 01:19:40 +05:30
										 |  |  |   if (err && FREECODECAMP_NODE_ENV !== 'production') { | 
					
						
							| 
									
										
										
										
											2018-10-08 00:00:50 +01:00
										 |  |  |     log('creating pathMigration'); | 
					
						
							| 
									
										
										
										
											2019-02-04 11:42:31 +00:00
										 |  |  |     return fs.writeFileSync(migrationMapPath, '{}'); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2019-08-19 01:19:40 +05:30
										 |  |  |   if (FREECODECAMP_NODE_ENV === 'production') { | 
					
						
							| 
									
										
										
										
											2019-02-04 11:42:31 +00:00
										 |  |  |     return getChallengesForLang(locale) | 
					
						
							|  |  |  |       .then(createPathMigrationMap) | 
					
						
							|  |  |  |       .then(map => { | 
					
						
							|  |  |  |         fs.writeFileSync(migrationMapPath, JSON.stringify(map)); | 
					
						
							|  |  |  |         log('pathMigration has been written'); | 
					
						
							|  |  |  |       }) | 
					
						
							|  |  |  |       .catch(err => { | 
					
						
							|  |  |  |         console.error(err); | 
					
						
							| 
									
										
										
										
											2019-02-16 08:48:52 +00:00
										 |  |  |         // eslint-disable-next-line
 | 
					
						
							| 
									
										
										
										
											2019-02-04 11:42:31 +00:00
										 |  |  |         process.exit(1); | 
					
						
							|  |  |  |       }); | 
					
						
							| 
									
										
										
										
											2018-05-27 10:57:18 +01:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2018-10-08 00:00:50 +01:00
										 |  |  |   log('pathMigration present'); | 
					
						
							| 
									
										
										
										
											2018-09-06 14:58:51 +01:00
										 |  |  |   return null; | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-27 12:28:31 +05:30
										 |  |  | fs.writeFileSync(`${clientPath}/config/env.json`, JSON.stringify(env)); | 
					
						
							| 
									
										
										
										
											2018-10-09 16:46:14 +05:30
										 |  |  | fs.writeFileSync(`${globalConfigPath}/env.json`, JSON.stringify(env)); |