@hadss/hmrouter-plugin 1.2.0-beta.0 → 1.2.0-beta.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (42) hide show
  1. package/README.md +8 -0
  2. package/dist/HMRouterPluginManager.js +9 -2
  3. package/dist/framework/core/PluginExecutionController.d.ts +11 -0
  4. package/dist/framework/core/PluginExecutionController.js +129 -22
  5. package/dist/framework/utils/FileUtil.d.ts +4 -0
  6. package/dist/framework/utils/FileUtil.js +11 -0
  7. package/dist/hmrouter_extension/config/HMRouterPluginConfig.js +3 -4
  8. package/dist/hmrouter_extension/processor/CodeGenerationProcessor.js +2 -1
  9. package/dist/hmrouter_extension/processor/InitializerProcessor.d.ts +1 -0
  10. package/dist/hmrouter_extension/processor/InitializerProcessor.js +29 -5
  11. package/dist/hmrouter_extension/processor/ObfuscationProcessor.js +1 -1
  12. package/dist/hmrouter_extension/processor/ResourceProcessor.js +14 -0
  13. package/dist/hmrouter_extension/processor/RouterMapBuildingProcessor.js +2 -1
  14. package/package.json +1 -1
  15. package/dist/HMRouterAnalyzer.d.ts +0 -31
  16. package/dist/HMRouterAnalyzer.js +0 -343
  17. package/dist/HMRouterHvigorPlugin.d.ts +0 -15
  18. package/dist/HMRouterHvigorPlugin.js +0 -153
  19. package/dist/HMRouterPluginConfig.d.ts +0 -39
  20. package/dist/HMRouterPluginConfig.js +0 -74
  21. package/dist/HMRouterPluginHandle.d.ts +0 -23
  22. package/dist/HMRouterPluginHandle.js +0 -222
  23. package/dist/common/Logger.d.ts +0 -13
  24. package/dist/common/Logger.js +0 -55
  25. package/dist/common/PluginModel.d.ts +0 -51
  26. package/dist/common/PluginModel.js +0 -23
  27. package/dist/constants/CommonConstants.d.ts +0 -39
  28. package/dist/constants/CommonConstants.js +0 -46
  29. package/dist/constants/ConfigConstants.d.ts +0 -12
  30. package/dist/constants/ConfigConstants.js +0 -16
  31. package/dist/constants/TaskConstants.d.ts +0 -9
  32. package/dist/constants/TaskConstants.js +0 -13
  33. package/dist/store/PluginStore.d.ts +0 -14
  34. package/dist/store/PluginStore.js +0 -20
  35. package/dist/utils/FileUtil.d.ts +0 -13
  36. package/dist/utils/FileUtil.js +0 -20
  37. package/dist/utils/ObfuscationUtil.d.ts +0 -4
  38. package/dist/utils/ObfuscationUtil.js +0 -34
  39. package/dist/utils/StringUtil.d.ts +0 -3
  40. package/dist/utils/StringUtil.js +0 -18
  41. package/dist/utils/TsAstUtil.d.ts +0 -9
  42. package/dist/utils/TsAstUtil.js +0 -97
