UNPKG

@hadss/hmrouter-plugin

Version:

HMRouter Compiler Plugin

119 lines (118 loc) 5.65 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.RouterMapBuildingProcessor = void 0; const framework_1 = require("../../framework"); const constants_1 = require("../constants"); const PluginError_1 = require("../error/PluginError"); class RouterMapBuildingProcessor { constructor(context) { this.context = context; } execute() { framework_1.Logger.debug(this.context.node.getNodeName(), `Start to build router map...`); this.readExistingRouterMap(); for (const result of this.context.getAnalyzeResults()) { this.buildRouterMap(result); } this.generateRouterMapFile(); } buildRouterMap(result) { if (!result.sourceFilePath) { throw PluginError_1.PluginError.create(PluginError_1.ErrorCode.ERROR_PAGE_SOURCE_FILE, this.context.config.moduleName, result.sourceFilePath); } let pageSourceFile = this.context.config .getRelativeSourcePath(result.sourceFilePath) .replaceAll(constants_1.FilePathConstants.FILE_SEPARATOR, constants_1.FilePathConstants.DELIMITER); if (result.annotation === constants_1.AnnotationConstants.ROUTER_ANNOTATION) { let pageUrl = result.pageUrl; let uniqueKey = `${result.name}#${pageUrl}`; let builderPath = this.context.generatedPaths.get(uniqueKey) || ''; this.context.routerMap.push({ name: pageUrl, pageSourceFile: builderPath, buildFunction: result.name + 'Builder', customData: result, }); return; } const annotationConfig = { [constants_1.AnnotationConstants.ANIMATOR_ANNOTATION]: { prefix: constants_1.PrefixConstants.ANIMATOR_PREFIX, propertyName: 'animatorName', resultType: 'HMAnimatorResult', }, [constants_1.AnnotationConstants.INTERCEPTOR_ANNOTATION]: { prefix: constants_1.PrefixConstants.INTERCEPTOR_PREFIX, propertyName: 'interceptorName', resultType: 'HMInterceptorResult', }, [constants_1.AnnotationConstants.LIFECYCLE_ANNOTATION]: { prefix: constants_1.PrefixConstants.LIFECYCLE_PREFIX, propertyName: 'lifecycleName', resultType: 'HMLifecycleResult', }, [constants_1.AnnotationConstants.SERVICE_ANNOTATION]: { prefix: constants_1.PrefixConstants.SERVICE_PREFIX, propertyName: 'serviceName', resultType: 'HMServiceResult', }, [constants_1.AnnotationConstants.SERVICE_PROVIDE_ANNOTATION]: { prefix: constants_1.PrefixConstants.SERVICE_PROVIDE_PREFIX, propertyName: 'serviceName', resultType: 'HMServiceResult', }, }; const config = annotationConfig[result.annotation]; const nameValue = result[config.propertyName]; const fullName = config.prefix + nameValue; this.context.routerMap.push({ name: fullName, pageSourceFile: pageSourceFile, buildFunction: '', customData: result, }); } generateRouterMapFile() { let set = new Set(); this.context.routerMap.forEach((item) => { if (set.has(item.name)) { throw PluginError_1.PluginError.create(PluginError_1.ErrorCode.DUPLICATE_NAME, this.context.config.moduleName, item.name); } set.add(item.name); }); let routerMap = { routerMap: this.context.routerMap.map((item) => { if (item.customData && item.customData.annotation) { delete item.customData.annotation; delete item.customData.sourceFilePath; delete item.customData.isDefaultExport; delete item.customData.templateData; } return item; }), }; const routerMapJsonStr = JSON.stringify(routerMap, null, 2); const routerMapFilePath = this.context.config.getRouterMapDir(); framework_1.PluginFileUtil.ensureFileSync(routerMapFilePath); framework_1.PluginFileUtil.writeFileSync(routerMapFilePath, routerMapJsonStr); framework_1.Logger.info(this.context.node.getNodeName(), `hm_router_map.json has been generated in ${routerMapFilePath}`); } readExistingRouterMap() { const moduleJsonOpt = this.context.moduleContext.getModuleJsonOpt(); if (!moduleJsonOpt?.module.routerMap) { framework_1.Logger.debug(this.context.node.getNodeName(), `No routerMap found in the module configuration`); return; } const routerMapFileName = moduleJsonOpt.module.routerMap.split(':')[1]; if (routerMapFileName !== 'hm_router_map') { const routerMapFilePath = this.context.config.getModuleRouterMapFilePath(routerMapFileName); const routerMapObj = framework_1.PluginFileUtil.readJson5(routerMapFilePath); const systemRouterMap = routerMapObj[constants_1.RouterMapConstants.ROUTER_MAP_KEY]; if (systemRouterMap && systemRouterMap.length > 0) { this.context.routerMap.unshift(...systemRouterMap); framework_1.Logger.debug(this.context.node.getNodeName(), `System routerMap added to the current routerMap`); } } } } exports.RouterMapBuildingProcessor = RouterMapBuildingProcessor;