| 
									
										
										
										
											2021-07-21 20:08:17 +02:00
										 |  |  | 'use strict'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-23 12:32:58 +02:00
										 |  |  | const { join } = require('path'); | 
					
						
							| 
									
										
										
										
											2021-07-21 20:08:17 +02:00
										 |  |  | const { flow, camelCase, upperFirst, lowerCase } = require('lodash'); | 
					
						
							| 
									
										
										
										
											2021-07-22 14:10:54 +02:00
										 |  |  | const fileExistsInPackages = require('../utils/fileExistsInPackages'); | 
					
						
							| 
									
										
										
										
											2021-07-21 20:08:17 +02:00
										 |  |  | const getPluginList = require('../utils/getPluginList'); | 
					
						
							| 
									
										
										
										
											2021-07-23 12:32:58 +02:00
										 |  |  | const packagesFolder = require('../utils/packagesFolder'); | 
					
						
							| 
									
										
										
										
											2021-07-21 20:08:17 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-29 14:20:03 +02:00
										 |  |  | const pascalCase = flow(camelCase, upperFirst); | 
					
						
							| 
									
										
										
										
											2021-07-21 20:08:17 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | const prompts = [ | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     type: 'list', | 
					
						
							|  |  |  |     name: 'plugin', | 
					
						
							|  |  |  |     message: 'Which plugin should be targeted?', | 
					
						
							|  |  |  |     choices: getPluginList, | 
					
						
							|  |  |  |   }, | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     type: 'input', | 
					
						
							|  |  |  |     name: 'name', | 
					
						
							|  |  |  |     message: 'What should be the name of the component?', | 
					
						
							| 
									
										
										
										
											2021-09-13 12:03:12 +02:00
										 |  |  |     async validate(name, answers) { | 
					
						
							| 
									
										
										
										
											2021-07-22 14:10:54 +02:00
										 |  |  |       if (!name) { | 
					
						
							|  |  |  |         return 'The name cannot be empty.'; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       return (await fileExistsInPackages(`${answers.plugin}/admin/src/components/${name}`)) | 
					
						
							|  |  |  |         ? 'This component already exists.' | 
					
						
							|  |  |  |         : true; | 
					
						
							|  |  |  |     }, | 
					
						
							| 
									
										
										
										
											2021-07-21 20:08:17 +02:00
										 |  |  |     filter: pascalCase, | 
					
						
							|  |  |  |   }, | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     type: 'confirm', | 
					
						
							|  |  |  |     name: 'styled', | 
					
						
							|  |  |  |     message: 'Is this a styled component?', | 
					
						
							|  |  |  |   }, | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     type: 'input', | 
					
						
							|  |  |  |     name: 'htmlTag', | 
					
						
							|  |  |  |     message: 'Which HTML tag should be used as a base?', | 
					
						
							| 
									
										
										
										
											2022-08-08 23:33:39 +02:00
										 |  |  |     when: (answers) => answers.styled, | 
					
						
							|  |  |  |     validate: (htmlTag) => (!htmlTag ? 'The HTML tag cannot be empty.' : true), | 
					
						
							| 
									
										
										
										
											2021-07-21 20:08:17 +02:00
										 |  |  |     filter: lowerCase, | 
					
						
							|  |  |  |   }, | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     type: 'confirm', | 
					
						
							|  |  |  |     name: 'useI18n', | 
					
						
							|  |  |  |     message: 'Will it use i18n?', | 
					
						
							| 
									
										
										
										
											2022-08-08 23:33:39 +02:00
										 |  |  |     when: (answers) => !answers.styled, | 
					
						
							| 
									
										
										
										
											2021-07-21 20:08:17 +02:00
										 |  |  |   }, | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     type: 'confirm', | 
					
						
							|  |  |  |     name: 'useRedux', | 
					
						
							|  |  |  |     message: 'Will it use Redux?', | 
					
						
							| 
									
										
										
										
											2022-08-08 23:33:39 +02:00
										 |  |  |     when: (answers) => !answers.styled, | 
					
						
							| 
									
										
										
										
											2021-07-21 20:08:17 +02:00
										 |  |  |   }, | 
					
						
							|  |  |  | ]; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-08 23:33:39 +02:00
										 |  |  | const actions = (answers) => { | 
					
						
							| 
									
										
										
										
											2021-07-21 20:08:17 +02:00
										 |  |  |   const { useRedux } = answers; | 
					
						
							|  |  |  |   const [pluginFolder, plugin] = answers.plugin.split('/'); | 
					
						
							|  |  |  |   answers.plugin = plugin; | 
					
						
							| 
									
										
										
										
											2021-07-23 12:32:58 +02:00
										 |  |  |   const templatesFolder = 'component/templates'; | 
					
						
							| 
									
										
										
										
											2021-07-21 20:08:17 +02:00
										 |  |  |   const pattern = useRedux ? '**/**.hbs' : '**/index.*.hbs'; | 
					
						
							| 
									
										
										
										
											2022-07-29 13:45:20 +02:00
										 |  |  |   const path = join(packagesFolder, pluginFolder, '{{ plugin }}/admin/src/components/{{ name }}'); | 
					
						
							| 
									
										
										
										
											2021-07-21 20:08:17 +02:00
										 |  |  |   return [ | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |       type: 'addMany', | 
					
						
							| 
									
										
										
										
											2021-07-23 12:32:58 +02:00
										 |  |  |       destination: path, | 
					
						
							|  |  |  |       templateFiles: [`${templatesFolder}/${pattern}`], | 
					
						
							|  |  |  |       base: templatesFolder, | 
					
						
							| 
									
										
										
										
											2021-07-21 20:08:17 +02:00
										 |  |  |     }, | 
					
						
							|  |  |  |     // TODO: If redux will be used then 'append' created reducer inside 'reducers.js'
 | 
					
						
							| 
									
										
										
										
											2021-07-23 12:32:58 +02:00
										 |  |  |     { | 
					
						
							|  |  |  |       type: 'lint', | 
					
						
							|  |  |  |       files: [path], | 
					
						
							|  |  |  |     }, | 
					
						
							| 
									
										
										
										
											2021-07-21 20:08:17 +02:00
										 |  |  |   ]; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module.exports = { prompts, actions }; |