@@ -1,343 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.AnalyzerService = exports.AnalyzerController = void 0;
7
- const ts_morph_1 = require("ts-morph");
8
- const Logger_1 = require("./common/Logger");
9
- const TsAstUtil_1 = require("./utils/TsAstUtil");
10
- const CommonConstants_1 = __importDefault(require("./constants/CommonConstants"));
11
- const FileUtil_1 = __importDefault(require("./utils/FileUtil"));
12
- const ConfigConstants_1 = __importDefault(require("./constants/ConfigConstants"));
13
- const PluginStore_1 = __importDefault(require("./store/PluginStore"));
14
- class AnalyzerController {
15
- constructor() {
16
- this.analyzeResult = new Set();
17
- }
18
- analyzeFile(sourceFilePath, config) {
19
- let analyzerService = new AnalyzerService(sourceFilePath, config);
20
- analyzerService.start();
21
- analyzerService.getResult().forEach(item => {
22
- item.pageSourceFile = sourceFilePath;
23
- this.analyzeResult.add(item);
24
- });
25
- this.parseConstants();
26
- }
27
- parseConstants() {
28
- this.analyzeResult.forEach((item) => {
29
- Object.getOwnPropertyNames(item).forEach((key) => {
30
- let propertyValue = Reflect.get(item, key);
31
- propertyValue = this.parsePropertyValue(propertyValue);
32
- if (propertyValue === '') {
33
- Logger_1.Logger.error(Logger_1.PluginError.ERR_NOT_EMPTY_STRING);
34
- throw new Error('constants value cannot be an empty string, filePath:' + item.pageSourceFile);
35
- }
36
- Reflect.set(item, key, propertyValue);
37
- });
38
- });
39
- }
40
- parsePropertyValue(propertyValue) {
41
- if (propertyValue.type === 'constant') {
42
- try {
43
- return TsAstUtil_1.TsAstUtil.parseConstantValue(TsAstUtil_1.TsAstUtil.getSourceFile(propertyValue.variableFilePath), propertyValue.variableName);
44
- }
45
- catch (error) {
46
- Logger_1.Logger.error(`Failed to parse constant ${propertyValue.variableName}: ${error.message}`);
47
- throw new Error(`Failed to parse constant ${propertyValue.variableName}: ${error.message}`);
48
- }
49
- }
50
- else if (propertyValue.type === 'object') {
51
- try {
52
- return TsAstUtil_1.TsAstUtil.parseConstantValue(TsAstUtil_1.TsAstUtil.getSourceFile(propertyValue.variableFilePath), propertyValue.variableName, propertyValue.propertyName);
53
- }
54
- catch (error) {
55
- Logger_1.Logger.error(`Failed to parse object property ${propertyValue.variableName}.${propertyValue.propertyName}: ${error.message}`);
56
- throw new Error(`Failed to parse object property ${propertyValue.variableName}.${propertyValue.propertyName}: ${error.message}`);
57
- }
58
- }
59
- else if (propertyValue.type === 'array') {
60
- return propertyValue.value.map((item) => {
61
- return this.parsePropertyValue(item);
62
- });
63
- }
64
- else {
65
- return propertyValue;
66
- }
67
- }
68
- getAnalyzeResultSet() {
69
- return this.analyzeResult;
70
- }
71
- clearAnalyzeResultSet() {
72
- this.analyzeResult.clear();
73
- }
74
- }
75
- exports.AnalyzerController = AnalyzerController;
76
- class AnalyzerService {
77
- constructor(sourceFilePath, config) {
78
- this.analyzerResultSet = new Set();
79
- this.importMap = new Map();
80
- this.sourceFilePath = sourceFilePath;
81
- this.sourceFile = TsAstUtil_1.TsAstUtil.getSourceFile(sourceFilePath);
82
- this.config = config;
83
- }
84
- start() {
85
- this.analyzeImport();
86
- this.analyzeRouter();
87
- this.analyzeComponent();
88
- this.parseFileByLineOrder();
89
- }
90
- getResult() {
91
- let HMRouterNum = 0;
92
- this.analyzerResultSet.forEach((analyzerResult) => {
93
- if (analyzerResult.annotation === CommonConstants_1.default.ROUTER_ANNOTATION) {
94
- HMRouterNum++;
95
- }
96
- });
97
- if (HMRouterNum > 1) {
98
- Logger_1.Logger.error(Logger_1.PluginError.ERR_REPEAT_ANNOTATION, this.sourceFilePath);
99
- throw new Error(`File:${this.sourceFilePath} exists more than one @HMRouter annotation`);
100
- }
101
- return this.analyzerResultSet;
102
- }
103
- analyzeImport() {
104
- this.sourceFile.getImportDeclarations().forEach((importDeclaration) => {
105
- const moduleSpecifier = importDeclaration.getModuleSpecifierValue();
106
- const namedImports = importDeclaration.getNamedImports().map((namedImport) => namedImport.getName());
107
- const defaultImport = importDeclaration.getDefaultImport()?.getText();
108
- const namespaceImport = importDeclaration.getNamespaceImport()?.getText();
109
- const importNames = [];
110
- if (namedImports.length > 0) {
111
- importNames.push(...namedImports);
112
- }
113
- if (defaultImport) {
114
- importNames.push(defaultImport);
115
- }
116
- if (namespaceImport) {
117
- importNames.push(namespaceImport);
118
- }
119
- if (importNames.length > 0) {
120
- if (this.importMap.has(moduleSpecifier)) {
121
- const existingImports = this.importMap.get(moduleSpecifier);
122
- this.importMap.set(moduleSpecifier, [...new Set([...existingImports, ...importNames])]);
123
- }
124
- else {
125
- this.importMap.set(moduleSpecifier, importNames);
126
- }
127
- }
128
- });
129
- }
130
- analyzeRouter() {
131
- let viewNameArr = this.sourceFile
132
- .getChildrenOfKind(ts_morph_1.SyntaxKind.ExpressionStatement)
133
- .map((node) => {
134
- return node.getText();
135
- })
136
- .filter((text) => {
137
- return text != 'struct';
138
- });
139
- this.sourceFile.getChildrenOfKind(ts_morph_1.SyntaxKind.MissingDeclaration).forEach((node, index) => {
140
- node.getChildrenOfKind(ts_morph_1.SyntaxKind.Decorator).forEach((decorator) => {
141
- this.addToResultSet(decorator, viewNameArr[index]);
142
- });
143
- });
144
- this.sourceFile.getExportAssignments().forEach((exportAssignment, index) => {
145
- exportAssignment.getDescendantsOfKind(ts_morph_1.SyntaxKind.Decorator).forEach((decorator) => {
146
- let result = this.addToResultSet(decorator, viewNameArr[index]);
147
- result.isDefaultExport = true;
148
- });
149
- });
150
- }
151
- parseFileByLineOrder() {
152
- const statements = this.sourceFile.getStatements();
153
- const sortedStatements = statements.sort((a, b) => a.getStart() - b.getStart());
154
- let HMRouterExists = false;
155
- let useNavDst = false;
156
- sortedStatements.forEach((statement) => {
157
- if (statement.getKind() === ts_morph_1.SyntaxKind.MissingDeclaration && statement.getText().includes('HMRouter')) {
158
- HMRouterExists = true;
159
- useNavDst = false;
160
- const text = statement.getText();
161
- if (/(useNavDst\s*:\s*true)/.test(text)) {
162
- useNavDst = true;
163
- Logger_1.Logger.info('Found HMRouter with useNavDst=true, skipping NavDestination check');
164
- }
165
- }
166
- if (statement.getKind() === ts_morph_1.SyntaxKind.Block && HMRouterExists) {
167
- HMRouterExists = false;
168
- if (useNavDst) {
169
- return;
170
- }
171
- let reg = new RegExp(/NavDestination\s*\(\s*\)/);
172
- let text = statement.getText();
173
- const cleanedCodeBlock = text
174
- .replace(/(["'`]).*?\1/g, '')
175
- .replace(/\/\/.*|\/\*[\s\S]*?\*\//g, '');
176
- if (reg.test(cleanedCodeBlock)) {
177
- Logger_1.Logger.error(Logger_1.PluginError.ERR_WRONG_DECORATION);
178
- throw new Error('NavDestination is not allowed in HMRouter, filePath:' + this.sourceFilePath);
179
- }
180
- }
181
- });
182
- }
183
- analyzeComponent() {
184
- this.sourceFile.getClasses().forEach((cls) => {
185
- cls.getDecorators().forEach((decorator) => {
186
- if (this.config.annotation.includes(decorator.getName())) {
187
- this.addToResultSet(decorator, cls.getName());
188
- }
189
- });
190
- cls.getMethods().forEach((method) => {
191
- method.getDecorators().forEach((decorator) => {
192
- let serviceResult = this.addToResultSet(decorator, cls.getName());
193
- serviceResult.functionName = method.getName();
194
- });
195
- });
196
- });
197
- }
198
- addToResultSet(decorator, componentName) {
199
- let decoratorResult = this.parseDecorator(decorator);
200
- decoratorResult.name = componentName;
201
- if (decoratorResult.annotation) {
202
- this.analyzerResultSet.add(decoratorResult);
203
- }
204
- return decoratorResult;
205
- }
206
- parseDecorator(decorator) {
207
- let decoratorResult = {};
208
- let decoratorName = decorator.getName();
209
- if (this.config.annotation.includes(decoratorName)) {
210
- decoratorResult.annotation = decoratorName;
211
- let args = this.parseDecoratorArguments(decorator);
212
- Object.assign(decoratorResult, args);
213
- }
214
- return decoratorResult;
215
- }
216
- parseDecoratorArguments(decorator) {
217
- let argResult = {};
218
- decorator.getArguments().map((arg) => {
219
- const objLiteral = arg.asKind(ts_morph_1.SyntaxKind.ObjectLiteralExpression);
220
- if (objLiteral) {
221
- objLiteral.getProperties().forEach((prop) => {
222
- let propertyName = prop.getName();
223
- let propertyValue = this.parseIdentifierPropertyValue(prop.getInitializer());
224
- Reflect.set(argResult, propertyName, propertyValue);
225
- });
226
- }
227
- });
228
- return argResult;
229
- }
230
- parseIdentifierPropertyValue(value) {
231
- switch (value.getKind()) {
232
- case ts_morph_1.SyntaxKind.Identifier:
233
- if (value.getText() === 'undefined') {
234
- throw new Error(`Invalid property value, in ${this.sourceFilePath}`);
235
- }
236
- return {
237
- type: 'constant',
238
- variableName: value.getText(),
239
- variableFilePath: this.getVariableFilePath(value.getText()),
240
- };
241
- case ts_morph_1.SyntaxKind.PropertyAccessExpression:
242
- return {
243
- type: 'object',
244
- variableName: value.getExpression().getText(),
245
- propertyName: value?.getName(),
246
- variableFilePath: this.getVariableFilePath(value?.getExpression().getText()),
247
- };
248
- case ts_morph_1.SyntaxKind.ArrayLiteralExpression:
249
- return {
250
- type: 'array',
251
- value: value
252
- .asKind(ts_morph_1.SyntaxKind.ArrayLiteralExpression)
253
- ?.getElements()
254
- .map((item) => this.parseIdentifierPropertyValue(item)),
255
- };
256
- default:
257
- return this.parsePrimitiveValue(value);
258
- }
259
- }
260
- parsePrimitiveValue(value) {
261
- let propertyValue;
262
- switch (value.getKind()) {
263
- case ts_morph_1.SyntaxKind.StringLiteral:
264
- propertyValue = value.asKind(ts_morph_1.SyntaxKind.StringLiteral)?.getLiteralValue();
265
- break;
266
- case ts_morph_1.SyntaxKind.NumericLiteral:
267
- propertyValue = value.asKind(ts_morph_1.SyntaxKind.NumericLiteral)?.getLiteralValue();
268
- break;
269
- case ts_morph_1.SyntaxKind.TrueKeyword:
270
- propertyValue = true;
271
- break;
272
- case ts_morph_1.SyntaxKind.FalseKeyword:
273
- propertyValue = false;
274
- break;
275
- case ts_morph_1.SyntaxKind.ArrayLiteralExpression:
276
- propertyValue = value
277
- .asKind(ts_morph_1.SyntaxKind.ArrayLiteralExpression)
278
- ?.getElements()
279
- .map((item) => item.asKind(ts_morph_1.SyntaxKind.StringLiteral)?.getLiteralValue());
280
- break;
281
- }
282
- return propertyValue;
283
- }
284
- getVariableFilePath(variableName) {
285
- let filePath = '';
286
- let classesNames = this.sourceFile.getClasses().map((classes) => {
287
- return classes.getName();
288
- });
289
- let variableNames = this.sourceFile.getVariableDeclarations().map((variableDeclaration) => {
290
- return variableDeclaration.getName();
291
- });
292
- if (classesNames.includes(variableName) || variableNames.includes(variableName)) {
293
- return this.sourceFilePath;
294
- }
295
- for (const [importPath, importNames] of this.importMap.entries()) {
296
- if (importNames.includes(variableName)) {
297
- try {
298
- let currentDir = FileUtil_1.default.pathResolve(this.sourceFilePath, CommonConstants_1.default.PARENT_DELIMITER);
299
- let tempFilePath = FileUtil_1.default.pathResolve(currentDir, importPath + CommonConstants_1.default.ETS_SUFFIX);
300
- if (FileUtil_1.default.exist(tempFilePath)) {
301
- filePath = tempFilePath;
302
- }
303
- else {
304
- try {
305
- filePath = this.getOtherModuleVariableFilePath(importPath, variableName);
306
- }
307
- catch (error) {
308
- Logger_1.Logger.warn(`Cannot find variable ${variableName} in module ${importPath}: ${error.message}`);
309
- }
310
- }
311
- }
312
- catch (error) {
313
- Logger_1.Logger.warn(`Error parsing import path ${importPath}: ${error.message}`);
314
- }
315
- }
316
- }
317
- if (!filePath) {
318
- Logger_1.Logger.warn(`Cannot find file path for variable ${variableName}`);
319
- }
320
- return filePath;
321
- }
322
- getOtherModuleVariableFilePath(moduleName, variableName) {
323
- let moduleFilePath = FileUtil_1.default.pathResolve(this.config.modulePath, CommonConstants_1.default.OH_MODULE_PATH, moduleName, ConfigConstants_1.default.DEFAULT_SCAN_DIR);
324
- if (!FileUtil_1.default.exist(moduleFilePath)) {
325
- moduleFilePath = FileUtil_1.default.pathResolve(PluginStore_1.default.getInstance().projectFilePath, CommonConstants_1.default.OH_MODULE_PATH, moduleName, ConfigConstants_1.default.DEFAULT_SCAN_DIR);
326
- }
327
- let variableMap;
328
- if (PluginStore_1.default.getInstance().variableCache.has(moduleName)) {
329
- variableMap = PluginStore_1.default.getInstance().variableCache.get(moduleName);
330
- }
331
- else {
332
- variableMap = TsAstUtil_1.TsAstUtil.parseCrossModuleVariable(moduleFilePath);
333
- PluginStore_1.default.getInstance().variableCache.set(moduleName, variableMap);
334
- }
335
- for (let [key, value] of variableMap) {
336
- if (value.includes(variableName)) {
337
- return key;
338
- }
339
- }
340
- throw new Error(`Unknown variable ${variableName} in ${moduleName}`);
341
- }
342
- }
343
- exports.AnalyzerService = AnalyzerService;
@@ -1,15 +0,0 @@
1
- import { AnalyzerResultLike, HMRouterResult, RouterInfo } from './common/PluginModel';
2
- import { CustomPageTemplateImpl, HMRouterPluginConfig } from './HMRouterPluginConfig';
3
- export declare class HMRouterHvigorPlugin {
4
- config: HMRouterPluginConfig;
5
- routerMap: RouterInfo[];
6
- scanFiles: string[];
7
- private analyzerController;
8
- constructor(config: HMRouterPluginConfig);
9
- analyzeAnnotation(): void;
10
- generateRouterMap(): void;
11
- matchedPath(filePath: string, customPageTemplate: CustomPageTemplateImpl[], defaultTplFilePath: string): string;
12
- pushRouterInfo(analyzeResult: AnalyzerResultLike): void;
13
- generateBuilder(analyzeResult: HMRouterResult, pageSourceFile: string, tempFilePath: string): string;
14
- deepScan(scanPath: string, filePath: string): void;
15
- }
@@ -1,153 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.HMRouterHvigorPlugin = void 0;
7
- const micromatch_1 = __importDefault(require("micromatch"));
8
- const ejs_1 = __importDefault(require("ejs"));
9
- const PluginModel_1 = require("./common/PluginModel");
10
- const Logger_1 = require("./common/Logger");
11
- const CommonConstants_1 = __importDefault(require("./constants/CommonConstants"));
12
- const FileUtil_1 = __importDefault(require("./utils/FileUtil"));
13
- const StringUtil_1 = require("./utils/StringUtil");
14
- const HMRouterAnalyzer_1 = require("./HMRouterAnalyzer");
15
- class HMRouterHvigorPlugin {
16
- constructor(config) {
17
- this.routerMap = [];
18
- this.scanFiles = [];
19
- this.analyzerController = new HMRouterAnalyzer_1.AnalyzerController();
20
- this.config = config;
21
- }
22
- analyzeAnnotation() {
23
- this.config.scanDir.forEach((dir) => {
24
- const scanPath = this.config.getScanPath(dir);
25
- this.deepScan(scanPath, '');
26
- });
27
- Logger_1.Logger.info(`Scanned ${this.scanFiles.length} files`, this.scanFiles);
28
- this.scanFiles.forEach((filePath) => {
29
- if (filePath.endsWith(CommonConstants_1.default.ETS_SUFFIX)) {
30
- this.analyzerController.analyzeFile(filePath, this.config);
31
- }
32
- });
33
- this.analyzerController.getAnalyzeResultSet().forEach((analyzerResult) => {
34
- this.pushRouterInfo(analyzerResult);
35
- });
36
- }
37
- generateRouterMap() {
38
- let set = new Set();
39
- this.routerMap.forEach((item) => {
40
- if (set.has(item.name)) {
41
- Logger_1.Logger.error(Logger_1.PluginError.ERR_DUPLICATE_NAME, item.name);
42
- throw new Error(`Route page name: ${item.name} is duplicated`);
43
- }
44
- else {
45
- set.add(item.name);
46
- }
47
- });
48
- let routerMap = {
49
- routerMap: this.routerMap.map((item) => {
50
- if (item.customData && item.customData.annotation) {
51
- delete item.customData.annotation;
52
- delete item.customData.pageSourceFile;
53
- delete item.customData.isDefaultExport;
54
- }
55
- return item;
56
- })
57
- };
58
- const routerMapJsonStr = JSON.stringify(routerMap, null, 2);
59
- const routerMapFilePath = this.config.getRouterMapDir();
60
- FileUtil_1.default.ensureFileSync(routerMapFilePath);
61
- FileUtil_1.default.writeFileSync(routerMapFilePath, routerMapJsonStr);
62
- Logger_1.Logger.info(`hm_router_map.json has been generated in ${routerMapFilePath}`);
63
- this.analyzerController.clearAnalyzeResultSet();
64
- }
65
- matchedPath(filePath, customPageTemplate, defaultTplFilePath) {
66
- for (const template of customPageTemplate) {
67
- if (micromatch_1.default.isMatch(filePath, template.srcPath)) {
68
- return FileUtil_1.default.pathResolve(this.config.configDir, template.templatePath);
69
- }
70
- }
71
- return defaultTplFilePath;
72
- }
73
- pushRouterInfo(analyzeResult) {
74
- let pageSourceFile = this.config
75
- .getRelativeSourcePath(analyzeResult.pageSourceFile)
76
- .replaceAll(CommonConstants_1.default.FILE_SEPARATOR, CommonConstants_1.default.DELIMITER);
77
- switch (analyzeResult.annotation) {
78
- case CommonConstants_1.default.ROUTER_ANNOTATION:
79
- let generatorFilePath = this.generateBuilder(analyzeResult, pageSourceFile, this.matchedPath(pageSourceFile, this.config.customPageTemplate, this.config.getDefaultTplFilePath()));
80
- let pageUrl = analyzeResult.pageUrl;
81
- this.routerMap.push(new PluginModel_1.RouterInfo(pageUrl, generatorFilePath, analyzeResult.name + 'Builder', analyzeResult));
82
- break;
83
- case CommonConstants_1.default.ANIMATOR_ANNOTATION:
84
- let animatorName = CommonConstants_1.default.ANIMATOR_PREFIX + analyzeResult.animatorName;
85
- this.routerMap.push(new PluginModel_1.RouterInfo(animatorName, pageSourceFile, '', analyzeResult));
86
- break;
87
- case CommonConstants_1.default.INTERCEPTOR_ANNOTATION:
88
- let interceptorName = CommonConstants_1.default.INTERCEPTOR_PREFIX + analyzeResult.interceptorName;
89
- this.routerMap.push(new PluginModel_1.RouterInfo(interceptorName, pageSourceFile, '', analyzeResult));
90
- break;
91
- case CommonConstants_1.default.LIFECYCLE_ANNOTATION:
92
- let lifecycleName = CommonConstants_1.default.LIFECYCLE_PREFIX + analyzeResult.lifecycleName;
93
- this.routerMap.push(new PluginModel_1.RouterInfo(lifecycleName, pageSourceFile, '', analyzeResult));
94
- break;
95
- case CommonConstants_1.default.SERVICE_ANNOTATION:
96
- let serviceName = CommonConstants_1.default.SERVICE_PREFIX + analyzeResult.serviceName;
97
- this.routerMap.push(new PluginModel_1.RouterInfo(serviceName, pageSourceFile, '', analyzeResult));
98
- break;
99
- case CommonConstants_1.default.SERVICE_PROVIDE_ANNOTATION:
100
- let className = CommonConstants_1.default.SERVICE_PROVIDE_PREFIX + analyzeResult.serviceName;
101
- this.routerMap.push(new PluginModel_1.RouterInfo(className, pageSourceFile, '', analyzeResult));
102
- break;
103
- }
104
- }
105
- generateBuilder(analyzeResult, pageSourceFile, tempFilePath) {
106
- let importPath = this.config
107
- .getRelativeBuilderPath(pageSourceFile)
108
- .replaceAll(CommonConstants_1.default.FILE_SEPARATOR, CommonConstants_1.default.DELIMITER)
109
- .replaceAll(CommonConstants_1.default.ETS_SUFFIX, '');
110
- let generatorViewName = CommonConstants_1.default.VIEW_NAME_PREFIX +
111
- analyzeResult.name +
112
- StringUtil_1.StringUtil.stringToHashCode(analyzeResult.pageUrl);
113
- const templateModel = new PluginModel_1.TemplateModel(analyzeResult.pageUrl, importPath, analyzeResult.name, !!analyzeResult.dialog, generatorViewName, analyzeResult.isDefaultExport);
114
- if (analyzeResult.useNavDst === true) {
115
- Logger_1.Logger.info(`Using custom template for ${analyzeResult.name} with useNavDst=true`);
116
- const customTplPath = FileUtil_1.default.pathResolve(__dirname, CommonConstants_1.default.PARENT_DELIMITER + CommonConstants_1.default.CUSTOM_BUILDER_TEMPLATE);
117
- if (FileUtil_1.default.exist(customTplPath)) {
118
- tempFilePath = customTplPath;
119
- }
120
- else {
121
- Logger_1.Logger.info(`Custom template not found at ${customTplPath}, falling back to default template`);
122
- }
123
- }
124
- if (!FileUtil_1.default.exist(tempFilePath)) {
125
- throw new Error('Invalid template path: ' + tempFilePath);
126
- }
127
- const tpl = FileUtil_1.default.readFileSync(tempFilePath).toString();
128
- const templateStr = ejs_1.default.render(tpl, templateModel);
129
- const generatorFilePath = this.config.getGeneratedFilePath(templateModel.generatorViewName);
130
- FileUtil_1.default.ensureFileSync(generatorFilePath);
131
- FileUtil_1.default.writeFileSync(generatorFilePath, templateStr);
132
- Logger_1.Logger.info(`Builder ${templateModel.generatorViewName}.ets has been generated in ${generatorFilePath}`);
133
- return this.config
134
- .getBuilderFilePath(templateModel.generatorViewName)
135
- .replaceAll(CommonConstants_1.default.FILE_SEPARATOR, CommonConstants_1.default.DELIMITER);
136
- }
137
- deepScan(scanPath, filePath) {
138
- let resolvePath = FileUtil_1.default.pathResolve(scanPath, filePath);
139
- if (!FileUtil_1.default.exist(resolvePath)) {
140
- return;
141
- }
142
- if (FileUtil_1.default.isDictionary(resolvePath)) {
143
- const files = FileUtil_1.default.readdirSync(resolvePath);
144
- files.forEach((file) => {
145
- this.deepScan(resolvePath, file);
146
- });
147
- }
148
- else {
149
- this.scanFiles.push(resolvePath);
150
- }
151
- }
152
- }
153
- exports.HMRouterHvigorPlugin = HMRouterHvigorPlugin;
@@ -1,39 +0,0 @@
1
- export declare class HMRouterPluginConfig {
2
- moduleName: string;
3
- modulePath: string;
4
- configDir: string;
5
- scanDir: string[];
6
- routerMapDir: string;
7
- builderDir: string;
8
- annotation: string[];
9
- defaultPageTemplate: string;
10
- customPageTemplate: CustomPageTemplateImpl[];
11
- saveGeneratedFile: boolean;
12
- autoObfuscation: boolean;
13
- constructor(moduleName: string, modulePath: string, configDir: string, param: HMRouterPluginConfigParam);
14
- getScanPath(dir: string): string;
15
- getRelativeSourcePath(filePath: string): string;
16
- getRelativeBuilderPath(filePath: string): string;
17
- getGeneratedFilePath(generatorViewName: string): string;
18
- getBuilderDir(): string;
19
- getBuilderFilePath(generatorViewName: string): string;
20
- getRouterMapDir(): string;
21
- getModuleRouterMapFilePath(routerMapFileName: string): string;
22
- getRawFilePath(): string;
23
- getDefaultTplFilePath(): string;
24
- getObfuscationFilePath(): string;
25
- getConsumerRulesFilePath(): string;
26
- }
27
- export interface HMRouterPluginConfigParam {
28
- scanDir?: string[];
29
- routerMapDir?: string;
30
- builderDir?: string;
31
- autoObfuscation?: boolean;
32
- saveGeneratedFile?: boolean;
33
- defaultPageTemplate?: string;
34
- customPageTemplate?: CustomPageTemplateImpl[];
35
- }
36
- export interface CustomPageTemplateImpl {
37
- srcPath: string[];
38
- templatePath: string;
39
- }
@@ -1,74 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.HMRouterPluginConfig = void 0;
7
- const path_1 = __importDefault(require("path"));
8
- const FileUtil_1 = __importDefault(require("./utils/FileUtil"));
9
- const ConfigConstants_1 = __importDefault(require("./constants/ConfigConstants"));
10
- const CommonConstants_1 = __importDefault(require("./constants/CommonConstants"));
11
- class HMRouterPluginConfig {
12
- constructor(moduleName, modulePath, configDir, param) {
13
- this.moduleName = moduleName;
14
- this.modulePath = modulePath;
15
- this.configDir = configDir;
16
- this.scanDir = param.scanDir ? [...new Set(param.scanDir)] : [ConfigConstants_1.default.DEFAULT_SCAN_DIR];
17
- this.routerMapDir = param.routerMapDir ? param.routerMapDir : ConfigConstants_1.default.DEFAULT_ROUTER_MAP_DIR;
18
- this.builderDir = param.builderDir ? param.builderDir : ConfigConstants_1.default.DEFAULT_BUILD_DIR;
19
- this.annotation = [
20
- ConfigConstants_1.default.ROUTER_ANNOTATION,
21
- ConfigConstants_1.default.ANIMATOR_ANNOTATION,
22
- ConfigConstants_1.default.INTERCEPTOR_ANNOTATION,
23
- ConfigConstants_1.default.LIFECYCLE_ANNOTATION,
24
- ConfigConstants_1.default.SERVICE_ANNOTATION,
25
- ConfigConstants_1.default.SERVICE_PROVIDE_ANNOTATION
26
- ];
27
- this.defaultPageTemplate =
28
- param.defaultPageTemplate ? param.defaultPageTemplate : ConfigConstants_1.default.DEFAULT_BUILD_TPL;
29
- this.customPageTemplate = param.customPageTemplate ? param.customPageTemplate : [];
30
- this.saveGeneratedFile = !!param.saveGeneratedFile;
31
- this.autoObfuscation = !!param.autoObfuscation;
32
- }
33
- getScanPath(dir) {
34
- return FileUtil_1.default.pathResolve(this.modulePath, dir);
35
- }
36
- getRelativeSourcePath(filePath) {
37
- return path_1.default.relative(this.modulePath, filePath);
38
- }
39
- getRelativeBuilderPath(filePath) {
40
- return path_1.default.relative(this.builderDir, filePath);
41
- }
42
- getGeneratedFilePath(generatorViewName) {
43
- return FileUtil_1.default.pathResolve(this.modulePath, this.builderDir, generatorViewName + CommonConstants_1.default.ETS_SUFFIX);
44
- }
45
- getBuilderDir() {
46
- return FileUtil_1.default.pathResolve(this.modulePath, this.builderDir);
47
- }
48
- getBuilderFilePath(generatorViewName) {
49
- return path_1.default.join(this.builderDir, generatorViewName + CommonConstants_1.default.ETS_SUFFIX);
50
- }
51
- getRouterMapDir() {
52
- return FileUtil_1.default.pathResolve(this.modulePath, this.routerMapDir, CommonConstants_1.default.ROUTER_MAP_NAME);
53
- }
54
- getModuleRouterMapFilePath(routerMapFileName) {
55
- return FileUtil_1.default.pathResolve(this.modulePath, this.routerMapDir, routerMapFileName + CommonConstants_1.default.JSON_SUFFIX);
56
- }
57
- getRawFilePath() {
58
- return FileUtil_1.default.pathResolve(this.modulePath, CommonConstants_1.default.RAWFILE_DIR);
59
- }
60
- getDefaultTplFilePath() {
61
- let templateFilePath = FileUtil_1.default.pathResolve(this.configDir, this.defaultPageTemplate);
62
- if (FileUtil_1.default.exist(templateFilePath)) {
63
- return templateFilePath;
64
- }
65
- return FileUtil_1.default.pathResolve(__dirname, CommonConstants_1.default.PARENT_DELIMITER + this.defaultPageTemplate);
66
- }
67
- getObfuscationFilePath() {
68
- return FileUtil_1.default.pathResolve(this.modulePath, CommonConstants_1.default.OBFUSCATION_FILE_NAME);
69
- }
70
- getConsumerRulesFilePath() {
71
- return FileUtil_1.default.pathResolve(this.modulePath, CommonConstants_1.default.CONSUMER_FILE_NAME);
72
- }
73
- }
74
- exports.HMRouterPluginConfig = HMRouterPluginConfig;
@@ -1,23 +0,0 @@
1
- import { HvigorNode } from '@ohos/hvigor';
2
- import { OhosHapContext, OhosHarContext, OhosHspContext } from '@ohos/hvigor-ohos-plugin';
3
- import { HMRouterPluginConfig } from './HMRouterPluginConfig';
4
- export declare class HMRouterPluginHandle {
5
- readonly config: HMRouterPluginConfig;
6
- private readonly node;
7
- private readonly moduleContext;
8
- private readonly appContext;
9
- private readonly plugin;
10
- constructor(node: HvigorNode, moduleContext: OhosHapContext | OhosHarContext | OhosHspContext);
11
- start(): void;
12
- private generateObfuscationFileTask;
13
- private isEnableObfuscation;
14
- private copyRouterMapToRawFileTask;
15
- private writeHspModuleName;
16
- private getRemoteHspModuleNames;
17
- private taskExec;
18
- private updateModuleJsonOpt;
19
- private updateBuildProfileOpt;
20
- private pushRouterInfo;
21
- private readConfig;
22
- private ensureNestedObject;
23
- }