|
| 1 | +import { |
| 2 | + AccessLevel, |
| 3 | + EggProtoImplClass, |
| 4 | + EggQualifierAttribute, |
| 5 | + EggType, |
| 6 | + InitTypeQualifierAttribute, |
| 7 | + LoadUnitNameQualifierAttribute, ModuleConfigs, |
| 8 | + ObjectInitType, |
| 9 | + PrototypeUtil, |
| 10 | + QualifierUtil, |
| 11 | + ModuleConfigHolder, ConfigSourceQualifierAttribute, |
| 12 | +} from '@eggjs/tegg'; |
| 13 | +import { ModuleConfigUtil } from '@eggjs/tegg/helper'; |
| 14 | +import { COMPATIBLE_PROTO_IMPLE_TYPE } from './EggCompatibleProtoImpl'; |
| 15 | +import { Application } from 'egg'; |
| 16 | + |
| 17 | +export class ModuleConfigLoader { |
| 18 | + constructor(readonly app: Application) { |
| 19 | + } |
| 20 | + |
| 21 | + private loadModuleConfigs(moduleConfigMap: Record<string, ModuleConfigHolder>): EggProtoImplClass { |
| 22 | + const moduleConfigs = new ModuleConfigs(moduleConfigMap); |
| 23 | + const func: EggProtoImplClass = function() { |
| 24 | + return moduleConfigs; |
| 25 | + } as any; |
| 26 | + const name = 'moduleConfigs'; |
| 27 | + Object.defineProperty(func, 'name', { |
| 28 | + value: name, |
| 29 | + writable: false, |
| 30 | + enumerable: false, |
| 31 | + configurable: true, |
| 32 | + }); |
| 33 | + PrototypeUtil.setIsEggPrototype(func); |
| 34 | + PrototypeUtil.setFilePath(func, 'mock_file_path'); |
| 35 | + PrototypeUtil.setProperty(func, { |
| 36 | + name, |
| 37 | + initType: ObjectInitType.SINGLETON, |
| 38 | + accessLevel: AccessLevel.PUBLIC, |
| 39 | + protoImplType: COMPATIBLE_PROTO_IMPLE_TYPE, |
| 40 | + }); |
| 41 | + QualifierUtil.addProtoQualifier(func, LoadUnitNameQualifierAttribute, 'app'); |
| 42 | + QualifierUtil.addProtoQualifier(func, InitTypeQualifierAttribute, ObjectInitType.SINGLETON); |
| 43 | + QualifierUtil.addProtoQualifier(func, EggQualifierAttribute, EggType.APP); |
| 44 | + return func; |
| 45 | + } |
| 46 | + |
| 47 | + loadModuleConfigList(): EggProtoImplClass[] { |
| 48 | + const result: EggProtoImplClass[] = []; |
| 49 | + const moduleConfigMap: Record<string, ModuleConfigHolder> = {}; |
| 50 | + for (const reference of this.app.moduleReferences) { |
| 51 | + const moduleName = ModuleConfigUtil.readModuleNameSync(reference.path); |
| 52 | + const config = ModuleConfigUtil.loadModuleConfigSync(reference.path, undefined, this.app.config.env) || {}; |
| 53 | + moduleConfigMap[moduleName] = { |
| 54 | + name: moduleName, |
| 55 | + reference: { |
| 56 | + name: moduleName, |
| 57 | + path: reference.path, |
| 58 | + }, |
| 59 | + config, |
| 60 | + }; |
| 61 | + |
| 62 | + const func: EggProtoImplClass = function() { |
| 63 | + return config; |
| 64 | + } as any; |
| 65 | + const name = 'moduleConfig'; |
| 66 | + Object.defineProperty(func, 'name', { |
| 67 | + value: name, |
| 68 | + writable: false, |
| 69 | + enumerable: false, |
| 70 | + configurable: true, |
| 71 | + }); |
| 72 | + PrototypeUtil.setIsEggPrototype(func); |
| 73 | + PrototypeUtil.setFilePath(func, 'mock_file_path'); |
| 74 | + PrototypeUtil.setProperty(func, { |
| 75 | + name, |
| 76 | + initType: ObjectInitType.SINGLETON, |
| 77 | + accessLevel: AccessLevel.PUBLIC, |
| 78 | + protoImplType: COMPATIBLE_PROTO_IMPLE_TYPE, |
| 79 | + }); |
| 80 | + QualifierUtil.addProtoQualifier(func, LoadUnitNameQualifierAttribute, 'app'); |
| 81 | + QualifierUtil.addProtoQualifier(func, InitTypeQualifierAttribute, ObjectInitType.SINGLETON); |
| 82 | + QualifierUtil.addProtoQualifier(func, EggQualifierAttribute, EggType.APP); |
| 83 | + QualifierUtil.addProtoQualifier(func, ConfigSourceQualifierAttribute, moduleName); |
| 84 | + result.push(func); |
| 85 | + } |
| 86 | + const moduleConfigs = this.loadModuleConfigs(moduleConfigMap); |
| 87 | + result.push(moduleConfigs); |
| 88 | + return result; |
| 89 | + } |
| 90 | +} |
0 commit comments