You can not select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
			
				
					51 lines
				
				1.4 KiB
			
		
		
			
		
	
	
					51 lines
				
				1.4 KiB
			| 
											1 year ago
										 | 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); | ||
|  |     } | ||
|  |   } | ||
|  | } |