@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.
- package/LICENSE +177 -0
- package/README.md +253 -0
- package/dist/core/Types.d.ts +196 -0
- package/dist/core/Types.js +47 -0
- package/dist/core/analyzers/ClassAnalyzer.d.ts +23 -0
- package/dist/core/analyzers/ClassAnalyzer.js +480 -0
- package/dist/core/analyzers/CustomTypeAnalyzer.d.ts +21 -0
- package/dist/core/analyzers/CustomTypeAnalyzer.js +243 -0
- package/dist/core/analyzers/SendableMergeChecker.d.ts +9 -0
- package/dist/core/analyzers/SendableMergeChecker.js +100 -0
- package/dist/core/analyzers/SendableMergeabilityRegistry.d.ts +7 -0
- package/dist/core/analyzers/SendableMergeabilityRegistry.js +23 -0
- package/dist/core/constants/DecoratorConstants.d.ts +7 -0
- package/dist/core/constants/DecoratorConstants.js +13 -0
- package/dist/core/constants/PathConstants.d.ts +6 -0
- package/dist/core/constants/PathConstants.js +10 -0
- package/dist/core/constants/StringConstants.d.ts +83 -0
- package/dist/core/constants/StringConstants.js +87 -0
- package/dist/core/constants/index.d.ts +3 -0
- package/dist/core/constants/index.js +19 -0
- package/dist/core/handlers/BaseTypeHandler.d.ts +23 -0
- package/dist/core/handlers/BaseTypeHandler.js +57 -0
- package/dist/core/handlers/CustomClassHandler.d.ts +21 -0
- package/dist/core/handlers/CustomClassHandler.js +191 -0
- package/dist/core/handlers/DateHandler.d.ts +2 -0
- package/dist/core/handlers/DateHandler.js +60 -0
- package/dist/core/handlers/DecimalHandler.d.ts +2 -0
- package/dist/core/handlers/DecimalHandler.js +60 -0
- package/dist/core/handlers/EnumHandler.d.ts +2 -0
- package/dist/core/handlers/EnumHandler.js +89 -0
- package/dist/core/handlers/GenericContainerHandler.d.ts +2 -0
- package/dist/core/handlers/GenericContainerHandler.js +440 -0
- package/dist/core/handlers/GenericHandler.d.ts +18 -0
- package/dist/core/handlers/GenericHandler.js +92 -0
- package/dist/core/handlers/HandlerBootstrap.d.ts +2 -0
- package/dist/core/handlers/HandlerBootstrap.js +28 -0
- package/dist/core/handlers/ITypeHandler.d.ts +23 -0
- package/dist/core/handlers/ITypeHandler.js +8 -0
- package/dist/core/handlers/PrimitiveHandler.d.ts +2 -0
- package/dist/core/handlers/PrimitiveHandler.js +127 -0
- package/dist/core/handlers/TupleHandler.d.ts +2 -0
- package/dist/core/handlers/TupleHandler.js +98 -0
- package/dist/core/handlers/TypeHandlerRegistry.d.ts +20 -0
- package/dist/core/handlers/TypeHandlerRegistry.js +113 -0
- package/dist/core/handlers/UnionTypeHandler.d.ts +2 -0
- package/dist/core/handlers/UnionTypeHandler.js +263 -0
- package/dist/core/handlers/index.d.ts +2 -0
- package/dist/core/handlers/index.js +5 -0
- package/dist/core/import-rewrite/services/BuildProfileUpdater.d.ts +8 -0
- package/dist/core/import-rewrite/services/BuildProfileUpdater.js +92 -0
- package/dist/core/import-rewrite/services/ImportRewriteService.d.ts +9 -0
- package/dist/core/import-rewrite/services/ImportRewriteService.js +61 -0
- package/dist/core/import-rewrite/services/ImportTransformService.d.ts +15 -0
- package/dist/core/import-rewrite/services/ImportTransformService.js +109 -0
- package/dist/core/import-rewrite/strategies/ImportScanStrategy.d.ts +17 -0
- package/dist/core/import-rewrite/strategies/ImportScanStrategy.js +137 -0
- package/dist/core/import-rewrite/types/ImportRewriteTypes.d.ts +17 -0
- package/dist/core/import-rewrite/types/ImportRewriteTypes.js +2 -0
- package/dist/core/index.d.ts +9 -0
- package/dist/core/index.js +27 -0
- package/dist/core/interfaces/ITask.d.ts +7 -0
- package/dist/core/interfaces/ITask.js +2 -0
- package/dist/core/interfaces/ITaskContext.d.ts +11 -0
- package/dist/core/interfaces/ITaskContext.js +2 -0
- package/dist/core/interfaces/index.d.ts +2 -0
- package/dist/core/interfaces/index.js +2 -0
- package/dist/core/logger/Logger.d.ts +13 -0
- package/dist/core/logger/Logger.js +78 -0
- package/dist/core/services/CodeAnalysisService.d.ts +7 -0
- package/dist/core/services/CodeAnalysisService.js +43 -0
- package/dist/core/services/CodeGenerationEngine.d.ts +12 -0
- package/dist/core/services/CodeGenerationEngine.js +102 -0
- package/dist/core/services/CodeGenerationService/CodeGenerationService.d.ts +13 -0
- package/dist/core/services/CodeGenerationService/CodeGenerationService.js +110 -0
- package/dist/core/services/CodeGenerationService/generators/MergedSendableClassGenerator.d.ts +13 -0
- package/dist/core/services/CodeGenerationService/generators/MergedSendableClassGenerator.js +119 -0
- package/dist/core/services/CodeGenerationService/generators/OriginalClassGenerator.d.ts +21 -0
- package/dist/core/services/CodeGenerationService/generators/OriginalClassGenerator.js +224 -0
- package/dist/core/services/CodeGenerationService/generators/SendableClassGenerator.d.ts +24 -0
- package/dist/core/services/CodeGenerationService/generators/SendableClassGenerator.js +307 -0
- package/dist/core/services/CodeGenerationService/generators/SerializerGenerator.d.ts +28 -0
- package/dist/core/services/CodeGenerationService/generators/SerializerGenerator.js +259 -0
- package/dist/core/services/CodeGenerationService/generators/TempSerializerGenerator.d.ts +19 -0
- package/dist/core/services/CodeGenerationService/generators/TempSerializerGenerator.js +178 -0
- package/dist/core/services/CodeGenerationService/index.d.ts +1 -0
- package/dist/core/services/CodeGenerationService/index.js +5 -0
- package/dist/core/services/CodeGenerationService/shared/ImportManager.d.ts +27 -0
- package/dist/core/services/CodeGenerationService/shared/ImportManager.js +127 -0
- package/dist/core/template/HandlebarsTemplateEngine.d.ts +26 -0
- package/dist/core/template/HandlebarsTemplateEngine.js +226 -0
- package/dist/core/utils/ConfigManager.d.ts +10 -0
- package/dist/core/utils/ConfigManager.js +32 -0
- package/dist/core/utils/CustomError.d.ts +30 -0
- package/dist/core/utils/CustomError.js +37 -0
- package/dist/core/utils/DeepCopyUtil.d.ts +15 -0
- package/dist/core/utils/DeepCopyUtil.js +138 -0
- package/dist/core/utils/GenericTypeSubstitutionUtil.d.ts +9 -0
- package/dist/core/utils/GenericTypeSubstitutionUtil.js +68 -0
- package/dist/core/utils/SerializationPathUtil.d.ts +18 -0
- package/dist/core/utils/SerializationPathUtil.js +107 -0
- package/dist/core/utils/TsMorphUtil.d.ts +8 -0
- package/dist/core/utils/TsMorphUtil.js +34 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +57 -0
- package/dist/json-plugin/JSONExecuteController.d.ts +13 -0
- package/dist/json-plugin/JSONExecuteController.js +103 -0
- package/dist/json-plugin/constants/TaskConstants.d.ts +7 -0
- package/dist/json-plugin/constants/TaskConstants.js +11 -0
- package/dist/json-plugin/interfaces/IModuleContext.d.ts +10 -0
- package/dist/json-plugin/interfaces/IModuleContext.js +2 -0
- package/dist/json-plugin/interfaces/ITargetContext.d.ts +8 -0
- package/dist/json-plugin/interfaces/ITargetContext.js +2 -0
- package/dist/json-plugin/interfaces/impl/ModuleContext.d.ts +12 -0
- package/dist/json-plugin/interfaces/impl/ModuleContext.js +24 -0
- package/dist/json-plugin/interfaces/impl/TargetContext.d.ts +23 -0
- package/dist/json-plugin/interfaces/impl/TargetContext.js +113 -0
- package/dist/json-plugin/tasks/BaseTask.d.ts +14 -0
- package/dist/json-plugin/tasks/BaseTask.js +53 -0
- package/dist/json-plugin/tasks/CleanTask.d.ts +8 -0
- package/dist/json-plugin/tasks/CleanTask.js +25 -0
- package/dist/json-plugin/tasks/CodeProcessingTask.d.ts +8 -0
- package/dist/json-plugin/tasks/CodeProcessingTask.js +26 -0
- package/dist/json-plugin/tasks/SyncTask.d.ts +8 -0
- package/dist/json-plugin/tasks/SyncTask.js +21 -0
- package/dist/json-plugin/tasks/WatchTask.d.ts +11 -0
- package/dist/json-plugin/tasks/WatchTask.js +102 -0
- package/package.json +46 -0
- package/src/core/Types.ts +356 -0
- package/src/core/analyzers/ClassAnalyzer.ts +824 -0
- package/src/core/analyzers/CustomTypeAnalyzer.ts +337 -0
- package/src/core/analyzers/SendableMergeChecker.ts +195 -0
- package/src/core/analyzers/SendableMergeabilityRegistry.ts +72 -0
- package/src/core/constants/DecoratorConstants.ts +27 -0
- package/src/core/constants/PathConstants.ts +31 -0
- package/src/core/constants/StringConstants.ts +152 -0
- package/src/core/constants/index.ts +21 -0
- package/src/core/handlers/BaseTypeHandler.ts +121 -0
- package/src/core/handlers/CustomClassHandler.ts +278 -0
- package/src/core/handlers/DateHandler.ts +75 -0
- package/src/core/handlers/DecimalHandler.ts +75 -0
- package/src/core/handlers/EnumHandler.ts +142 -0
- package/src/core/handlers/GenericContainerHandler.ts +621 -0
- package/src/core/handlers/GenericHandler.ts +130 -0
- package/src/core/handlers/HandlerBootstrap.ts +64 -0
- package/src/core/handlers/ITypeHandler.ts +133 -0
- package/src/core/handlers/PrimitiveHandler.ts +159 -0
- package/src/core/handlers/TupleHandler.ts +145 -0
- package/src/core/handlers/TypeHandlerRegistry.ts +236 -0
- package/src/core/handlers/UnionTypeHandler.ts +400 -0
- package/src/core/handlers/index.ts +18 -0
- package/src/core/import-rewrite/services/BuildProfileUpdater.ts +145 -0
- package/src/core/import-rewrite/services/ImportRewriteService.ts +129 -0
- package/src/core/import-rewrite/services/ImportTransformService.ts +200 -0
- package/src/core/import-rewrite/strategies/ImportScanStrategy.ts +303 -0
- package/src/core/import-rewrite/types/ImportRewriteTypes.ts +100 -0
- package/src/core/index.ts +31 -0
- package/src/core/interfaces/ITask.ts +23 -0
- package/src/core/interfaces/ITaskContext.ts +94 -0
- package/src/core/interfaces/index.ts +17 -0
- package/src/core/logger/Logger.ts +149 -0
- package/src/core/services/CodeAnalysisService.ts +67 -0
- package/src/core/services/CodeGenerationEngine.ts +181 -0
- package/src/core/services/CodeGenerationService/CodeGenerationService.ts +159 -0
- package/src/core/services/CodeGenerationService/generators/MergedSendableClassGenerator.ts +189 -0
- package/src/core/services/CodeGenerationService/generators/OriginalClassGenerator.ts +314 -0
- package/src/core/services/CodeGenerationService/generators/SendableClassGenerator.ts +421 -0
- package/src/core/services/CodeGenerationService/generators/SerializerGenerator.ts +392 -0
- package/src/core/services/CodeGenerationService/generators/TempSerializerGenerator.ts +277 -0
- package/src/core/services/CodeGenerationService/index.ts +16 -0
- package/src/core/services/CodeGenerationService/shared/ImportManager.ts +191 -0
- package/src/core/template/HandlebarsTemplateEngine.ts +282 -0
- package/src/core/utils/ConfigManager.ts +49 -0
- package/src/core/utils/CustomError.ts +51 -0
- package/src/core/utils/DeepCopyUtil.ts +185 -0
- package/src/core/utils/GenericTypeSubstitutionUtil.ts +136 -0
- package/src/core/utils/SerializationPathUtil.ts +142 -0
- package/src/core/utils/TsMorphUtil.ts +50 -0
- package/src/index.ts +81 -0
- package/src/json-plugin/JSONExecuteController.ts +134 -0
- package/src/json-plugin/constants/TaskConstants.ts +22 -0
- package/src/json-plugin/interfaces/IModuleContext.ts +27 -0
- package/src/json-plugin/interfaces/ITargetContext.ts +25 -0
- package/src/json-plugin/interfaces/impl/ModuleContext.ts +45 -0
- package/src/json-plugin/interfaces/impl/TargetContext.ts +196 -0
- package/src/json-plugin/tasks/BaseTask.ts +94 -0
- package/src/json-plugin/tasks/CleanTask.ts +36 -0
- package/src/json-plugin/tasks/CodeProcessingTask.ts +41 -0
- package/src/json-plugin/tasks/SyncTask.ts +33 -0
- package/src/json-plugin/tasks/WatchTask.ts +99 -0
- package/template/SerializerPerformanceTemplate.hbs +35 -0
- package/template/SerializerRegisterTemplate.hbs +10 -0
- package/template/SerializerStrictTemplate.hbs +89 -0
- package/template/SerializerTemplate.hbs +176 -0
- package/tsconfig.json +17 -0
- package/tslint.json +3 -0
|
@@ -0,0 +1,92 @@
|
|
|
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.BuildProfileUpdaterImpl = void 0;
|
|
7
|
+
const __1 = require("../..");
|
|
8
|
+
const SerializationPathUtil_1 = __importDefault(require("../../utils/SerializationPathUtil"));
|
|
9
|
+
class BuildProfileUpdaterImpl {
|
|
10
|
+
updateSourceRoots(targetContext) {
|
|
11
|
+
try {
|
|
12
|
+
const moduleRoot = this.getModuleRootFromContext(targetContext);
|
|
13
|
+
const buildProfilePath = SerializationPathUtil_1.default.join(moduleRoot, 'build-profile.json5');
|
|
14
|
+
__1.Logger.debug(`准备更新 build-profile.json5: ${buildProfilePath}`);
|
|
15
|
+
if (!SerializationPathUtil_1.default.exist(buildProfilePath)) {
|
|
16
|
+
__1.Logger.warn(`build-profile.json5 文件不存在: ${buildProfilePath}`);
|
|
17
|
+
return;
|
|
18
|
+
}
|
|
19
|
+
const config = targetContext.context.buildProfileOpt;
|
|
20
|
+
__1.Logger.debug(`当前配置: ${JSON.stringify(config, null, 2)}`);
|
|
21
|
+
const updated = this.updateSourceRootsConfig(config);
|
|
22
|
+
if (updated) {
|
|
23
|
+
this.writeBuildProfile(targetContext, buildProfilePath, config);
|
|
24
|
+
__1.Logger.info(`已更新 build-profile.json5 的 sourceRoots 配置: ${buildProfilePath}`);
|
|
25
|
+
}
|
|
26
|
+
else {
|
|
27
|
+
__1.Logger.debug('sourceRoots 配置无需更新');
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
catch (error) {
|
|
31
|
+
__1.Logger.error(`更新 build-profile.json5 失败: ${error}`);
|
|
32
|
+
throw error;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
getModuleRootFromContext(context) {
|
|
36
|
+
const outputRoot = context.getOutputRoot();
|
|
37
|
+
let currentDir = SerializationPathUtil_1.default.dirname(outputRoot);
|
|
38
|
+
while (currentDir && currentDir !== SerializationPathUtil_1.default.dirname(currentDir)) {
|
|
39
|
+
const buildProfilePath = SerializationPathUtil_1.default.join(currentDir, 'build-profile.json5');
|
|
40
|
+
if (SerializationPathUtil_1.default.exist(buildProfilePath)) {
|
|
41
|
+
return currentDir;
|
|
42
|
+
}
|
|
43
|
+
currentDir = SerializationPathUtil_1.default.dirname(currentDir);
|
|
44
|
+
}
|
|
45
|
+
return SerializationPathUtil_1.default.dirname(SerializationPathUtil_1.default.dirname(outputRoot));
|
|
46
|
+
}
|
|
47
|
+
readBuildProfile(buildProfilePath) {
|
|
48
|
+
try {
|
|
49
|
+
return SerializationPathUtil_1.default.readJson5(buildProfilePath);
|
|
50
|
+
}
|
|
51
|
+
catch (error) {
|
|
52
|
+
__1.Logger.error(`读取 build-profile.json5 失败: ${buildProfilePath}, 错误: ${error}`);
|
|
53
|
+
throw new Error(`无法读取配置文件: ${buildProfilePath}`);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
writeBuildProfile(targetContext, buildProfilePath, config) {
|
|
57
|
+
try {
|
|
58
|
+
targetContext.context.moduleContext.setBuildProfileOpt(config);
|
|
59
|
+
}
|
|
60
|
+
catch (error) {
|
|
61
|
+
__1.Logger.error(`写入 build-profile.json5 失败: ${buildProfilePath}, 错误: ${error}`);
|
|
62
|
+
throw new Error(`无法写入配置文件: ${buildProfilePath}`);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
updateSourceRootsConfig(config) {
|
|
66
|
+
let updated = false;
|
|
67
|
+
const generatedSourceRoot = "./src/generated";
|
|
68
|
+
__1.Logger.info(`target.source.sourceRoots start`);
|
|
69
|
+
if (config.targets && Array.isArray(config.targets)) {
|
|
70
|
+
for (const target of config.targets) {
|
|
71
|
+
__1.Logger.info(`target.source.sourceRoots is target: ${target}`);
|
|
72
|
+
if (!target.source) {
|
|
73
|
+
target.source = {};
|
|
74
|
+
}
|
|
75
|
+
if (!target.source.sourceRoots) {
|
|
76
|
+
target.source.sourceRoots = [];
|
|
77
|
+
}
|
|
78
|
+
if (!Array.isArray(target.source.sourceRoots)) {
|
|
79
|
+
__1.Logger.warn('target.source.sourceRoots 不是数组,将重置为数组');
|
|
80
|
+
target.source.sourceRoots = [];
|
|
81
|
+
}
|
|
82
|
+
if (!target.source.sourceRoots.includes(generatedSourceRoot)) {
|
|
83
|
+
target.source.sourceRoots.unshift(generatedSourceRoot);
|
|
84
|
+
__1.Logger.info(`添加 target.source.sourceRoots: ${generatedSourceRoot}`);
|
|
85
|
+
updated = true;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
return updated;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
exports.BuildProfileUpdaterImpl = BuildProfileUpdaterImpl;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { ITaskContext, ImportRewriteOptions } from '../..';
|
|
2
|
+
import { GeneratedFileInfo, ImportRewriteService as IImportRewriteService } from '../types/ImportRewriteTypes';
|
|
3
|
+
export declare class ImportRewriteServiceImpl implements IImportRewriteService {
|
|
4
|
+
private readonly transformService;
|
|
5
|
+
constructor();
|
|
6
|
+
rewriteImports(context: ITaskContext, generatedFileInfos: GeneratedFileInfo[], options: ImportRewriteOptions): void;
|
|
7
|
+
private scanFilesWithModelImports;
|
|
8
|
+
private shouldCheckFile;
|
|
9
|
+
}
|
|
@@ -0,0 +1,61 @@
|
|
|
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.ImportRewriteServiceImpl = void 0;
|
|
7
|
+
const __1 = require("../..");
|
|
8
|
+
const ImportTransformService_1 = require("./ImportTransformService");
|
|
9
|
+
const SerializationPathUtil_1 = __importDefault(require("../../utils/SerializationPathUtil"));
|
|
10
|
+
class ImportRewriteServiceImpl {
|
|
11
|
+
constructor() {
|
|
12
|
+
this.transformService = new ImportTransformService_1.ImportTransformServiceImpl();
|
|
13
|
+
}
|
|
14
|
+
rewriteImports(context, generatedFileInfos, options) {
|
|
15
|
+
const filesToUpdate = this.scanFilesWithModelImports(options.scanPaths, generatedFileInfos, context);
|
|
16
|
+
if (filesToUpdate.length === 0) {
|
|
17
|
+
__1.Logger.info('未找到需要重写导入的文件');
|
|
18
|
+
return;
|
|
19
|
+
}
|
|
20
|
+
__1.Logger.info(`找到 ${filesToUpdate.length} 个文件需要重写导入`);
|
|
21
|
+
let successCount = 0;
|
|
22
|
+
let errorCount = 0;
|
|
23
|
+
for (const filePath of filesToUpdate) {
|
|
24
|
+
try {
|
|
25
|
+
this.transformService.rewriteFileImports(filePath, context, generatedFileInfos, options.preserveOriginalImports);
|
|
26
|
+
successCount++;
|
|
27
|
+
}
|
|
28
|
+
catch (error) {
|
|
29
|
+
errorCount++;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
__1.Logger.info(`导入重写完成: 成功 ${successCount} 个,失败 ${errorCount} 个`);
|
|
33
|
+
}
|
|
34
|
+
scanFilesWithModelImports(scanPaths, generatedFileInfos, context) {
|
|
35
|
+
const filesToUpdate = [];
|
|
36
|
+
for (const scanPath of scanPaths) {
|
|
37
|
+
const absoluteScanPath = SerializationPathUtil_1.default.isAbsolute(scanPath)
|
|
38
|
+
? scanPath
|
|
39
|
+
: SerializationPathUtil_1.default.pathResolve(context.getModulePath(), scanPath);
|
|
40
|
+
if (!SerializationPathUtil_1.default.exist(absoluteScanPath)) {
|
|
41
|
+
__1.Logger.warn(`扫描路径不存在: ${absoluteScanPath}`);
|
|
42
|
+
continue;
|
|
43
|
+
}
|
|
44
|
+
const files = SerializationPathUtil_1.default.scanEtsFiles(absoluteScanPath);
|
|
45
|
+
for (const filePath of files) {
|
|
46
|
+
if (this.shouldCheckFile(filePath, generatedFileInfos)) {
|
|
47
|
+
filesToUpdate.push(filePath);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
const IndexFilePath = SerializationPathUtil_1.default.pathResolve(context.getModulePath(), 'Index.ets');
|
|
52
|
+
if (SerializationPathUtil_1.default.exist(IndexFilePath)) {
|
|
53
|
+
filesToUpdate.push(IndexFilePath);
|
|
54
|
+
}
|
|
55
|
+
return filesToUpdate;
|
|
56
|
+
}
|
|
57
|
+
shouldCheckFile(filePath, generatedFileInfos) {
|
|
58
|
+
return !generatedFileInfos.some(info => filePath.startsWith(SerializationPathUtil_1.default.dirname(info.generatedFilePath)));
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
exports.ImportRewriteServiceImpl = ImportRewriteServiceImpl;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { ITaskContext } from '../..';
|
|
2
|
+
import { GeneratedFileInfo, ImportTransformService } from '../types/ImportRewriteTypes';
|
|
3
|
+
export declare class ImportTransformServiceImpl implements ImportTransformService {
|
|
4
|
+
private readonly scanStrategy;
|
|
5
|
+
constructor();
|
|
6
|
+
rewriteFileImports(filePath: string, context: ITaskContext, generatedFileInfos: GeneratedFileInfo[], preserveOriginalImports?: boolean): void;
|
|
7
|
+
private processImportDeclarations;
|
|
8
|
+
private processExportDeclarations;
|
|
9
|
+
private rewriteSingleExport;
|
|
10
|
+
private rewriteSingleImport;
|
|
11
|
+
private replaceWithCommentedOriginal;
|
|
12
|
+
private handleRewriteResult;
|
|
13
|
+
private handleRewriteError;
|
|
14
|
+
private escapeRegExp;
|
|
15
|
+
}
|
|
@@ -0,0 +1,109 @@
|
|
|
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.ImportTransformServiceImpl = void 0;
|
|
7
|
+
const __1 = require("../..");
|
|
8
|
+
const ImportScanStrategy_1 = require("../strategies/ImportScanStrategy");
|
|
9
|
+
const SerializationPathUtil_1 = __importDefault(require("../../utils/SerializationPathUtil"));
|
|
10
|
+
const CustomError_1 = require("../../utils/CustomError");
|
|
11
|
+
const TsMorphUtil_1 = require("../../utils/TsMorphUtil");
|
|
12
|
+
class ImportTransformServiceImpl {
|
|
13
|
+
constructor() {
|
|
14
|
+
this.scanStrategy = new ImportScanStrategy_1.ImportScanStrategyImpl();
|
|
15
|
+
}
|
|
16
|
+
rewriteFileImports(filePath, context, generatedFileInfos, preserveOriginalImports = false) {
|
|
17
|
+
try {
|
|
18
|
+
const sourceFile = TsMorphUtil_1.TsMorphUtil.getProject().addSourceFileAtPath(filePath);
|
|
19
|
+
const importRewriteCount = this.processImportDeclarations(sourceFile, context, generatedFileInfos, preserveOriginalImports);
|
|
20
|
+
const exportRewriteCount = this.processExportDeclarations(sourceFile, context, generatedFileInfos, preserveOriginalImports);
|
|
21
|
+
const totalRewriteCount = importRewriteCount + exportRewriteCount;
|
|
22
|
+
this.handleRewriteResult(sourceFile, filePath, totalRewriteCount);
|
|
23
|
+
}
|
|
24
|
+
catch (error) {
|
|
25
|
+
this.handleRewriteError(error, filePath);
|
|
26
|
+
throw error;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
processImportDeclarations(sourceFile, context, generatedFileInfos, preserveOriginalImports) {
|
|
30
|
+
const importDeclarations = sourceFile.getImportDeclarations();
|
|
31
|
+
let rewriteCount = 0;
|
|
32
|
+
for (const importDecl of importDeclarations) {
|
|
33
|
+
const matchedInfo = this.scanStrategy.shouldRewriteImport(importDecl, sourceFile.getFilePath(), generatedFileInfos, context);
|
|
34
|
+
if (matchedInfo) {
|
|
35
|
+
this.rewriteSingleImport(importDecl, context, matchedInfo, preserveOriginalImports);
|
|
36
|
+
rewriteCount++;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
return rewriteCount;
|
|
40
|
+
}
|
|
41
|
+
processExportDeclarations(sourceFile, context, generatedFileInfos, preserveOriginalImports) {
|
|
42
|
+
const exportDeclarations = sourceFile.getExportDeclarations();
|
|
43
|
+
let rewriteCount = 0;
|
|
44
|
+
for (const exportDecl of exportDeclarations) {
|
|
45
|
+
const moduleSpecifier = exportDecl.getModuleSpecifierValue();
|
|
46
|
+
if (!moduleSpecifier) {
|
|
47
|
+
continue;
|
|
48
|
+
}
|
|
49
|
+
const matchedInfo = this.scanStrategy.shouldRewriteExport(moduleSpecifier, sourceFile.getFilePath(), generatedFileInfos, context);
|
|
50
|
+
if (matchedInfo) {
|
|
51
|
+
this.rewriteSingleExport(exportDecl, context, matchedInfo, preserveOriginalImports);
|
|
52
|
+
rewriteCount++;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
return rewriteCount;
|
|
56
|
+
}
|
|
57
|
+
rewriteSingleExport(exportDecl, context, matchedInfo, preserveOriginalImports) {
|
|
58
|
+
const originalModuleSpecifier = exportDecl.getModuleSpecifierValue();
|
|
59
|
+
if (!originalModuleSpecifier) {
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
const newModuleSpecifier = SerializationPathUtil_1.default.calculateModularImportPath(matchedInfo.generatedFilePath, context);
|
|
63
|
+
if (preserveOriginalImports) {
|
|
64
|
+
this.replaceWithCommentedOriginal(exportDecl, originalModuleSpecifier, newModuleSpecifier);
|
|
65
|
+
}
|
|
66
|
+
else {
|
|
67
|
+
exportDecl.setModuleSpecifier(newModuleSpecifier);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
rewriteSingleImport(importDecl, context, matchedInfo, preserveOriginalImports) {
|
|
71
|
+
const originalModuleSpecifier = importDecl.getModuleSpecifierValue();
|
|
72
|
+
const newModuleSpecifier = SerializationPathUtil_1.default.calculateModularImportPath(matchedInfo.generatedFilePath, context);
|
|
73
|
+
if (preserveOriginalImports) {
|
|
74
|
+
this.replaceWithCommentedOriginal(importDecl, originalModuleSpecifier, newModuleSpecifier);
|
|
75
|
+
}
|
|
76
|
+
else {
|
|
77
|
+
importDecl.setModuleSpecifier(newModuleSpecifier);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
replaceWithCommentedOriginal(importDecl, originalModuleSpecifier, newModuleSpecifier) {
|
|
81
|
+
const originalImportText = importDecl.getFullText().trim();
|
|
82
|
+
const commentedOriginal = `// ${originalImportText}`;
|
|
83
|
+
const newImportText = importDecl
|
|
84
|
+
.getFullText()
|
|
85
|
+
.replace(new RegExp(`(['"]?)${this.escapeRegExp(originalModuleSpecifier)}\\1`, 'g'), `'${newModuleSpecifier}'`);
|
|
86
|
+
importDecl.replaceWithText(`${commentedOriginal}\n${newImportText.trim()}`);
|
|
87
|
+
}
|
|
88
|
+
handleRewriteResult(sourceFile, filePath, rewriteCount) {
|
|
89
|
+
if (rewriteCount > 0) {
|
|
90
|
+
sourceFile.saveSync();
|
|
91
|
+
__1.Logger.info(`文件 ${filePath} 重写了 ${rewriteCount} 个导入语句`);
|
|
92
|
+
}
|
|
93
|
+
sourceFile.forget();
|
|
94
|
+
}
|
|
95
|
+
handleRewriteError(error, filePath) {
|
|
96
|
+
if (error instanceof Error) {
|
|
97
|
+
__1.Logger.error(`Error: ${error.constructor.name}: ${CustomError_1.ErrorCodes.REWRITE_IMPORT_FAIL}`);
|
|
98
|
+
__1.Logger.error(`Error Message: 重写文件导入失败: ${filePath}`);
|
|
99
|
+
__1.Logger.error(`错误信息: ${error.message}\n${error.stack}`);
|
|
100
|
+
}
|
|
101
|
+
else {
|
|
102
|
+
__1.Logger.error(`重写文件导入失败: ${filePath}, 未知错误: ${error}`);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
escapeRegExp(string) {
|
|
106
|
+
return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
exports.ImportTransformServiceImpl = ImportTransformServiceImpl;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { ImportDeclaration } from 'ts-morph';
|
|
2
|
+
import { ITaskContext } from '../..';
|
|
3
|
+
import { GeneratedFileInfo, ImportScanStrategy } from '../types/ImportRewriteTypes';
|
|
4
|
+
export declare class ImportScanStrategyImpl implements ImportScanStrategy {
|
|
5
|
+
shouldRewriteImport(importDeclaration: ImportDeclaration, currentFilePath: string, generatedFileInfos: GeneratedFileInfo[], context: ITaskContext): GeneratedFileInfo | null;
|
|
6
|
+
shouldRewriteExport(moduleSpecifier: string, currentFilePath: string, generatedFileInfos: GeneratedFileInfo[], context: ITaskContext): GeneratedFileInfo | null;
|
|
7
|
+
private resolveModuleSpecifier;
|
|
8
|
+
private resolveModularImportToSourceFile;
|
|
9
|
+
private extractFilePathFromModularImport;
|
|
10
|
+
private isPathSuffix;
|
|
11
|
+
private removeFileExtension;
|
|
12
|
+
private isTypeDefinitionFile;
|
|
13
|
+
private isRelativeImport;
|
|
14
|
+
private resolveImportPath;
|
|
15
|
+
private pathMatches;
|
|
16
|
+
private extractImportedTypes;
|
|
17
|
+
}
|
|
@@ -0,0 +1,137 @@
|
|
|
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.ImportScanStrategyImpl = void 0;
|
|
7
|
+
const Logger_1 = require("../../logger/Logger");
|
|
8
|
+
const constants_1 = require("../../constants");
|
|
9
|
+
const SerializationPathUtil_1 = __importDefault(require("../../utils/SerializationPathUtil"));
|
|
10
|
+
class ImportScanStrategyImpl {
|
|
11
|
+
shouldRewriteImport(importDeclaration, currentFilePath, generatedFileInfos, context) {
|
|
12
|
+
const moduleSpecifier = importDeclaration.getModuleSpecifierValue();
|
|
13
|
+
const importedTypes = this.extractImportedTypes(importDeclaration);
|
|
14
|
+
if (this.isTypeDefinitionFile(currentFilePath, generatedFileInfos)) {
|
|
15
|
+
return null;
|
|
16
|
+
}
|
|
17
|
+
const matchedInfo = this.resolveModuleSpecifier(moduleSpecifier, currentFilePath, generatedFileInfos, context);
|
|
18
|
+
if (matchedInfo) {
|
|
19
|
+
const matchedTypes = importedTypes.filter(typeName => matchedInfo.classNames.has(typeName));
|
|
20
|
+
if (matchedTypes.length > 0) {
|
|
21
|
+
return matchedInfo;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
return null;
|
|
25
|
+
}
|
|
26
|
+
shouldRewriteExport(moduleSpecifier, currentFilePath, generatedFileInfos, context) {
|
|
27
|
+
if (!moduleSpecifier) {
|
|
28
|
+
return null;
|
|
29
|
+
}
|
|
30
|
+
if (this.isTypeDefinitionFile(currentFilePath, generatedFileInfos)) {
|
|
31
|
+
return null;
|
|
32
|
+
}
|
|
33
|
+
return this.resolveModuleSpecifier(moduleSpecifier, currentFilePath, generatedFileInfos, context);
|
|
34
|
+
}
|
|
35
|
+
resolveModuleSpecifier(moduleSpecifier, currentFilePath, generatedFileInfos, context) {
|
|
36
|
+
if (this.isRelativeImport(moduleSpecifier)) {
|
|
37
|
+
const resolvedSourcePath = this.resolveImportPath(moduleSpecifier, currentFilePath);
|
|
38
|
+
for (const generatedInfo of generatedFileInfos) {
|
|
39
|
+
if (this.pathMatches(resolvedSourcePath, generatedInfo.sourceFilePath)) {
|
|
40
|
+
return generatedInfo;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
return null;
|
|
44
|
+
}
|
|
45
|
+
return this.resolveModularImportToSourceFile(moduleSpecifier, generatedFileInfos, context);
|
|
46
|
+
}
|
|
47
|
+
resolveModularImportToSourceFile(moduleSpecifier, generatedFileInfos, context) {
|
|
48
|
+
const filePath = this.extractFilePathFromModularImport(moduleSpecifier);
|
|
49
|
+
if (!filePath) {
|
|
50
|
+
return null;
|
|
51
|
+
}
|
|
52
|
+
for (const info of generatedFileInfos) {
|
|
53
|
+
const sourceFileRelativePath = context.calculateSourceRootToModuleRoot(info.sourceFilePath);
|
|
54
|
+
const pathWithoutExtension = this.removeFileExtension(sourceFileRelativePath);
|
|
55
|
+
const normalizedSourcePath = pathWithoutExtension.replaceAll(SerializationPathUtil_1.default.sep, '/');
|
|
56
|
+
if (this.isPathSuffix(filePath, normalizedSourcePath)) {
|
|
57
|
+
Logger_1.Logger.debug(`模块化导入 '${moduleSpecifier}' 匹配源文件: ${info.sourceFilePath}`);
|
|
58
|
+
return info;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
return null;
|
|
62
|
+
}
|
|
63
|
+
extractFilePathFromModularImport(moduleSpecifier) {
|
|
64
|
+
const parts = moduleSpecifier.split('/');
|
|
65
|
+
if (parts.length < 2) {
|
|
66
|
+
return null;
|
|
67
|
+
}
|
|
68
|
+
let filePathStartIndex = 1;
|
|
69
|
+
if (parts[0].startsWith('@')) {
|
|
70
|
+
filePathStartIndex = 2;
|
|
71
|
+
}
|
|
72
|
+
if (filePathStartIndex >= parts.length) {
|
|
73
|
+
return null;
|
|
74
|
+
}
|
|
75
|
+
return parts.slice(filePathStartIndex).join('/');
|
|
76
|
+
}
|
|
77
|
+
isPathSuffix(shortPath, fullPath) {
|
|
78
|
+
const shortParts = shortPath.split('/').filter(p => p.length > 0);
|
|
79
|
+
const fullParts = fullPath.split('/').filter(p => p.length > 0);
|
|
80
|
+
if (shortParts.length > fullParts.length) {
|
|
81
|
+
return false;
|
|
82
|
+
}
|
|
83
|
+
const offset = fullParts.length - shortParts.length;
|
|
84
|
+
for (let i = 0; i < shortParts.length; i++) {
|
|
85
|
+
if (shortParts[i] !== fullParts[offset + i]) {
|
|
86
|
+
return false;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
return true;
|
|
90
|
+
}
|
|
91
|
+
removeFileExtension(filePath) {
|
|
92
|
+
const lastDotIndex = filePath.lastIndexOf('.');
|
|
93
|
+
if (lastDotIndex > 0) {
|
|
94
|
+
return filePath.substring(0, lastDotIndex);
|
|
95
|
+
}
|
|
96
|
+
return filePath;
|
|
97
|
+
}
|
|
98
|
+
isTypeDefinitionFile(filePath, generatedFileInfos) {
|
|
99
|
+
return generatedFileInfos.some(info => info.sourceFilePath === filePath);
|
|
100
|
+
}
|
|
101
|
+
isRelativeImport(moduleSpecifier) {
|
|
102
|
+
return moduleSpecifier.startsWith('./') || moduleSpecifier.startsWith('../');
|
|
103
|
+
}
|
|
104
|
+
resolveImportPath(moduleSpecifier, currentFilePath) {
|
|
105
|
+
if (this.isRelativeImport(moduleSpecifier)) {
|
|
106
|
+
const currentDir = SerializationPathUtil_1.default.dirname(currentFilePath);
|
|
107
|
+
const resolvedPath = SerializationPathUtil_1.default.pathResolve(currentDir, moduleSpecifier);
|
|
108
|
+
if (!SerializationPathUtil_1.default.extname(resolvedPath)) {
|
|
109
|
+
return resolvedPath + constants_1.PathConstants.ETS_EXTENSIONS;
|
|
110
|
+
}
|
|
111
|
+
return resolvedPath;
|
|
112
|
+
}
|
|
113
|
+
return moduleSpecifier;
|
|
114
|
+
}
|
|
115
|
+
pathMatches(resolvedPath, sourcePath) {
|
|
116
|
+
const normalizedResolved = SerializationPathUtil_1.default.pathResolve(resolvedPath);
|
|
117
|
+
const normalizedSource = SerializationPathUtil_1.default.pathResolve(sourcePath);
|
|
118
|
+
return normalizedResolved === normalizedSource;
|
|
119
|
+
}
|
|
120
|
+
extractImportedTypes(importDeclaration) {
|
|
121
|
+
const importedTypes = [];
|
|
122
|
+
const namedImports = importDeclaration.getNamedImports();
|
|
123
|
+
for (const namedImport of namedImports) {
|
|
124
|
+
importedTypes.push(namedImport.getName());
|
|
125
|
+
}
|
|
126
|
+
const defaultImport = importDeclaration.getDefaultImport();
|
|
127
|
+
if (defaultImport) {
|
|
128
|
+
importedTypes.push(defaultImport.getText());
|
|
129
|
+
}
|
|
130
|
+
const namespaceImport = importDeclaration.getNamespaceImport();
|
|
131
|
+
if (namespaceImport) {
|
|
132
|
+
Logger_1.Logger.warn(`跳过命名空间导入: ${namespaceImport.getText()}`);
|
|
133
|
+
}
|
|
134
|
+
return importedTypes;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
exports.ImportScanStrategyImpl = ImportScanStrategyImpl;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { ImportDeclaration } from 'ts-morph';
|
|
2
|
+
import { ITaskContext, ImportRewriteOptions } from '../..';
|
|
3
|
+
export interface GeneratedFileInfo {
|
|
4
|
+
sourceFilePath: string;
|
|
5
|
+
generatedFilePath: string;
|
|
6
|
+
classNames: Set<string>;
|
|
7
|
+
}
|
|
8
|
+
export interface ImportRewriteService {
|
|
9
|
+
rewriteImports(context: ITaskContext, generatedFileInfos: GeneratedFileInfo[], options: ImportRewriteOptions): void;
|
|
10
|
+
}
|
|
11
|
+
export interface ImportScanStrategy {
|
|
12
|
+
shouldRewriteImport(importDeclaration: ImportDeclaration, currentFilePath: string, generatedFileInfos: GeneratedFileInfo[], context: ITaskContext): GeneratedFileInfo | null;
|
|
13
|
+
shouldRewriteExport(moduleSpecifier: string, currentFilePath: string, generatedFileInfos: GeneratedFileInfo[], context: ITaskContext): GeneratedFileInfo | null;
|
|
14
|
+
}
|
|
15
|
+
export interface ImportTransformService {
|
|
16
|
+
rewriteFileImports(filePath: string, context: ITaskContext, generatedFileInfos: GeneratedFileInfo[], preserveOriginalImports?: boolean): void;
|
|
17
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export * from './interfaces';
|
|
2
|
+
export * from './logger/Logger';
|
|
3
|
+
export * from './constants';
|
|
4
|
+
export * from './Types';
|
|
5
|
+
export * from './services/CodeGenerationEngine';
|
|
6
|
+
export * from './import-rewrite/types/ImportRewriteTypes';
|
|
7
|
+
export * from './import-rewrite/services/ImportRewriteService';
|
|
8
|
+
export * from './utils/ConfigManager';
|
|
9
|
+
export { TempSerializerGenerator } from './services/CodeGenerationService/generators/TempSerializerGenerator';
|
|
@@ -0,0 +1,27 @@
|
|
|
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 __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
exports.TempSerializerGenerator = void 0;
|
|
18
|
+
__exportStar(require("./interfaces"), exports);
|
|
19
|
+
__exportStar(require("./logger/Logger"), exports);
|
|
20
|
+
__exportStar(require("./constants"), exports);
|
|
21
|
+
__exportStar(require("./Types"), exports);
|
|
22
|
+
__exportStar(require("./services/CodeGenerationEngine"), exports);
|
|
23
|
+
__exportStar(require("./import-rewrite/types/ImportRewriteTypes"), exports);
|
|
24
|
+
__exportStar(require("./import-rewrite/services/ImportRewriteService"), exports);
|
|
25
|
+
__exportStar(require("./utils/ConfigManager"), exports);
|
|
26
|
+
var TempSerializerGenerator_1 = require("./services/CodeGenerationService/generators/TempSerializerGenerator");
|
|
27
|
+
Object.defineProperty(exports, "TempSerializerGenerator", { enumerable: true, get: function () { return TempSerializerGenerator_1.TempSerializerGenerator; } });
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export interface ITaskContext {
|
|
2
|
+
mapToOutputPath(absSourceFile: string): string;
|
|
3
|
+
getOutputRoot(): string;
|
|
4
|
+
getModuleName(): string;
|
|
5
|
+
getModulePath(): string;
|
|
6
|
+
getPackageName(): string;
|
|
7
|
+
getRelativePath(absolutePath: string): string;
|
|
8
|
+
isValidOutputPath(outputPath: string): boolean;
|
|
9
|
+
ensureOutputDirectory(outputPath: string): void;
|
|
10
|
+
calculateSourceRootToModuleRoot(absolutePath: string): string;
|
|
11
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export declare class Logger {
|
|
2
|
+
private static currentModule;
|
|
3
|
+
static setModuleContext(moduleName: string): void;
|
|
4
|
+
static clearModuleContext(): void;
|
|
5
|
+
static getCurrentModule(): string;
|
|
6
|
+
private static formatMessage;
|
|
7
|
+
static error(msg: string, ...args: unknown[]): void;
|
|
8
|
+
static info(msg: string, ...args: unknown[]): void;
|
|
9
|
+
static warn(msg: string, ...args: unknown[]): void;
|
|
10
|
+
static debug(msg: string, ...args: unknown[]): void;
|
|
11
|
+
static withModuleContext<T>(moduleName: string, fn: () => T): T;
|
|
12
|
+
static withModuleContextAsync<T>(moduleName: string, fn: () => Promise<T>): Promise<T>;
|
|
13
|
+
}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Logger = void 0;
|
|
4
|
+
const hvigor_1 = require("@ohos/hvigor");
|
|
5
|
+
const PREFIX = '[TSerialization]';
|
|
6
|
+
class Logger {
|
|
7
|
+
static setModuleContext(moduleName) {
|
|
8
|
+
this.currentModule = moduleName;
|
|
9
|
+
}
|
|
10
|
+
static clearModuleContext() {
|
|
11
|
+
this.currentModule = '';
|
|
12
|
+
}
|
|
13
|
+
static getCurrentModule() {
|
|
14
|
+
return this.currentModule;
|
|
15
|
+
}
|
|
16
|
+
static formatMessage(msg) {
|
|
17
|
+
const modulePrefix = this.currentModule ? `[${this.currentModule}]` : '';
|
|
18
|
+
return `${PREFIX}${modulePrefix} ${msg}`;
|
|
19
|
+
}
|
|
20
|
+
static error(msg, ...args) {
|
|
21
|
+
const formattedMsg = this.formatMessage(msg);
|
|
22
|
+
if (args.length > 0) {
|
|
23
|
+
hvigor_1.HvigorLogger.getLogger().error(formattedMsg, ...args);
|
|
24
|
+
}
|
|
25
|
+
else {
|
|
26
|
+
hvigor_1.HvigorLogger.getLogger().error(formattedMsg);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
static info(msg, ...args) {
|
|
30
|
+
const formattedMsg = this.formatMessage(msg);
|
|
31
|
+
if (args.length > 0) {
|
|
32
|
+
hvigor_1.HvigorLogger.getLogger().info(formattedMsg, ...args);
|
|
33
|
+
}
|
|
34
|
+
else {
|
|
35
|
+
hvigor_1.HvigorLogger.getLogger().info(formattedMsg);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
static warn(msg, ...args) {
|
|
39
|
+
const formattedMsg = this.formatMessage(msg);
|
|
40
|
+
if (args.length > 0) {
|
|
41
|
+
hvigor_1.HvigorLogger.getLogger().warn(formattedMsg, ...args);
|
|
42
|
+
}
|
|
43
|
+
else {
|
|
44
|
+
hvigor_1.HvigorLogger.getLogger().warn(formattedMsg);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
static debug(msg, ...args) {
|
|
48
|
+
const formattedMsg = this.formatMessage(msg);
|
|
49
|
+
if (args.length > 0) {
|
|
50
|
+
hvigor_1.HvigorLogger.getLogger().debug(formattedMsg, ...args);
|
|
51
|
+
}
|
|
52
|
+
else {
|
|
53
|
+
hvigor_1.HvigorLogger.getLogger().debug(formattedMsg);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
static withModuleContext(moduleName, fn) {
|
|
57
|
+
const previousModule = this.currentModule;
|
|
58
|
+
try {
|
|
59
|
+
this.setModuleContext(moduleName);
|
|
60
|
+
return fn();
|
|
61
|
+
}
|
|
62
|
+
finally {
|
|
63
|
+
this.currentModule = previousModule;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
static async withModuleContextAsync(moduleName, fn) {
|
|
67
|
+
const previousModule = this.currentModule;
|
|
68
|
+
try {
|
|
69
|
+
this.setModuleContext(moduleName);
|
|
70
|
+
return await fn();
|
|
71
|
+
}
|
|
72
|
+
finally {
|
|
73
|
+
this.currentModule = previousModule;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
exports.Logger = Logger;
|
|
78
|
+
Logger.currentModule = '';
|