@hadss/hmrouter-plugin
Version:
HMRouter Compiler Plugin
121 lines (120 loc) • 5.83 kB
JavaScript
"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 projectPath = framework_1.PluginStore.getInstance().get('projectFilePath');
const projectConfigFilePath = framework_1.PluginFileUtil.pathResolve(projectPath, constants_1.FilePathConstants.CONFIG_FILE_NAME);
const configParam = this.readConfigFile(projectConfigFilePath);
return this.resolveConfigPaths(configParam, projectPath);
}
readModuleConfig(node) {
const modulePath = node.getNodePath();
const moduleConfigFilePath = framework_1.PluginFileUtil.pathResolve(modulePath, constants_1.FilePathConstants.CONFIG_FILE_NAME);
const configParam = this.readConfigFile(moduleConfigFilePath);
return this.resolveConfigPaths(configParam, modulePath);
}
resolveConfigPaths(configParam, basePath) {
if (configParam.defaultPageTemplate) {
configParam.defaultPageTemplate = framework_1.PluginFileUtil.pathResolve(basePath, configParam.defaultPageTemplate);
}
if (configParam.customPageTemplate) {
configParam.customPageTemplate.forEach((item) => {
item.templatePath = framework_1.PluginFileUtil.pathResolve(basePath, item.templatePath);
});
}
return configParam;
}
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;
}
let configDir = modulePath;
const moduleConfigPath = framework_1.PluginFileUtil.pathResolve(modulePath, constants_1.FilePathConstants.CONFIG_FILE_NAME);
const projectConfigPath = framework_1.PluginFileUtil.pathResolve(framework_1.PluginStore.getInstance().get('projectFilePath'), constants_1.FilePathConstants.CONFIG_FILE_NAME);
if (framework_1.PluginFileUtil.exist(moduleConfigPath)) {
configDir = modulePath;
}
else if (framework_1.PluginFileUtil.exist(projectConfigPath)) {
configDir = framework_1.PluginStore.getInstance().get('projectFilePath');
}
return new HMRouterPluginConfig_1.HMRouterPluginConfig(nodeName, modulePath, configDir, configParam);
}
}
exports.InitializerProcessor = InitializerProcessor;