UNPKG

@hadss/hmrouter-plugin

Version:

HMRouter Compiler Plugin

97 lines (96 loc) 4.46 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.InitializerProcessor = void 0; const hvigor_ohos_plugin_1 = require("@ohos/hvigor-ohos-plugin"); const framework_1 = require("../../framework"); const HMRouterPluginConfig_1 = require("../config/HMRouterPluginConfig"); const constants_1 = require("../constants"); class InitializerProcessor { constructor(context) { this.context = context; } execute() { this.context.config = this.initConfig(this.context.node); this.context.routerMap = []; framework_1.Logger.debug(this.context.node.getNodeName(), `read config finished, config: ${this.context.config}`); this.scanFiles(); framework_1.Logger.debug(this.context.node.getNodeName(), `Scanned ${this.context.scanFiles.length} files ${this.context.scanFiles}`); } scanFiles() { this.context.config.scanDir.forEach((dir) => { const scanPath = this.context.config.getScanPath(dir); this.deepScan(scanPath, '', this.context); }); } deepScan(scanPath, filePath, context) { let resolvePath = framework_1.PluginFileUtil.pathResolve(scanPath, filePath); if (!framework_1.PluginFileUtil.exist(resolvePath)) { return; } if (framework_1.PluginFileUtil.isDictionary(resolvePath)) { const files = framework_1.PluginFileUtil.readdirSync(resolvePath); files.forEach((file) => { this.deepScan(resolvePath, file, context); }); } else { context.scanFiles.push(resolvePath); } } initConfig(node) { this.setupAppContext(node); const projectConfigParam = this.readProjectConfig(); const moduleConfigParam = this.readModuleConfig(node); const mergedConfigParam = this.mergeConfigs(projectConfigParam, moduleConfigParam); return this.createConfigInstance(node, mergedConfigParam); } readConfigFile(configPath) { let configParam = {}; if (framework_1.PluginFileUtil.exist(configPath)) { configParam = framework_1.PluginFileUtil.readJson5(configPath); } return configParam; } readProjectConfig() { const projectConfigFilePath = framework_1.PluginFileUtil.pathResolve(framework_1.PluginStore.getInstance().get('projectFilePath'), constants_1.FilePathConstants.CONFIG_FILE_NAME); return this.readConfigFile(projectConfigFilePath); } readModuleConfig(node) { const moduleConfigFilePath = framework_1.PluginFileUtil.pathResolve(node.getNodePath(), constants_1.FilePathConstants.CONFIG_FILE_NAME); return this.readConfigFile(moduleConfigFilePath); } mergeConfigs(projectConfigParam, moduleConfigParam) { const defaultConfig = { scanDir: [constants_1.FilePathConstants.DEFAULT_SCAN_DIR], routerMapDir: constants_1.FilePathConstants.DEFAULT_ROUTER_MAP_DIR, builderDir: constants_1.FilePathConstants.DEFAULT_BUILD_DIR, defaultPageTemplate: constants_1.TemplateConstants.VIEW_BUILDER_TEMPLATE, customPageTemplate: [], saveGeneratedFile: false, autoObfuscation: false, }; return { ...defaultConfig, ...projectConfigParam, ...moduleConfigParam, }; } setupAppContext(node) { const appContext = node.getParentNode()?.getContext(hvigor_ohos_plugin_1.OhosPluginId.OHOS_APP_PLUGIN); if (appContext) { framework_1.PluginStore.getInstance().set('projectFilePath', appContext.getProjectPath()); framework_1.PluginStore.getInstance().set('appContext', appContext); framework_1.PluginStore.getInstance().set('buildMode', appContext.getBuildMode()); } } createConfigInstance(node, configParam) { const modulePath = node.getNodePath(); let nodeName = node.getNodeName(); if ((0, framework_1.isHapModule)(node)) { let packageJson = framework_1.PluginFileUtil.readJson5(framework_1.PluginFileUtil.pathResolve(node.getNodePath(), constants_1.FilePathConstants.OH_PACKAGE_FILE_NAME)); nodeName = packageJson.name; } return new HMRouterPluginConfig_1.HMRouterPluginConfig(nodeName, modulePath, modulePath, configParam); } } exports.InitializerProcessor = InitializerProcessor;