|  |  |  | const fs = require('fs'); | 
					
						
							|  |  |  | const Json5 =require('json5'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * 用于自动生成前端组件 | 
					
						
							|  |  |  |  * 通过 src/routes/routes.json 文件构建 src/components/index.ts 文件 | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | // 解析前端路由配置文件 | 
					
						
							|  |  |  | const routesJson = Json5.parse(fs.readFileSync('./src/routes/routes.json', 'utf8')); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | let content =''; | 
					
						
							|  |  |  | content +='/**\n'; | 
					
						
							|  |  |  | content +=' * 此文件为自动生成文件,请勿修改\n'; | 
					
						
							|  |  |  | content +=' */\n\n'; | 
					
						
							|  |  |  | for(const route of routesJson){ | 
					
						
							|  |  |  |   generateImportComonents(route); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | content +='\n'; | 
					
						
							|  |  |  | content +='const localComponents = { \n'; | 
					
						
							|  |  |  | for(const route of routesJson){ | 
					
						
							|  |  |  |   generateComonents(route); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | content +='}\n\n'; | 
					
						
							|  |  |  | content +='export default localComponents;\n'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | fs.writeFileSync('./src/components/index.ts', content); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | console.info('components generated!'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function generateImportComonents(route){ | 
					
						
							|  |  |  |   const componentName =route.component.replaceAll('.','_'); | 
					
						
							|  |  |  |   const componentPath =route.componentPath; | 
					
						
							|  |  |  |   content +=`import ${componentName} from '${componentPath}';\n`; | 
					
						
							|  |  |  |   if(route.children && route.children.length){ | 
					
						
							|  |  |  |     for(const child of route.children){ | 
					
						
							|  |  |  |       generateImportComonents(child); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function generateComonents(route){ | 
					
						
							|  |  |  |   const componentName =route.component.replaceAll('.','_'); | 
					
						
							|  |  |  |   content +=`'${route.component}': ${componentName},\n`; | 
					
						
							|  |  |  |   if(route.children && route.children.length){ | 
					
						
							|  |  |  |     for(const child of route.children){ | 
					
						
							|  |  |  |       generateComonents(child); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | } |