@hadss/turbo-trans-json-plugin 1.0.0-rc.0

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 (195) hide show
  1. package/LICENSE +177 -0
  2. package/README.md +253 -0
  3. package/dist/core/Types.d.ts +196 -0
  4. package/dist/core/Types.js +47 -0
  5. package/dist/core/analyzers/ClassAnalyzer.d.ts +23 -0
  6. package/dist/core/analyzers/ClassAnalyzer.js +480 -0
  7. package/dist/core/analyzers/CustomTypeAnalyzer.d.ts +21 -0
  8. package/dist/core/analyzers/CustomTypeAnalyzer.js +243 -0
  9. package/dist/core/analyzers/SendableMergeChecker.d.ts +9 -0
  10. package/dist/core/analyzers/SendableMergeChecker.js +100 -0
  11. package/dist/core/analyzers/SendableMergeabilityRegistry.d.ts +7 -0
  12. package/dist/core/analyzers/SendableMergeabilityRegistry.js +23 -0
  13. package/dist/core/constants/DecoratorConstants.d.ts +7 -0
  14. package/dist/core/constants/DecoratorConstants.js +13 -0
  15. package/dist/core/constants/PathConstants.d.ts +6 -0
  16. package/dist/core/constants/PathConstants.js +10 -0
  17. package/dist/core/constants/StringConstants.d.ts +83 -0
  18. package/dist/core/constants/StringConstants.js +87 -0
  19. package/dist/core/constants/index.d.ts +3 -0
  20. package/dist/core/constants/index.js +19 -0
  21. package/dist/core/handlers/BaseTypeHandler.d.ts +23 -0
  22. package/dist/core/handlers/BaseTypeHandler.js +57 -0
  23. package/dist/core/handlers/CustomClassHandler.d.ts +21 -0
  24. package/dist/core/handlers/CustomClassHandler.js +191 -0
  25. package/dist/core/handlers/DateHandler.d.ts +2 -0
  26. package/dist/core/handlers/DateHandler.js +60 -0
  27. package/dist/core/handlers/DecimalHandler.d.ts +2 -0
  28. package/dist/core/handlers/DecimalHandler.js +60 -0
  29. package/dist/core/handlers/EnumHandler.d.ts +2 -0
  30. package/dist/core/handlers/EnumHandler.js +89 -0
  31. package/dist/core/handlers/GenericContainerHandler.d.ts +2 -0
  32. package/dist/core/handlers/GenericContainerHandler.js +440 -0
  33. package/dist/core/handlers/GenericHandler.d.ts +18 -0
  34. package/dist/core/handlers/GenericHandler.js +92 -0
  35. package/dist/core/handlers/HandlerBootstrap.d.ts +2 -0
  36. package/dist/core/handlers/HandlerBootstrap.js +28 -0
  37. package/dist/core/handlers/ITypeHandler.d.ts +23 -0
  38. package/dist/core/handlers/ITypeHandler.js +8 -0
  39. package/dist/core/handlers/PrimitiveHandler.d.ts +2 -0
  40. package/dist/core/handlers/PrimitiveHandler.js +127 -0
  41. package/dist/core/handlers/TupleHandler.d.ts +2 -0
  42. package/dist/core/handlers/TupleHandler.js +98 -0
  43. package/dist/core/handlers/TypeHandlerRegistry.d.ts +20 -0
  44. package/dist/core/handlers/TypeHandlerRegistry.js +113 -0
  45. package/dist/core/handlers/UnionTypeHandler.d.ts +2 -0
  46. package/dist/core/handlers/UnionTypeHandler.js +263 -0
  47. package/dist/core/handlers/index.d.ts +2 -0
  48. package/dist/core/handlers/index.js +5 -0
  49. package/dist/core/import-rewrite/services/BuildProfileUpdater.d.ts +8 -0
  50. package/dist/core/import-rewrite/services/BuildProfileUpdater.js +92 -0
  51. package/dist/core/import-rewrite/services/ImportRewriteService.d.ts +9 -0
  52. package/dist/core/import-rewrite/services/ImportRewriteService.js +61 -0
  53. package/dist/core/import-rewrite/services/ImportTransformService.d.ts +15 -0
  54. package/dist/core/import-rewrite/services/ImportTransformService.js +109 -0
  55. package/dist/core/import-rewrite/strategies/ImportScanStrategy.d.ts +17 -0
  56. package/dist/core/import-rewrite/strategies/ImportScanStrategy.js +137 -0
  57. package/dist/core/import-rewrite/types/ImportRewriteTypes.d.ts +17 -0
  58. package/dist/core/import-rewrite/types/ImportRewriteTypes.js +2 -0
  59. package/dist/core/index.d.ts +9 -0
  60. package/dist/core/index.js +27 -0
  61. package/dist/core/interfaces/ITask.d.ts +7 -0
  62. package/dist/core/interfaces/ITask.js +2 -0
  63. package/dist/core/interfaces/ITaskContext.d.ts +11 -0
  64. package/dist/core/interfaces/ITaskContext.js +2 -0
  65. package/dist/core/interfaces/index.d.ts +2 -0
  66. package/dist/core/interfaces/index.js +2 -0
  67. package/dist/core/logger/Logger.d.ts +13 -0
  68. package/dist/core/logger/Logger.js +78 -0
  69. package/dist/core/services/CodeAnalysisService.d.ts +7 -0
  70. package/dist/core/services/CodeAnalysisService.js +43 -0
  71. package/dist/core/services/CodeGenerationEngine.d.ts +12 -0
  72. package/dist/core/services/CodeGenerationEngine.js +102 -0
  73. package/dist/core/services/CodeGenerationService/CodeGenerationService.d.ts +13 -0
  74. package/dist/core/services/CodeGenerationService/CodeGenerationService.js +110 -0
  75. package/dist/core/services/CodeGenerationService/generators/MergedSendableClassGenerator.d.ts +13 -0
  76. package/dist/core/services/CodeGenerationService/generators/MergedSendableClassGenerator.js +119 -0
  77. package/dist/core/services/CodeGenerationService/generators/OriginalClassGenerator.d.ts +21 -0
  78. package/dist/core/services/CodeGenerationService/generators/OriginalClassGenerator.js +224 -0
  79. package/dist/core/services/CodeGenerationService/generators/SendableClassGenerator.d.ts +24 -0
  80. package/dist/core/services/CodeGenerationService/generators/SendableClassGenerator.js +307 -0
  81. package/dist/core/services/CodeGenerationService/generators/SerializerGenerator.d.ts +28 -0
  82. package/dist/core/services/CodeGenerationService/generators/SerializerGenerator.js +259 -0
  83. package/dist/core/services/CodeGenerationService/generators/TempSerializerGenerator.d.ts +19 -0
  84. package/dist/core/services/CodeGenerationService/generators/TempSerializerGenerator.js +178 -0
  85. package/dist/core/services/CodeGenerationService/index.d.ts +1 -0
  86. package/dist/core/services/CodeGenerationService/index.js +5 -0
  87. package/dist/core/services/CodeGenerationService/shared/ImportManager.d.ts +27 -0
  88. package/dist/core/services/CodeGenerationService/shared/ImportManager.js +127 -0
  89. package/dist/core/template/HandlebarsTemplateEngine.d.ts +26 -0
  90. package/dist/core/template/HandlebarsTemplateEngine.js +226 -0
  91. package/dist/core/utils/ConfigManager.d.ts +10 -0
  92. package/dist/core/utils/ConfigManager.js +32 -0
  93. package/dist/core/utils/CustomError.d.ts +30 -0
  94. package/dist/core/utils/CustomError.js +37 -0
  95. package/dist/core/utils/DeepCopyUtil.d.ts +15 -0
  96. package/dist/core/utils/DeepCopyUtil.js +138 -0
  97. package/dist/core/utils/GenericTypeSubstitutionUtil.d.ts +9 -0
  98. package/dist/core/utils/GenericTypeSubstitutionUtil.js +68 -0
  99. package/dist/core/utils/SerializationPathUtil.d.ts +18 -0
  100. package/dist/core/utils/SerializationPathUtil.js +107 -0
  101. package/dist/core/utils/TsMorphUtil.d.ts +8 -0
  102. package/dist/core/utils/TsMorphUtil.js +34 -0
  103. package/dist/index.d.ts +4 -0
  104. package/dist/index.js +57 -0
  105. package/dist/json-plugin/JSONExecuteController.d.ts +13 -0
  106. package/dist/json-plugin/JSONExecuteController.js +103 -0
  107. package/dist/json-plugin/constants/TaskConstants.d.ts +7 -0
  108. package/dist/json-plugin/constants/TaskConstants.js +11 -0
  109. package/dist/json-plugin/interfaces/IModuleContext.d.ts +10 -0
  110. package/dist/json-plugin/interfaces/IModuleContext.js +2 -0
  111. package/dist/json-plugin/interfaces/ITargetContext.d.ts +8 -0
  112. package/dist/json-plugin/interfaces/ITargetContext.js +2 -0
  113. package/dist/json-plugin/interfaces/impl/ModuleContext.d.ts +12 -0
  114. package/dist/json-plugin/interfaces/impl/ModuleContext.js +24 -0
  115. package/dist/json-plugin/interfaces/impl/TargetContext.d.ts +23 -0
  116. package/dist/json-plugin/interfaces/impl/TargetContext.js +113 -0
  117. package/dist/json-plugin/tasks/BaseTask.d.ts +14 -0
  118. package/dist/json-plugin/tasks/BaseTask.js +53 -0
  119. package/dist/json-plugin/tasks/CleanTask.d.ts +8 -0
  120. package/dist/json-plugin/tasks/CleanTask.js +25 -0
  121. package/dist/json-plugin/tasks/CodeProcessingTask.d.ts +8 -0
  122. package/dist/json-plugin/tasks/CodeProcessingTask.js +26 -0
  123. package/dist/json-plugin/tasks/SyncTask.d.ts +8 -0
  124. package/dist/json-plugin/tasks/SyncTask.js +21 -0
  125. package/dist/json-plugin/tasks/WatchTask.d.ts +11 -0
  126. package/dist/json-plugin/tasks/WatchTask.js +102 -0
  127. package/package.json +46 -0
  128. package/src/core/Types.ts +356 -0
  129. package/src/core/analyzers/ClassAnalyzer.ts +824 -0
  130. package/src/core/analyzers/CustomTypeAnalyzer.ts +337 -0
  131. package/src/core/analyzers/SendableMergeChecker.ts +195 -0
  132. package/src/core/analyzers/SendableMergeabilityRegistry.ts +72 -0
  133. package/src/core/constants/DecoratorConstants.ts +27 -0
  134. package/src/core/constants/PathConstants.ts +31 -0
  135. package/src/core/constants/StringConstants.ts +152 -0
  136. package/src/core/constants/index.ts +21 -0
  137. package/src/core/handlers/BaseTypeHandler.ts +121 -0
  138. package/src/core/handlers/CustomClassHandler.ts +278 -0
  139. package/src/core/handlers/DateHandler.ts +75 -0
  140. package/src/core/handlers/DecimalHandler.ts +75 -0
  141. package/src/core/handlers/EnumHandler.ts +142 -0
  142. package/src/core/handlers/GenericContainerHandler.ts +621 -0
  143. package/src/core/handlers/GenericHandler.ts +130 -0
  144. package/src/core/handlers/HandlerBootstrap.ts +64 -0
  145. package/src/core/handlers/ITypeHandler.ts +133 -0
  146. package/src/core/handlers/PrimitiveHandler.ts +159 -0
  147. package/src/core/handlers/TupleHandler.ts +145 -0
  148. package/src/core/handlers/TypeHandlerRegistry.ts +236 -0
  149. package/src/core/handlers/UnionTypeHandler.ts +400 -0
  150. package/src/core/handlers/index.ts +18 -0
  151. package/src/core/import-rewrite/services/BuildProfileUpdater.ts +145 -0
  152. package/src/core/import-rewrite/services/ImportRewriteService.ts +129 -0
  153. package/src/core/import-rewrite/services/ImportTransformService.ts +200 -0
  154. package/src/core/import-rewrite/strategies/ImportScanStrategy.ts +303 -0
  155. package/src/core/import-rewrite/types/ImportRewriteTypes.ts +100 -0
  156. package/src/core/index.ts +31 -0
  157. package/src/core/interfaces/ITask.ts +23 -0
  158. package/src/core/interfaces/ITaskContext.ts +94 -0
  159. package/src/core/interfaces/index.ts +17 -0
  160. package/src/core/logger/Logger.ts +149 -0
  161. package/src/core/services/CodeAnalysisService.ts +67 -0
  162. package/src/core/services/CodeGenerationEngine.ts +181 -0
  163. package/src/core/services/CodeGenerationService/CodeGenerationService.ts +159 -0
  164. package/src/core/services/CodeGenerationService/generators/MergedSendableClassGenerator.ts +189 -0
  165. package/src/core/services/CodeGenerationService/generators/OriginalClassGenerator.ts +314 -0
  166. package/src/core/services/CodeGenerationService/generators/SendableClassGenerator.ts +421 -0
  167. package/src/core/services/CodeGenerationService/generators/SerializerGenerator.ts +392 -0
  168. package/src/core/services/CodeGenerationService/generators/TempSerializerGenerator.ts +277 -0
  169. package/src/core/services/CodeGenerationService/index.ts +16 -0
  170. package/src/core/services/CodeGenerationService/shared/ImportManager.ts +191 -0
  171. package/src/core/template/HandlebarsTemplateEngine.ts +282 -0
  172. package/src/core/utils/ConfigManager.ts +49 -0
  173. package/src/core/utils/CustomError.ts +51 -0
  174. package/src/core/utils/DeepCopyUtil.ts +185 -0
  175. package/src/core/utils/GenericTypeSubstitutionUtil.ts +136 -0
  176. package/src/core/utils/SerializationPathUtil.ts +142 -0
  177. package/src/core/utils/TsMorphUtil.ts +50 -0
  178. package/src/index.ts +81 -0
  179. package/src/json-plugin/JSONExecuteController.ts +134 -0
  180. package/src/json-plugin/constants/TaskConstants.ts +22 -0
  181. package/src/json-plugin/interfaces/IModuleContext.ts +27 -0
  182. package/src/json-plugin/interfaces/ITargetContext.ts +25 -0
  183. package/src/json-plugin/interfaces/impl/ModuleContext.ts +45 -0
  184. package/src/json-plugin/interfaces/impl/TargetContext.ts +196 -0
  185. package/src/json-plugin/tasks/BaseTask.ts +94 -0
  186. package/src/json-plugin/tasks/CleanTask.ts +36 -0
  187. package/src/json-plugin/tasks/CodeProcessingTask.ts +41 -0
  188. package/src/json-plugin/tasks/SyncTask.ts +33 -0
  189. package/src/json-plugin/tasks/WatchTask.ts +99 -0
  190. package/template/SerializerPerformanceTemplate.hbs +35 -0
  191. package/template/SerializerRegisterTemplate.hbs +10 -0
  192. package/template/SerializerStrictTemplate.hbs +89 -0
  193. package/template/SerializerTemplate.hbs +176 -0
  194. package/tsconfig.json +17 -0
  195. package/tslint.json +3 -0
