@hadss/hmrouter-plugin
Version:
HMRouter Compiler Plugin
130 lines (129 loc) • 5.79 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.harPlugin = exports.hspPlugin = exports.hapPlugin = void 0;
const hvigor_1 = require("@ohos/hvigor");
const hvigor_ohos_plugin_1 = require("@ohos/hvigor-ohos-plugin");
const Logger_1 = require("./common/Logger");
const CommonConstants_1 = __importDefault(require("./constants/CommonConstants"));
const TsAstUtil_1 = require("./utils/TsAstUtil");
const FileUtil_1 = __importDefault(require("./utils/FileUtil"));
const HMRouterPluginHandle_1 = require("./HMRouterPluginHandle");
const PluginStore_1 = __importDefault(require("./store/PluginStore"));
class HMRouterPluginMgr {
constructor() {
this.hmRouterPluginSet = new Set();
hvigor_1.hvigor.nodesEvaluated(() => {
this.hmRouterPluginSet.forEach((pluginHandle) => {
pluginHandle.start();
});
});
hvigor_1.hvigor.buildFinished(() => {
Logger_1.Logger.info('buildFinished deleteFile exec...');
this.hmRouterPluginSet.forEach((pluginHandle) => {
if (pluginHandle.config.saveGeneratedFile) {
Logger_1.Logger.info(pluginHandle.config.moduleName + ' saveGeneratedFile is true, skip deleting');
return;
}
this.deleteRouterMapFile(pluginHandle);
this.deleteRawFile(pluginHandle);
this.deleteGeneratorFile(pluginHandle);
if (pluginHandle.config.autoObfuscation) {
this.deleteObfuscationFile(pluginHandle);
}
});
HMRouterPluginMgrInstance = null;
TsAstUtil_1.TsAstUtil.clearProject();
});
}
registerHMRouterPlugin(node, pluginId) {
const moduleContext = node.getContext(pluginId);
if (!moduleContext) {
Logger_1.Logger.error(Logger_1.PluginError.ERR_ERROR_CONFIG, node.getNodePath());
throw new Error('moduleContext is null');
}
let pluginHandle = new HMRouterPluginHandle_1.HMRouterPluginHandle(node, moduleContext);
let packageJson = FileUtil_1.default.readJson5(FileUtil_1.default.pathResolve(node.getNodePath(), 'oh-package.json5'));
if (pluginId === hvigor_ohos_plugin_1.OhosPluginId.OHOS_HAP_PLUGIN) {
pluginHandle.config.moduleName = packageJson.name;
}
if (pluginId === hvigor_ohos_plugin_1.OhosPluginId.OHOS_HSP_PLUGIN) {
PluginStore_1.default.getInstance().hspModuleNames.push(packageJson.name);
}
this.hmRouterPluginSet.add(pluginHandle);
}
deleteGeneratorFile(pluginHandle) {
let builderDirPath = pluginHandle.config.getBuilderDir();
if (FileUtil_1.default.exist(builderDirPath)) {
FileUtil_1.default.rmSync(builderDirPath, {
recursive: true
});
Logger_1.Logger.info(pluginHandle.config.modulePath + ' delete builder dir');
}
}
deleteRouterMapFile(pluginHandle) {
let routerMapDirPath = pluginHandle.config.getRouterMapDir();
if (FileUtil_1.default.exist(routerMapDirPath)) {
FileUtil_1.default.unlinkSync(routerMapDirPath);
Logger_1.Logger.info(routerMapDirPath + ' delete hm_router_map.json');
}
}
deleteRawFile(pluginHandle) {
let rawFilePath = pluginHandle.config.getRawFilePath();
if (FileUtil_1.default.exist(rawFilePath)) {
FileUtil_1.default.unlinkSync(rawFilePath);
Logger_1.Logger.info(pluginHandle.config.modulePath + ' delete rawfile hm_router_map.json');
}
}
deleteObfuscationFile(pluginHandle) {
let obfuscationFilePath = pluginHandle.config.getObfuscationFilePath();
if (FileUtil_1.default.exist(obfuscationFilePath)) {
FileUtil_1.default.unlinkSync(obfuscationFilePath);
Logger_1.Logger.info(pluginHandle.config.modulePath + ' delete obfuscation hmrouter-obfuscation-rules.txt');
}
let consumerRulesPath = pluginHandle.config.getConsumerRulesFilePath();
if (FileUtil_1.default.exist(consumerRulesPath)) {
FileUtil_1.default.unlinkSync(consumerRulesPath);
Logger_1.Logger.info(pluginHandle.config.modulePath + ' delete hmrouter-consumer-rules.txt');
}
}
}
let HMRouterPluginMgrInstance = null;
function hapPlugin() {
return {
pluginId: CommonConstants_1.default.HAP_PLUGIN_ID,
apply(node) {
if (!HMRouterPluginMgrInstance) {
HMRouterPluginMgrInstance = new HMRouterPluginMgr();
}
HMRouterPluginMgrInstance.registerHMRouterPlugin(node, hvigor_ohos_plugin_1.OhosPluginId.OHOS_HAP_PLUGIN);
}
};
}
exports.hapPlugin = hapPlugin;
function hspPlugin() {
return {
pluginId: CommonConstants_1.default.HSP_PLUGIN_ID,
apply(node) {
if (!HMRouterPluginMgrInstance) {
HMRouterPluginMgrInstance = new HMRouterPluginMgr();
}
HMRouterPluginMgrInstance.registerHMRouterPlugin(node, hvigor_ohos_plugin_1.OhosPluginId.OHOS_HSP_PLUGIN);
}
};
}
exports.hspPlugin = hspPlugin;
function harPlugin() {
return {
pluginId: CommonConstants_1.default.HAR_PLUGIN_ID,
apply(node) {
if (!HMRouterPluginMgrInstance) {
HMRouterPluginMgrInstance = new HMRouterPluginMgr();
}
HMRouterPluginMgrInstance.registerHMRouterPlugin(node, hvigor_ohos_plugin_1.OhosPluginId.OHOS_HAR_PLUGIN);
}
};
}
exports.harPlugin = harPlugin;