UNPKG

@hadss/hmrouter-plugin

Version:

HMRouter Compiler Plugin

154 lines (153 loc) 8.28 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.HMRouterHvigorPlugin = void 0; const micromatch_1 = __importDefault(require("micromatch")); const ejs_1 = __importDefault(require("ejs")); const PluginModel_1 = require("./common/PluginModel"); const Logger_1 = require("./common/Logger"); const CommonConstants_1 = __importDefault(require("./constants/CommonConstants")); const FileUtil_1 = __importDefault(require("./utils/FileUtil")); const StringUtil_1 = require("./utils/StringUtil"); const HMRouterAnalyzer_1 = require("./HMRouterAnalyzer"); class HMRouterHvigorPlugin { constructor(config) { this.routerMap = []; this.scanFiles = []; this.analyzerController = new HMRouterAnalyzer_1.AnalyzerController(); this.config = config; } analyzeAnnotation() { this.config.scanDir.forEach((dir) => { const scanPath = this.config.getScanPath(dir); this.deepScan(scanPath, ''); }); Logger_1.Logger.info(`Scanned ${this.scanFiles.length} files`, this.scanFiles); this.scanFiles.forEach((filePath) => { if (filePath.endsWith(CommonConstants_1.default.ETS_SUFFIX)) { this.analyzerController.analyzeFile(filePath, this.config); } }); this.analyzerController.getAnalyzeResultSet().forEach((analyzerResult) => { this.pushRouterInfo(analyzerResult); }); } generateRouterMap() { let set = new Set(); this.routerMap.forEach((item) => { if (set.has(item.name)) { Logger_1.Logger.error(Logger_1.PluginError.ERR_DUPLICATE_NAME, item.name); throw new Error(`Route page name: ${item.name} is duplicated`); } else { set.add(item.name); } }); let routerMap = { routerMap: this.routerMap.map((item) => { if (item.customData && item.customData.annotation) { delete item.customData.annotation; delete item.customData.pageSourceFile; delete item.customData.isDefaultExport; } return item; }) }; const routerMapJsonStr = JSON.stringify(routerMap, null, 2); const routerMapFilePath = this.config.getRouterMapDir(); FileUtil_1.default.ensureFileSync(routerMapFilePath); FileUtil_1.default.writeFileSync(routerMapFilePath, routerMapJsonStr); Logger_1.Logger.info(`hm_router_map.json has been generated in ${routerMapFilePath}`); this.analyzerController.clearAnalyzeResultSet(); } matchedPath(filePath, customPageTemplate, defaultTplFilePath) { for (const template of customPageTemplate) { if (micromatch_1.default.isMatch(filePath, template.srcPath)) { return FileUtil_1.default.pathResolve(this.config.configDir, template.templatePath); } } return defaultTplFilePath; } pushRouterInfo(analyzeResult) { let pageSourceFile = this.config .getRelativeSourcePath(analyzeResult.pageSourceFile) .replaceAll(CommonConstants_1.default.FILE_SEPARATOR, CommonConstants_1.default.DELIMITER); switch (analyzeResult.annotation) { case CommonConstants_1.default.ROUTER_ANNOTATION: let generatorFilePath = this.generateBuilder(analyzeResult, pageSourceFile, this.matchedPath(pageSourceFile, this.config.customPageTemplate, this.config.getDefaultTplFilePath())); let pageUrl = analyzeResult.pageUrl; this.routerMap.push(new PluginModel_1.RouterInfo(pageUrl, generatorFilePath, analyzeResult.name + 'Builder', analyzeResult)); break; case CommonConstants_1.default.ANIMATOR_ANNOTATION: let animatorName = CommonConstants_1.default.ANIMATOR_PREFIX + analyzeResult.animatorName; this.routerMap.push(new PluginModel_1.RouterInfo(animatorName, pageSourceFile, '', analyzeResult)); break; case CommonConstants_1.default.INTERCEPTOR_ANNOTATION: let interceptorName = CommonConstants_1.default.INTERCEPTOR_PREFIX + analyzeResult.interceptorName; this.routerMap.push(new PluginModel_1.RouterInfo(interceptorName, pageSourceFile, '', analyzeResult)); break; case CommonConstants_1.default.LIFECYCLE_ANNOTATION: let lifecycleName = CommonConstants_1.default.LIFECYCLE_PREFIX + analyzeResult.lifecycleName; this.routerMap.push(new PluginModel_1.RouterInfo(lifecycleName, pageSourceFile, '', analyzeResult)); break; case CommonConstants_1.default.SERVICE_ANNOTATION: let serviceName = CommonConstants_1.default.SERVICE_PREFIX + analyzeResult.serviceName; this.routerMap.push(new PluginModel_1.RouterInfo(serviceName, pageSourceFile, '', analyzeResult)); break; case CommonConstants_1.default.SERVICE_PROVIDE_ANNOTATION: let className = CommonConstants_1.default.SERVICE_PROVIDE_PREFIX + analyzeResult.serviceName; this.routerMap.push(new PluginModel_1.RouterInfo(className, pageSourceFile, '', analyzeResult)); break; } } generateBuilder(analyzeResult, pageSourceFile, tempFilePath) { let importPath = this.config .getRelativeBuilderPath(pageSourceFile) .replaceAll(CommonConstants_1.default.FILE_SEPARATOR, CommonConstants_1.default.DELIMITER) .replaceAll(CommonConstants_1.default.ETS_SUFFIX, ''); let generatorViewName = CommonConstants_1.default.VIEW_NAME_PREFIX + analyzeResult.name + StringUtil_1.StringUtil.stringToHashCode(analyzeResult.pageUrl); const templateModel = new PluginModel_1.TemplateModel(analyzeResult.pageUrl, importPath, analyzeResult.name, !!analyzeResult.dialog, generatorViewName, analyzeResult.isDefaultExport); if (analyzeResult.useNavDst === true) { Logger_1.Logger.info(`Using custom template for ${analyzeResult.name} with useNavDst=true`); const customTplPath = FileUtil_1.default.pathResolve(__dirname, CommonConstants_1.default.PARENT_DELIMITER + CommonConstants_1.default.CUSTOM_BUILDER_TEMPLATE); if (FileUtil_1.default.exist(customTplPath)) { tempFilePath = customTplPath; } else { Logger_1.Logger.info(`Custom template not found at ${customTplPath}, falling back to default template`); } } if (!FileUtil_1.default.exist(tempFilePath)) { throw new Error('Invalid template path: ' + tempFilePath); } const tpl = FileUtil_1.default.readFileSync(tempFilePath).toString(); const templateStr = ejs_1.default.render(tpl, templateModel); const generatorFilePath = this.config.getGeneratedFilePath(templateModel.generatorViewName); FileUtil_1.default.ensureFileSync(generatorFilePath); FileUtil_1.default.writeFileSync(generatorFilePath, templateStr); Logger_1.Logger.info(`Builder ${templateModel.generatorViewName}.ets has been generated in ${generatorFilePath}`); return this.config .getBuilderFilePath(templateModel.generatorViewName) .replaceAll(CommonConstants_1.default.FILE_SEPARATOR, CommonConstants_1.default.DELIMITER); } deepScan(scanPath, filePath) { let resolvePath = FileUtil_1.default.pathResolve(scanPath, filePath); if (!FileUtil_1.default.exist(resolvePath)) { return; } if (FileUtil_1.default.isDictionary(resolvePath)) { const files = FileUtil_1.default.readdirSync(resolvePath); files.forEach((file) => { this.deepScan(resolvePath, file); }); } else { this.scanFiles.push(resolvePath); } } } exports.HMRouterHvigorPlugin = HMRouterHvigorPlugin;