| 
									
										
										
										
											2018-10-19 10:59:14 +01:00
										 |  |  | const path = require('path'); | 
					
						
							|  |  |  | const fs = require('fs'); | 
					
						
							| 
									
										
										
										
											2018-10-19 13:53:11 +01:00
										 |  |  | const matter = require('gray-matter'); | 
					
						
							| 
									
										
										
										
											2019-01-15 01:44:42 +03:00
										 |  |  | const { dasherize } = require('../../../client/utils'); | 
					
						
							| 
									
										
										
										
											2018-12-06 11:18:56 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | const pass = true; | 
					
						
							| 
									
										
										
										
											2018-10-19 10:59:14 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | const guideRoot = path.resolve(__dirname, '../../../guide'); | 
					
						
							| 
									
										
										
										
											2018-12-06 11:18:56 +00:00
										 |  |  | const challengeRoot = path.resolve(__dirname, '../../../curriculum/challenges'); | 
					
						
							|  |  |  | exports.guideRoot = guideRoot; | 
					
						
							|  |  |  | exports.challengeRoot = challengeRoot; | 
					
						
							| 
									
										
										
										
											2018-10-19 10:59:14 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | const allowedLangDirNames = [ | 
					
						
							|  |  |  |   'arabic', | 
					
						
							|  |  |  |   'chinese', | 
					
						
							|  |  |  |   'english', | 
					
						
							|  |  |  |   'portuguese', | 
					
						
							|  |  |  |   'russian', | 
					
						
							|  |  |  |   'spanish' | 
					
						
							|  |  |  | ]; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-06 11:18:56 +00:00
										 |  |  | exports.checkGuideFile = function checkGuideFile(file) { | 
					
						
							| 
									
										
										
										
											2018-10-19 10:59:14 +01:00
										 |  |  |   const { stat, depth, name, fullPath } = file; | 
					
						
							|  |  |  |   if (depth === 1) { | 
					
						
							|  |  |  |     if (stat.isFile()) { | 
					
						
							|  |  |  |       throw new Error(`${name} is not valid in the ${guideRoot} directory`); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     if (!allowedLangDirNames.includes(name)) { | 
					
						
							|  |  |  |       throw new Error(`${name} should not be in the ${guideRoot} directory`); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   if (stat.isDirectory()) { | 
					
						
							| 
									
										
										
										
											2018-12-06 11:18:56 +00:00
										 |  |  |     return checkDirName(name, fullPath); | 
					
						
							| 
									
										
										
										
											2018-10-19 10:59:14 +01:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2018-12-06 11:18:56 +00:00
										 |  |  |   return checkGuideFileName(name, fullPath); | 
					
						
							|  |  |  | }; | 
					
						
							| 
									
										
										
										
											2018-10-19 13:53:11 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | function checkDirName(dirName, fullPath) { | 
					
						
							|  |  |  |   return new Promise((resolve, reject) => { | 
					
						
							| 
									
										
										
										
											2019-01-15 01:44:42 +03:00
										 |  |  |     if (dasherize(dirName) !== dirName) { | 
					
						
							| 
									
										
										
										
											2018-10-19 13:53:11 +01:00
										 |  |  |       return reject( | 
					
						
							|  |  |  |         new Error(`
 | 
					
						
							| 
									
										
										
										
											2019-01-15 01:44:42 +03:00
										 |  |  | Invalid or upper case character found in '${dirName}', please use '-' for spaces | 
					
						
							|  |  |  | and all folder names must be lower case. Valid characters are [a-z0-9\\-.]. | 
					
						
							| 
									
										
										
										
											2018-10-19 13:53:11 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-15 01:44:42 +03:00
										 |  |  |   Found in: | 
					
						
							| 
									
										
										
										
											2018-10-19 13:53:11 +01:00
										 |  |  |     ${fullPath} | 
					
						
							|  |  |  | `)
 | 
					
						
							|  |  |  |       ); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-12-06 11:18:56 +00:00
										 |  |  |     return resolve(pass); | 
					
						
							| 
									
										
										
										
											2018-10-19 13:53:11 +01:00
										 |  |  |   }); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-06 11:18:56 +00:00
										 |  |  | function checkGuideFileName(fileName, fullPath) { | 
					
						
							| 
									
										
										
										
											2018-10-19 13:53:11 +01:00
										 |  |  |   return new Promise((resolve, reject) => { | 
					
						
							|  |  |  |     if (fileName !== 'index.md') { | 
					
						
							|  |  |  |       return reject( | 
					
						
							|  |  |  |         new Error( | 
					
						
							|  |  |  |           `${fileName} is not a valid file name, please use 'index.md'
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       Found in: | 
					
						
							|  |  |  |         ${fullPath} | 
					
						
							|  |  |  |     `
 | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  |       ); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-12-06 11:18:56 +00:00
										 |  |  |     return resolve(pass); | 
					
						
							| 
									
										
										
										
											2018-10-19 13:53:11 +01:00
										 |  |  |   }); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-06 11:18:56 +00:00
										 |  |  | exports.checkFrontmatter = function checkFrontmatter( | 
					
						
							|  |  |  |   { fullPath, stat }, | 
					
						
							|  |  |  |   options = { | 
					
						
							|  |  |  |     validator() { | 
					
						
							|  |  |  |       return true; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | ) { | 
					
						
							|  |  |  |   if (!stat.isFile()) { | 
					
						
							|  |  |  |     return Promise.resolve(pass); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2018-10-19 13:53:11 +01:00
										 |  |  |   return new Promise((resolve, reject) => | 
					
						
							|  |  |  |     fs.readFile(fullPath, 'utf8', (err, content) => { | 
					
						
							|  |  |  |       if (err) { | 
					
						
							|  |  |  |         return reject(new Error(err)); | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |       try { | 
					
						
							|  |  |  |         const { data: frontmatter } = matter(content); | 
					
						
							| 
									
										
										
										
											2018-12-06 11:18:56 +00:00
										 |  |  |         const { validator } = options; | 
					
						
							|  |  |  |         if (!validator(frontmatter)) { | 
					
						
							| 
									
										
										
										
											2018-10-19 13:53:11 +01:00
										 |  |  |           return reject( | 
					
						
							| 
									
										
										
										
											2018-12-06 11:18:56 +00:00
										 |  |  |             new Error( | 
					
						
							|  |  |  |               `The article at: ${fullPath} failed frontmatter validation.` | 
					
						
							|  |  |  |             ) | 
					
						
							| 
									
										
										
										
											2018-10-19 13:53:11 +01:00
										 |  |  |           ); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-12-06 11:18:56 +00:00
										 |  |  |         return resolve(pass); | 
					
						
							| 
									
										
										
										
											2018-10-19 13:53:11 +01:00
										 |  |  |       } catch (e) { | 
					
						
							|  |  |  |         console.log(`
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-15 01:44:42 +03:00
										 |  |  |   The below occurred in: | 
					
						
							| 
									
										
										
										
											2018-10-19 13:53:11 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |   ${fullPath} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   `);
 | 
					
						
							|  |  |  |         throw e; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     }) | 
					
						
							|  |  |  |   ); | 
					
						
							| 
									
										
										
										
											2018-12-06 11:18:56 +00:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function extractLangFromFileName({ path: relativePath }) { | 
					
						
							|  |  |  |   return relativePath.split(path.sep)[0]; | 
					
						
							| 
									
										
										
										
											2018-10-19 13:53:11 +01:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2018-12-06 11:18:56 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | exports.extractLangFromFileName = extractLangFromFileName; |