| 
									
										
										
										
											2021-07-21 20:08:17 +02:00
										 |  |  | 'use strict'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-08 23:33:39 +02:00
										 |  |  | /* eslint-disable eqeqeq */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-23 12:32:58 +02:00
										 |  |  | const { ESLint } = require('eslint'); | 
					
						
							| 
									
										
										
										
											2021-07-21 20:08:17 +02:00
										 |  |  | const componentGenerator = require('./component'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // This is used to be able to indent block inside Handlebars helpers and improve templates visibility.
 | 
					
						
							|  |  |  | // It's not very robust, and forces you to use 2 spaces indentation inside for your blocks.
 | 
					
						
							|  |  |  | // If it become a pain don't hesitate to remove it.
 | 
					
						
							| 
									
										
										
										
											2022-08-08 23:33:39 +02:00
										 |  |  | const leftShift = (str) => str.replace(/^ {2}/gm, ''); | 
					
						
							| 
									
										
										
										
											2021-07-21 20:08:17 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-22 14:10:54 +02:00
										 |  |  | const evaluateExpression = (a, operator, b) => { | 
					
						
							|  |  |  |   switch (operator) { | 
					
						
							|  |  |  |     case '==': | 
					
						
							|  |  |  |       return a == b; | 
					
						
							|  |  |  |     case '===': | 
					
						
							|  |  |  |       return a === b; | 
					
						
							|  |  |  |     case '!=': | 
					
						
							|  |  |  |       return a != b; | 
					
						
							|  |  |  |     case '!==': | 
					
						
							|  |  |  |       return a !== b; | 
					
						
							|  |  |  |     case '<': | 
					
						
							|  |  |  |       return a < b; | 
					
						
							|  |  |  |     case '<=': | 
					
						
							|  |  |  |       return a <= b; | 
					
						
							|  |  |  |     case '>': | 
					
						
							|  |  |  |       return a > b; | 
					
						
							|  |  |  |     case '>=': | 
					
						
							|  |  |  |       return a >= b; | 
					
						
							|  |  |  |     case '&&': | 
					
						
							|  |  |  |       return a && b; | 
					
						
							|  |  |  |     case '||': | 
					
						
							|  |  |  |       return a || b; | 
					
						
							|  |  |  |     default: | 
					
						
							|  |  |  |       return false; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-08 23:33:39 +02:00
										 |  |  | /** | 
					
						
							|  |  |  |  * @param {import('plop').NodePlopAPI} plop | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | module.exports = function generator(plop) { | 
					
						
							|  |  |  |   plop.setHelper('if', (...args) => { | 
					
						
							|  |  |  |     const end = args.length - 1; | 
					
						
							|  |  |  |     const { fn, inverse } = args[end]; | 
					
						
							|  |  |  |     if (args.length === 2) { | 
					
						
							|  |  |  |       const condition = args[0]; | 
					
						
							| 
									
										
										
										
											2021-07-22 14:10:54 +02:00
										 |  |  |       return leftShift(condition ? fn(this) : inverse(this)); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-08-08 23:33:39 +02:00
										 |  |  |     const [a, operator, b] = Array.from(args).slice(0, end); | 
					
						
							| 
									
										
										
										
											2022-08-08 15:50:34 +02:00
										 |  |  |     return leftShift(evaluateExpression(a, operator, b) ? fn(this) : inverse(this)); | 
					
						
							| 
									
										
										
										
											2021-07-21 20:08:17 +02:00
										 |  |  |   }); | 
					
						
							| 
									
										
										
										
											2022-08-08 23:33:39 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |   plop.setHelper('unless', (...args) => { | 
					
						
							|  |  |  |     const end = args.length - 1; | 
					
						
							|  |  |  |     const { fn, inverse } = args[end]; | 
					
						
							|  |  |  |     if (args.length === 2) { | 
					
						
							|  |  |  |       const condition = args[0]; | 
					
						
							| 
									
										
										
										
											2021-07-22 14:10:54 +02:00
										 |  |  |       return leftShift(!condition ? fn(this) : inverse(this)); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-08-08 23:33:39 +02:00
										 |  |  |     const [a, operator, b] = Array.from(args).slice(0, end); | 
					
						
							| 
									
										
										
										
											2022-08-08 15:50:34 +02:00
										 |  |  |     return leftShift(!evaluateExpression(a, operator, b) ? fn(this) : inverse(this)); | 
					
						
							| 
									
										
										
										
											2021-07-22 14:10:54 +02:00
										 |  |  |   }); | 
					
						
							| 
									
										
										
										
											2022-08-08 23:33:39 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |   plop.setHelper('else', (_, { fn }) => { | 
					
						
							| 
									
										
										
										
											2021-07-22 14:10:54 +02:00
										 |  |  |     return leftShift(fn(this)); | 
					
						
							| 
									
										
										
										
											2021-07-21 20:08:17 +02:00
										 |  |  |   }); | 
					
						
							| 
									
										
										
										
											2022-08-08 23:33:39 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |   plop.setActionType('lint', async (answers, config, plopfileApi) => { | 
					
						
							| 
									
										
										
										
											2021-07-23 12:32:58 +02:00
										 |  |  |     const { files } = config; | 
					
						
							| 
									
										
										
										
											2022-08-08 23:33:39 +02:00
										 |  |  |     const patterns = files.map((file) => plopfileApi.renderString(file, answers)); | 
					
						
							| 
									
										
										
										
											2021-07-23 12:32:58 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     const eslint = new ESLint({ fix: true }); | 
					
						
							|  |  |  |     const results = await eslint.lintFiles(patterns); | 
					
						
							|  |  |  |     await ESLint.outputFixes(results); | 
					
						
							|  |  |  |     return 'Linting errors autofixed.'; | 
					
						
							|  |  |  |   }); | 
					
						
							| 
									
										
										
										
											2022-08-08 23:33:39 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-21 20:08:17 +02:00
										 |  |  |   plop.setGenerator('component', componentGenerator); | 
					
						
							|  |  |  | }; |