@@ -0,0 +1,37 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ErrorCodes = exports.CustomError = void 0;
4
+ class CustomError extends Error {
5
+ constructor(message, code) {
6
+ super(message);
7
+ this.name = 'CustomError';
8
+ this.code = code;
9
+ }
10
+ }
11
+ exports.CustomError = CustomError;
12
+ var ErrorCodes;
13
+ (function (ErrorCodes) {
14
+ ErrorCodes[ErrorCodes["INTERNAL_ERROR"] = 3000] = "INTERNAL_ERROR";
15
+ ErrorCodes[ErrorCodes["TEMPLATE_NOT_FOUND"] = 3011] = "TEMPLATE_NOT_FOUND";
16
+ ErrorCodes[ErrorCodes["TEMPLATE_REGISTER_FAIL"] = 3012] = "TEMPLATE_REGISTER_FAIL";
17
+ ErrorCodes[ErrorCodes["ROOT_DIRECTROY_NOT_FOUND"] = 3021] = "ROOT_DIRECTROY_NOT_FOUND";
18
+ ErrorCodes[ErrorCodes["INVALID_OUTPUT_FAIL"] = 3022] = "INVALID_OUTPUT_FAIL";
19
+ ErrorCodes[ErrorCodes["INVALID_FILE_CONTENT"] = 3023] = "INVALID_FILE_CONTENT";
20
+ ErrorCodes[ErrorCodes["HANDLER_NOT_FOUND"] = 3031] = "HANDLER_NOT_FOUND";
21
+ ErrorCodes[ErrorCodes["CLASS_DECLARATION_NOT_FOUND"] = 3032] = "CLASS_DECLARATION_NOT_FOUND";
22
+ ErrorCodes[ErrorCodes["IS_NOT_ENUM"] = 3033] = "IS_NOT_ENUM";
23
+ ErrorCodes[ErrorCodes["ENUM_STRUCTURE_NOT_FOUND"] = 3034] = "ENUM_STRUCTURE_NOT_FOUND";
24
+ ErrorCodes[ErrorCodes["TYPE_NOT_SUPPORT"] = 3041] = "TYPE_NOT_SUPPORT";
25
+ ErrorCodes[ErrorCodes["LITERAL_NOT_SOPPORT"] = 3042] = "LITERAL_NOT_SOPPORT";
26
+ ErrorCodes[ErrorCodes["TRANSIENT_ATTR_OPTIONAL"] = 3043] = "TRANSIENT_ATTR_OPTIONAL";
27
+ ErrorCodes[ErrorCodes["ANNOTATION_CONFLICT"] = 3044] = "ANNOTATION_CONFLICT";
28
+ ErrorCodes[ErrorCodes["UNEXPECTED_GENERIC"] = 3051] = "UNEXPECTED_GENERIC";
29
+ ErrorCodes[ErrorCodes["GENERIC_NOT_MATCH"] = 3052] = "GENERIC_NOT_MATCH";
30
+ ErrorCodes[ErrorCodes["UNION_STRUCTURE_NOT_FOUND"] = 3061] = "UNION_STRUCTURE_NOT_FOUND";
31
+ ErrorCodes[ErrorCodes["UNION_EXPEDTED_TWO"] = 3062] = "UNION_EXPEDTED_TWO";
32
+ ErrorCodes[ErrorCodes["UNION_INNER_NOT_FOUND"] = 3063] = "UNION_INNER_NOT_FOUND";
33
+ ErrorCodes[ErrorCodes["UNION_UNSUPPORTED_PATTERN"] = 3064] = "UNION_UNSUPPORTED_PATTERN";
34
+ ErrorCodes[ErrorCodes["UNION_EXPEDTED_THREE"] = 3065] = "UNION_EXPEDTED_THREE";
35
+ ErrorCodes[ErrorCodes["REWRITE_IMPORT_FAIL"] = 3071] = "REWRITE_IMPORT_FAIL";
36
+ ErrorCodes[ErrorCodes["NOT_SENDABLE"] = 3072] = "NOT_SENDABLE";
37
+ })(ErrorCodes || (exports.ErrorCodes = ErrorCodes = {}));
@@ -0,0 +1,15 @@
1
+ import { PropertyAnalysis, TypeStructure, TypeDependency } from '..';
2
+ export declare class DeepCopyUtil {
3
+ static copyPropertyAnalysis(original: PropertyAnalysis): PropertyAnalysis;
4
+ private static copyPropertyDecorators;
5
+ static copyTypeStructure(original: TypeStructure): TypeStructure;
6
+ static copyTypeDependencies(original: TypeDependency[]): TypeDependency[];
7
+ private static copyTypeDependency;
8
+ private static copyDependencySource;
9
+ private static copyTypeDetails;
10
+ private static copyEnumDetails;
11
+ private static copyClassDetails;
12
+ private static copyInterfaceDetails;
13
+ private static copyTypeAliasDetails;
14
+ static deepCopy<T>(obj: T): T;
15
+ }
@@ -0,0 +1,138 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.DeepCopyUtil = void 0;
4
+ class DeepCopyUtil {
5
+ static copyPropertyAnalysis(original) {
6
+ return {
7
+ name: original.name,
8
+ defaultValue: original.defaultValue,
9
+ decorators: this.copyPropertyDecorators(original.decorators),
10
+ isMust: original.isMust,
11
+ type: this.copyTypeStructure(original.type)
12
+ };
13
+ }
14
+ static copyPropertyDecorators(original) {
15
+ if (!original) {
16
+ return undefined;
17
+ }
18
+ return {
19
+ serialName: original.serialName,
20
+ isRequired: original.isRequired,
21
+ isTransient: original.isTransient,
22
+ with: original.with
23
+ };
24
+ }
25
+ static copyTypeStructure(original) {
26
+ const copy = {
27
+ kind: original.kind,
28
+ depth: original.depth,
29
+ sourceText: original.sourceText,
30
+ args: [],
31
+ dependencies: this.copyTypeDependencies(original.dependencies)
32
+ };
33
+ if (original.enumDetails) {
34
+ copy.enumDetails = this.copyEnumDetails(original.enumDetails);
35
+ }
36
+ if (original.classDetails) {
37
+ copy.classDetails = this.copyClassDetails(original.classDetails);
38
+ }
39
+ if (original.unionDetails) {
40
+ copy.unionDetails = original.unionDetails.map(member => this.copyTypeStructure(member));
41
+ }
42
+ if (original.args && original.args.length > 0) {
43
+ copy.args = original.args.map(arg => this.copyTypeStructure(arg));
44
+ }
45
+ return copy;
46
+ }
47
+ static copyTypeDependencies(original) {
48
+ return original.map(dependency => this.copyTypeDependency(dependency));
49
+ }
50
+ static copyTypeDependency(original) {
51
+ const copy = {
52
+ typeName: original.typeName,
53
+ typeKind: original.typeKind,
54
+ importPath: original.importPath,
55
+ source: this.copyDependencySource(original.source)
56
+ };
57
+ if (original.details) {
58
+ copy.details = this.copyTypeDetails(original.details);
59
+ }
60
+ return copy;
61
+ }
62
+ static copyDependencySource(original) {
63
+ return {
64
+ type: original.type,
65
+ sourceFilePath: original.sourceFilePath,
66
+ originalImport: original.originalImport,
67
+ packageName: original.packageName
68
+ };
69
+ }
70
+ static copyTypeDetails(original) {
71
+ switch (original.kind) {
72
+ case 'enum':
73
+ return this.copyEnumDetails(original);
74
+ case 'class':
75
+ return this.copyClassDetails(original);
76
+ case 'interface':
77
+ return this.copyInterfaceDetails(original);
78
+ case 'type':
79
+ return this.copyTypeAliasDetails(original);
80
+ default:
81
+ return { ...original };
82
+ }
83
+ }
84
+ static copyEnumDetails(original) {
85
+ return {
86
+ kind: original.kind,
87
+ members: original.members ? original.members.map((member) => ({
88
+ name: member.name,
89
+ value: member.value,
90
+ literalValue: member.literalValue
91
+ })) : [],
92
+ valueType: original.valueType
93
+ };
94
+ }
95
+ static copyClassDetails(original) {
96
+ return {
97
+ kind: original.kind,
98
+ classDecl: original.classDecl,
99
+ isGeneric: original.isGeneric
100
+ };
101
+ }
102
+ static copyInterfaceDetails(original) {
103
+ return {
104
+ kind: original.kind,
105
+ interfaceDecl: original.interfaceDecl,
106
+ isGeneric: original.isGeneric
107
+ };
108
+ }
109
+ static copyTypeAliasDetails(original) {
110
+ return {
111
+ kind: original.kind,
112
+ aliasedType: original.aliasedType,
113
+ resolvedType: this.copyTypeStructure(original.resolvedType)
114
+ };
115
+ }
116
+ static deepCopy(obj) {
117
+ if (obj === null || typeof obj !== 'object') {
118
+ return obj;
119
+ }
120
+ if (obj instanceof Date) {
121
+ return new Date(obj.getTime());
122
+ }
123
+ if (obj instanceof Array) {
124
+ return obj.map(item => this.deepCopy(item));
125
+ }
126
+ if (typeof obj === 'object') {
127
+ const copy = {};
128
+ for (const key in obj) {
129
+ if (obj.hasOwnProperty(key)) {
130
+ copy[key] = this.deepCopy(obj[key]);
131
+ }
132
+ }
133
+ return copy;
134
+ }
135
+ return obj;
136
+ }
137
+ }
138
+ exports.DeepCopyUtil = DeepCopyUtil;
@@ -0,0 +1,9 @@
1
+ import { TypeNode } from 'ts-morph';
2
+ import { PropertyAnalysis } from '..';
3
+ export declare class GenericTypeSubstitutionUtil {
4
+ static buildGenericMapping(parentGenericParams: string[], childTypeArguments: TypeNode[]): Map<string, TypeNode>;
5
+ static substitutePropertyType(property: PropertyAnalysis, genericMapping: Map<string, TypeNode>, currentClassGenericParams?: string[]): PropertyAnalysis;
6
+ private static substituteTypeStructure;
7
+ private static constructTypeStructureFromTypeName;
8
+ private static collectDependenciesFromChildren;
9
+ }
@@ -0,0 +1,68 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.GenericTypeSubstitutionUtil = void 0;
4
+ const __1 = require("..");
5
+ const handlers_1 = require("../handlers");
6
+ const DeepCopyUtil_1 = require("./DeepCopyUtil");
7
+ class GenericTypeSubstitutionUtil {
8
+ static buildGenericMapping(parentGenericParams, childTypeArguments) {
9
+ const mapping = new Map();
10
+ const length = Math.min(parentGenericParams.length, childTypeArguments.length);
11
+ for (let i = 0; i < length; i++) {
12
+ mapping.set(parentGenericParams[i], childTypeArguments[i]);
13
+ }
14
+ return mapping;
15
+ }
16
+ static substitutePropertyType(property, genericMapping, currentClassGenericParams = []) {
17
+ if (genericMapping.size === 0) {
18
+ return property;
19
+ }
20
+ const newProperty = DeepCopyUtil_1.DeepCopyUtil.copyPropertyAnalysis(property);
21
+ newProperty.type = this.substituteTypeStructure(newProperty.type, genericMapping, currentClassGenericParams);
22
+ return newProperty;
23
+ }
24
+ static substituteTypeStructure(typeStructure, mapping, currentClassGenericParams = []) {
25
+ if (typeStructure.kind === __1.PropertyKind.GENERIC) {
26
+ const genericTypeName = typeStructure.sourceText;
27
+ if (mapping.has(genericTypeName)) {
28
+ const substitutedTypeNode = mapping.get(genericTypeName);
29
+ return this.constructTypeStructureFromTypeName(substitutedTypeNode, currentClassGenericParams);
30
+ }
31
+ return typeStructure;
32
+ }
33
+ const newStructure = DeepCopyUtil_1.DeepCopyUtil.copyTypeStructure(typeStructure);
34
+ if (newStructure.unionDetails) {
35
+ newStructure.unionDetails = newStructure.unionDetails.map(member => this.substituteTypeStructure(member, mapping, currentClassGenericParams));
36
+ }
37
+ if (newStructure.args) {
38
+ newStructure.args = newStructure.args.map(arg => this.substituteTypeStructure(arg, mapping, currentClassGenericParams));
39
+ }
40
+ newStructure.dependencies = this.collectDependenciesFromChildren(newStructure);
41
+ return newStructure;
42
+ }
43
+ static constructTypeStructureFromTypeName(typeNode, currentClassGenericParams = []) {
44
+ return handlers_1.TypeHandlerRegistry.getInstance().parseType(typeNode, {
45
+ depth: 0,
46
+ genericParams: currentClassGenericParams
47
+ });
48
+ }
49
+ static collectDependenciesFromChildren(children) {
50
+ const allDependencies = [];
51
+ if (children.unionDetails) {
52
+ for (const member of children.unionDetails) {
53
+ if (member.dependencies) {
54
+ allDependencies.push(...member.dependencies);
55
+ }
56
+ }
57
+ }
58
+ if (children.args) {
59
+ for (const arg of children.args) {
60
+ if (arg.dependencies) {
61
+ allDependencies.push(...arg.dependencies);
62
+ }
63
+ }
64
+ }
65
+ return allDependencies;
66
+ }
67
+ }
68
+ exports.GenericTypeSubstitutionUtil = GenericTypeSubstitutionUtil;
@@ -0,0 +1,18 @@
1
+ import { FileUtil } from '@ohos/hvigor';
2
+ import * as fs from "fs";
3
+ import { ITaskContext } from '..';
4
+ export default class SerializationPathUtil extends FileUtil {
5
+ static readonly sep: string;
6
+ static dirname(p: string): string;
7
+ static basename(p: string, suffix?: string): string;
8
+ static extname(p: string): string;
9
+ static isAbsolute(p: string): boolean;
10
+ static normalize(p: string): string;
11
+ static relative(from: string, to: string): string;
12
+ static join(...paths: string[]): string;
13
+ static rmSync(path: fs.PathLike, options?: fs.RmOptions): void;
14
+ static calculateModularImportPath(absolutePath: string, context: ITaskContext): string;
15
+ static mapToSendableOutputPath(className: string, context: ITaskContext): string;
16
+ static scanEtsFiles(directory: string): string[];
17
+ private static removeFileExtension;
18
+ }
@@ -0,0 +1,107 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
35
+ Object.defineProperty(exports, "__esModule", { value: true });
36
+ const hvigor_1 = require("@ohos/hvigor");
37
+ const path = __importStar(require("path"));
38
+ const fs = __importStar(require("fs"));
39
+ const __1 = require("..");
40
+ class SerializationPathUtil extends hvigor_1.FileUtil {
41
+ static dirname(p) {
42
+ return path.dirname(p);
43
+ }
44
+ static basename(p, suffix) {
45
+ return path.basename(p, suffix);
46
+ }
47
+ static extname(p) {
48
+ return path.extname(p);
49
+ }
50
+ static isAbsolute(p) {
51
+ return path.isAbsolute(p);
52
+ }
53
+ static normalize(p) {
54
+ return path.normalize(p);
55
+ }
56
+ static relative(from, to) {
57
+ return path.relative(from, to);
58
+ }
59
+ static join(...paths) {
60
+ return path.join(...paths);
61
+ }
62
+ static rmSync(path, options) {
63
+ return fs.rmSync(path, options);
64
+ }
65
+ static calculateModularImportPath(absolutePath, context) {
66
+ const packageName = context.getPackageName();
67
+ const moduleRelativePath = context.calculateSourceRootToModuleRoot(absolutePath);
68
+ const pathWithoutExtension = this.removeFileExtension(moduleRelativePath);
69
+ return `${packageName}/${pathWithoutExtension}`;
70
+ }
71
+ static mapToSendableOutputPath(className, context) {
72
+ const baseOutputDir = context.getOutputRoot();
73
+ const sendableModelDir = this.join(baseOutputDir, 'ets/sendableModel');
74
+ return this.join(sendableModelDir, `Sendable${className}.ets`);
75
+ }
76
+ static scanEtsFiles(directory) {
77
+ const etsFiles = [];
78
+ const deepScanEtsFiles = (scanPath, relativePath) => {
79
+ let resolvePath = this.pathResolve(scanPath, relativePath);
80
+ if (!this.exist(resolvePath)) {
81
+ return;
82
+ }
83
+ if (this.isDictionary(resolvePath)) {
84
+ const files = fs.readdirSync(resolvePath);
85
+ files.forEach(file => {
86
+ deepScanEtsFiles(resolvePath, file);
87
+ });
88
+ }
89
+ else {
90
+ if (path.extname(relativePath) === __1.PathConstants.ETS_EXTENSIONS) {
91
+ etsFiles.push(resolvePath);
92
+ }
93
+ }
94
+ };
95
+ deepScanEtsFiles(directory, '');
96
+ return etsFiles;
97
+ }
98
+ static removeFileExtension(filePath) {
99
+ const lastDotIndex = filePath.lastIndexOf('.');
100
+ if (lastDotIndex > 0) {
101
+ return filePath.substring(0, lastDotIndex);
102
+ }
103
+ return filePath;
104
+ }
105
+ }
106
+ SerializationPathUtil.sep = path.sep;
107
+ exports.default = SerializationPathUtil;
@@ -0,0 +1,8 @@
1
+ import { Project, ProjectOptions, TypeChecker } from 'ts-morph';
2
+ export declare class TsMorphUtil {
3
+ static project: Project | null;
4
+ private constructor();
5
+ static getProject(options?: ProjectOptions): Project;
6
+ static getTypeChecker(): TypeChecker;
7
+ static clearProject(): void;
8
+ }
@@ -0,0 +1,34 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.TsMorphUtil = void 0;
4
+ const ts_morph_1 = require("ts-morph");
5
+ const Logger_1 = require("../logger/Logger");
6
+ class TsMorphUtil {
7
+ constructor() { }
8
+ static getProject(options) {
9
+ if (!this.project) {
10
+ Logger_1.Logger.info('创建ts-morph项目');
11
+ this.project = new ts_morph_1.Project(options || {
12
+ compilerOptions: {
13
+ allowNonTsExtensions: true
14
+ }
15
+ });
16
+ }
17
+ return this.project;
18
+ }
19
+ static getTypeChecker() {
20
+ return this.getProject().getTypeChecker();
21
+ }
22
+ static clearProject() {
23
+ const sourceFiles = TsMorphUtil.getProject().getSourceFiles();
24
+ Logger_1.Logger.info(`Clearing ${sourceFiles.length} source files...`);
25
+ if (sourceFiles && sourceFiles.length > 0) {
26
+ sourceFiles.forEach((sourceFile) => {
27
+ TsMorphUtil.getProject().removeSourceFile(sourceFile);
28
+ });
29
+ }
30
+ TsMorphUtil.project = null;
31
+ }
32
+ }
33
+ exports.TsMorphUtil = TsMorphUtil;
34
+ TsMorphUtil.project = null;
@@ -0,0 +1,4 @@
1
+ import { HvigorPlugin } from '@ohos/hvigor';
2
+ import { Hvigor } from '@ohos/hvigor/src/base/external/core/hvigor';
3
+ import { TurboTransJsonPluginOptions } from './core';
4
+ export declare function turboTransJsonPlugin(hvigor: Hvigor, options?: TurboTransJsonPluginOptions): HvigorPlugin;
package/dist/index.js ADDED
@@ -0,0 +1,57 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.turboTransJsonPlugin = turboTransJsonPlugin;
4
+ const core_1 = require("./core");
5
+ const HandlerBootstrap_1 = require("./core/handlers/HandlerBootstrap");
6
+ const JSONExecuteController_1 = require("./json-plugin/JSONExecuteController");
7
+ const PLUGIN_ID = core_1.StringConstants.PLUGIN_ID;
8
+ const DEFAULT_OPTIONS = {
9
+ ignoreModuleNames: [],
10
+ scanDir: core_1.PathConstants.DEFAULT_SCAN_DIRS,
11
+ deserializationMode: core_1.DeserializationMode.PERFORMANCE,
12
+ };
13
+ function turboTransJsonPlugin(hvigor, options = DEFAULT_OPTIONS) {
14
+ return {
15
+ pluginId: PLUGIN_ID,
16
+ apply(node) {
17
+ TurboTransJSONPluginManager.getInstance(hvigor).initialization(node, options);
18
+ }
19
+ };
20
+ }
21
+ class TurboTransJSONPluginManager {
22
+ constructor(hvigor) {
23
+ this.moduleExecuteControllers = new Set();
24
+ this.setupHvigorHooks(hvigor);
25
+ }
26
+ static getInstance(hvigor) {
27
+ if (!TurboTransJSONPluginManager.instance) {
28
+ TurboTransJSONPluginManager.instance = new TurboTransJSONPluginManager(hvigor);
29
+ }
30
+ return TurboTransJSONPluginManager.instance;
31
+ }
32
+ initialization(node, options) {
33
+ (0, HandlerBootstrap_1.initializeTypeHandlers)();
34
+ node.subNodes(subNode => {
35
+ const subNodeName = subNode.getNodeName();
36
+ if (options.ignoreModuleNames.includes(subNodeName)) {
37
+ return;
38
+ }
39
+ const executeController = new JSONExecuteController_1.JSONExecuteController(subNode, options);
40
+ this.moduleExecuteControllers.add(executeController);
41
+ });
42
+ }
43
+ setupHvigorHooks(hvigor) {
44
+ hvigor.nodesEvaluated(() => {
45
+ this.moduleExecuteControllers.forEach(controller => {
46
+ controller.execute();
47
+ });
48
+ });
49
+ hvigor.buildFinished(() => {
50
+ this.moduleExecuteControllers.forEach(controller => {
51
+ controller.compile();
52
+ });
53
+ this.moduleExecuteControllers.clear();
54
+ TurboTransJSONPluginManager.instance = null;
55
+ });
56
+ }
57
+ }
@@ -0,0 +1,13 @@
1
+ import { HvigorNode } from '@ohos/hvigor';
2
+ import { ITask, TurboTransJsonPluginOptions } from '../core';
3
+ export declare class JSONExecuteController {
4
+ private readonly node;
5
+ private readonly options;
6
+ tasks: Set<ITask>;
7
+ constructor(node: HvigorNode, options: TurboTransJsonPluginOptions);
8
+ execute(): void;
9
+ compile(): void;
10
+ private initTasks;
11
+ private taskCleanUp;
12
+ private registerHvigorTasks;
13
+ }
@@ -0,0 +1,103 @@
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.JSONExecuteController = void 0;
7
+ const core_1 = require("../core");
8
+ const TargetContext_1 = require("./interfaces/impl/TargetContext");
9
+ const ModuleContext_1 = require("./interfaces/impl/ModuleContext");
10
+ const CodeProcessingTask_1 = require("./tasks/CodeProcessingTask");
11
+ const CleanTask_1 = require("./tasks/CleanTask");
12
+ const SyncTask_1 = require("./tasks/SyncTask");
13
+ const WatchTask_1 = require("./tasks/WatchTask");
14
+ const SerializationPathUtil_1 = __importDefault(require("../core/utils/SerializationPathUtil"));
15
+ const CustomError_1 = require("../core/utils/CustomError");
16
+ const TaskConstants_1 = require("./constants/TaskConstants");
17
+ const TsMorphUtil_1 = require("../core/utils/TsMorphUtil");
18
+ const CustomTypeAnalyzer_1 = require("../core/analyzers/CustomTypeAnalyzer");
19
+ const SendableMergeabilityRegistry_1 = require("../core/analyzers/SendableMergeabilityRegistry");
20
+ class JSONExecuteController {
21
+ constructor(node, options) {
22
+ this.tasks = new Set();
23
+ this.node = node;
24
+ this.options = options;
25
+ }
26
+ execute() {
27
+ const configManager = core_1.ConfigManager.getInstance();
28
+ configManager.setConfig(this.options);
29
+ const moduleContext = this.node.getContext(this.node.getAllPluginIds()[0]);
30
+ const context = new ModuleContext_1.ModuleContextImpl(this.node, moduleContext);
31
+ context.initScanFiles(this.options.scanDir);
32
+ this.initTasks();
33
+ moduleContext.targets((target) => {
34
+ this.registerHvigorTasks(target, context);
35
+ });
36
+ }
37
+ compile() {
38
+ }
39
+ initTasks() {
40
+ this.tasks.add(new CodeProcessingTask_1.CodeProcessingTask());
41
+ this.tasks.add(new CleanTask_1.CleanTask());
42
+ this.tasks.add(new SyncTask_1.SyncTask());
43
+ this.tasks.add(new WatchTask_1.WatchTask());
44
+ }
45
+ taskCleanUp() {
46
+ const sourceFiles = TsMorphUtil_1.TsMorphUtil.getProject().getSourceFiles();
47
+ core_1.Logger.info(`code analysis Clearing ${sourceFiles.length} source files...`);
48
+ sourceFiles.forEach((sourceFile) => {
49
+ TsMorphUtil_1.TsMorphUtil.getProject().removeSourceFile(sourceFile);
50
+ });
51
+ CustomTypeAnalyzer_1.CustomTypeAnalyzer.getInstance().clearCache();
52
+ SendableMergeabilityRegistry_1.SendableMergeabilityRegistry.clear();
53
+ }
54
+ registerHvigorTasks(target, context) {
55
+ const targetContext = new TargetContext_1.TargetContextImpl(target, context);
56
+ this.tasks.forEach((task) => {
57
+ this.node.registerTask({
58
+ name: task.taskName,
59
+ run: async () => {
60
+ try {
61
+ core_1.Logger.info(`${task.taskName} run`);
62
+ core_1.TempSerializerGenerator.getInstance().init(targetContext);
63
+ const result = task.run(targetContext);
64
+ if (task.taskName !== TaskConstants_1.TaskConstants.CLEAN_TASK) {
65
+ core_1.TempSerializerGenerator.getInstance().saveFile();
66
+ }
67
+ if (result instanceof Promise) {
68
+ await result;
69
+ }
70
+ if (task.taskName === TaskConstants_1.TaskConstants.SYNC_TASK || task.taskName === TaskConstants_1.TaskConstants.CODE_PROCESSING_TASK) {
71
+ this.taskCleanUp();
72
+ }
73
+ core_1.Logger.info(`${task.taskName} end`);
74
+ }
75
+ catch (error) {
76
+ if (error instanceof CustomError_1.CustomError) {
77
+ core_1.Logger.error(`Error: ${error.constructor.name}: ${error.code} ${error.message}\n${error.stack}`);
78
+ }
79
+ else {
80
+ const err = error;
81
+ core_1.Logger.error(`Error: ${err.constructor.name}: ${CustomError_1.ErrorCodes.INTERNAL_ERROR} ${err.message}\n${err.stack}`);
82
+ }
83
+ this.taskCleanUp();
84
+ throw new Error('serialization execute failed');
85
+ }
86
+ },
87
+ dependencies: task.dependencies.map((dep) => `${target.getTargetName()}@${dep}`),
88
+ postDependencies: task.postDependencies.map((dep) => `${target.getTargetName()}@${dep}`)
89
+ });
90
+ });
91
+ const cleanTask = this.node.getTaskByName('clean');
92
+ if (cleanTask) {
93
+ cleanTask.beforeRun(() => {
94
+ const generatedRootDir = targetContext.getOutputRoot();
95
+ if (SerializationPathUtil_1.default.exist(generatedRootDir)) {
96
+ core_1.Logger.info(`Clean up the generated directory: ${generatedRootDir}`);
97
+ SerializationPathUtil_1.default.rmSync(generatedRootDir, { recursive: true, force: true });
98
+ }
99
+ });
100
+ }
101
+ }
102
+ }
103
+ exports.JSONExecuteController = JSONExecuteController;
@@ -0,0 +1,7 @@
1
+ export declare class TaskConstants {
2
+ static readonly SYNC_TASK = "jsonSync";
3
+ static readonly CLEAN_TASK = "jsonClean";
4
+ static readonly WATCH_TASK = "jsonWatch";
5
+ static readonly CODE_PROCESSING_TASK = "SerializationTask";
6
+ static readonly PRE_BUILD_TASK = "PreBuild";
7
+ }
@@ -0,0 +1,11 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.TaskConstants = void 0;
4
+ class TaskConstants {
5
+ }
6
+ exports.TaskConstants = TaskConstants;
7
+ TaskConstants.SYNC_TASK = 'jsonSync';
8
+ TaskConstants.CLEAN_TASK = 'jsonClean';
9
+ TaskConstants.WATCH_TASK = 'jsonWatch';
10
+ TaskConstants.CODE_PROCESSING_TASK = 'SerializationTask';
11
+ TaskConstants.PRE_BUILD_TASK = 'PreBuild';
@@ -0,0 +1,10 @@
1
+ import { HvigorNode } from '@ohos/hvigor';
2
+ import { ModuleBuildProfile } from '@ohos/hvigor-ohos-plugin/src/options/build/module-build-profile';
3
+ import { OhosModuleContext } from '@ohos/hvigor-ohos-plugin/src/plugin/context/plugin-context';
4
+ export interface IModuleContext {
5
+ readonly node: HvigorNode;
6
+ readonly moduleContext: OhosModuleContext;
7
+ readonly buildProfileOpt: ModuleBuildProfile.ModuleBuildOpt;
8
+ readonly scanFiles: string[];
9
+ initScanFiles(scanDir: string[]): void;
10
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,8 @@
1
+ import { Target } from '@ohos/hvigor-ohos-plugin';
2
+ import { IModuleContext } from './IModuleContext';
3
+ import { ITaskContext } from '../../core';
4
+ export interface ITargetContext extends ITaskContext {
5
+ readonly target: Target;
6
+ readonly context: IModuleContext;
7
+ getCurrentTargetScanFiles(): string[];
8
+ }