@eggjs/tegg-standalone 4.0.0-beta.8 → 4.0.0-beta.9

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.
@@ -0,0 +1,23 @@
1
+ import { type EggPrototype, type LoadUnit } from '@eggjs/tegg-metadata';
2
+ import { type QualifierInfo } from '@eggjs/tegg';
3
+ export declare const StandaloneLoadUnitType = "StandaloneLoadUnitType";
4
+ export interface InnerObject {
5
+ obj: object;
6
+ qualifiers?: QualifierInfo[];
7
+ }
8
+ export declare class StandaloneLoadUnit implements LoadUnit {
9
+ readonly id: string;
10
+ readonly name: string;
11
+ readonly unitPath: string;
12
+ readonly type = "StandaloneLoadUnitType";
13
+ private innerObject;
14
+ private protoMap;
15
+ constructor(innerObject: Record<string, InnerObject[]>);
16
+ init(): Promise<void>;
17
+ containPrototype(proto: EggPrototype): boolean;
18
+ getEggPrototype(name: string, qualifiers: QualifierInfo[]): EggPrototype[];
19
+ registerEggPrototype(proto: EggPrototype): void;
20
+ deletePrototype(proto: EggPrototype): void;
21
+ destroy(): Promise<void>;
22
+ iterateEggPrototype(): IterableIterator<EggPrototype>;
23
+ }
@@ -0,0 +1,62 @@
1
+ import { EggPrototypeFactory } from '@eggjs/tegg-metadata';
2
+ import { ObjectInitType } from '@eggjs/tegg';
3
+ import { MapUtil } from '@eggjs/tegg-common-util';
4
+ import { IdenticalUtil } from '@eggjs/tegg-lifecycle';
5
+ import { StandaloneInnerObjectProto } from "./StandaloneInnerObjectProto.js";
6
+ export const StandaloneLoadUnitType = 'StandaloneLoadUnitType';
7
+ export class StandaloneLoadUnit {
8
+ id = 'StandaloneLoadUnit';
9
+ name = 'StandaloneLoadUnit';
10
+ unitPath = 'MockStandaloneLoadUnitPath';
11
+ type = StandaloneLoadUnitType;
12
+ innerObject;
13
+ protoMap = new Map();
14
+ constructor(innerObject) {
15
+ this.innerObject = innerObject;
16
+ }
17
+ async init() {
18
+ for (const [name, objs] of Object.entries(this.innerObject)) {
19
+ for (const { obj, qualifiers } of objs) {
20
+ const proto = new StandaloneInnerObjectProto(IdenticalUtil.createProtoId(this.id, name), name, (() => obj), ObjectInitType.SINGLETON, this.id, qualifiers || []);
21
+ EggPrototypeFactory.instance.registerPrototype(proto, this);
22
+ }
23
+ }
24
+ }
25
+ containPrototype(proto) {
26
+ return !!(this.protoMap.get(proto.name)?.find(t => t === proto));
27
+ }
28
+ getEggPrototype(name, qualifiers) {
29
+ const protos = this.protoMap.get(name);
30
+ return protos?.filter(proto => proto.verifyQualifiers(qualifiers)) || [];
31
+ }
32
+ registerEggPrototype(proto) {
33
+ const protoList = MapUtil.getOrStore(this.protoMap, proto.name, []);
34
+ protoList.push(proto);
35
+ }
36
+ deletePrototype(proto) {
37
+ const protos = this.protoMap.get(proto.name);
38
+ if (protos) {
39
+ const index = protos.indexOf(proto);
40
+ if (index !== -1) {
41
+ protos.splice(index, 1);
42
+ }
43
+ }
44
+ }
45
+ async destroy() {
46
+ for (const namedProtoMap of this.protoMap.values()) {
47
+ for (const proto of namedProtoMap.values()) {
48
+ EggPrototypeFactory.instance.deletePrototype(proto, this);
49
+ }
50
+ }
51
+ this.protoMap.clear();
52
+ }
53
+ iterateEggPrototype() {
54
+ const protos = Array.from(this.protoMap.values())
55
+ .reduce((p, c) => {
56
+ p = p.concat(c);
57
+ return p;
58
+ }, []);
59
+ return protos.values();
60
+ }
61
+ }
62
+ //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiU3RhbmRhbG9uZUxvYWRVbml0LmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vc3JjL1N0YW5kYWxvbmVMb2FkVW5pdC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSxPQUFPLEVBQXFCLG1CQUFtQixFQUFpQixNQUFNLHNCQUFzQixDQUFDO0FBQzdGLE9BQU8sRUFBeUIsY0FBYyxFQUFzQixNQUFNLGFBQWEsQ0FBQzt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HANGED
@@ -1,120 +1,6 @@
1
- import { EggPrototype, EggPrototypeLifecycleContext, InjectObjectProto, LoadUnit } from "@eggjs/tegg-metadata";
2
- import { ModuleReference, ReadModuleReferenceOptions } from "@eggjs/tegg-common-util";
3
- import { AbstractEggContext, EggContext, EggObject, LoadUnitInstance } from "@eggjs/tegg-runtime";
4
- import { AccessLevel, EggObjectName, EggProtoImplClass, EggPrototypeName, Id, Logger, MetaDataKey, ModuleConfigHolder, ObjectInitTypeLike, QualifierInfo, QualifierValue } from "@eggjs/tegg";
5
-
6
- //#region src/EggModuleLoader.d.ts
7
- interface EggModuleLoaderOptions {
8
- logger: Logger;
9
- baseDir: string;
10
- dump?: boolean;
11
- }
12
- declare class EggModuleLoader {
13
- private moduleReferences;
14
- private globalGraph;
15
- private options;
16
- constructor(moduleReferences: readonly ModuleReference[], options: EggModuleLoaderOptions);
17
- init(): Promise<void>;
18
- private static generateAppGraph;
19
- load(): Promise<LoadUnit[]>;
20
- static preLoad(moduleReferences: readonly ModuleReference[], options: EggModuleLoaderOptions): Promise<void>;
21
- }
22
- //#endregion
23
- //#region src/StandaloneLoadUnit.d.ts
24
- interface InnerObject {
25
- obj: object;
26
- qualifiers?: QualifierInfo[];
27
- }
28
- //#endregion
29
- //#region src/Runner.d.ts
30
- interface ModuleDependency extends ReadModuleReferenceOptions {
31
- baseDir: string;
32
- }
33
- interface RunnerOptions {
34
- /**
35
- * @deprecated
36
- * use inner object handlers instead
37
- */
38
- innerObjects?: Record<string, object>;
39
- env?: string;
40
- name?: string;
41
- innerObjectHandlers?: Record<string, InnerObject[]>;
42
- dependencies?: (string | ModuleDependency)[];
43
- dump?: boolean;
44
- }
45
- declare class Runner {
46
- readonly cwd: string;
47
- readonly moduleReferences: readonly ModuleReference[];
48
- readonly moduleConfigs: Record<string, ModuleConfigHolder>;
49
- readonly env?: string;
50
- readonly name?: string;
51
- readonly options?: RunnerOptions;
52
- private loadUnitLoader;
53
- private runnerProto;
54
- private configSourceEggPrototypeHook;
55
- private loadUnitMultiInstanceProtoHook;
56
- private dalTableEggPrototypeHook;
57
- private dalModuleLoadUnitHook;
58
- private transactionPrototypeHook;
59
- private crosscutAdviceFactory;
60
- private loadUnitAopHook;
61
- private eggPrototypeCrossCutHook;
62
- private eggObjectAopHook;
63
- loadUnits: LoadUnit[];
64
- loadUnitInstances: LoadUnitInstance[];
65
- innerObjects: Record<string, InnerObject[]>;
66
- constructor(cwd: string, options?: RunnerOptions);
67
- load(): Promise<LoadUnit[]>;
68
- static getModuleReferences(cwd: string, dependencies?: RunnerOptions['dependencies']): readonly ModuleReference[];
69
- static preLoad(cwd: string, dependencies?: RunnerOptions['dependencies']): Promise<void>;
70
- private initLoaderInstance;
71
- init(): Promise<void>;
72
- run<T>(aCtx?: EggContext): Promise<T>;
73
- destroy(): Promise<void>;
74
- }
75
- //#endregion
76
- //#region src/main.d.ts
77
- declare function preLoad(cwd: string, dependencies?: RunnerOptions['dependencies']): Promise<void>;
78
- declare function main<T = void>(cwd: string, options?: RunnerOptions): Promise<T>;
79
- //#endregion
80
- //#region src/StandaloneInnerObjectProto.d.ts
81
- declare class StandaloneInnerObjectProto implements EggPrototype {
82
- [key: symbol]: PropertyDescriptor;
83
- private readonly clazz;
84
- private readonly qualifiers;
85
- readonly id: string;
86
- readonly name: EggPrototypeName;
87
- readonly initType: ObjectInitTypeLike;
88
- readonly accessLevel: AccessLevel;
89
- readonly injectObjects: InjectObjectProto[];
90
- readonly loadUnitId: Id;
91
- constructor(id: string, name: EggPrototypeName, clazz: EggProtoImplClass, initType: ObjectInitTypeLike, loadUnitId: Id, qualifiers: QualifierInfo[]);
92
- verifyQualifiers(qualifiers: QualifierInfo[]): boolean;
93
- verifyQualifier(qualifier: QualifierInfo): boolean;
94
- constructEggObject(): object;
95
- getMetaData<T>(metadataKey: MetaDataKey): T | undefined;
96
- getQualifier(attribute: string): QualifierValue | undefined;
97
- static create(ctx: EggPrototypeLifecycleContext): EggPrototype;
98
- }
99
- //#endregion
100
- //#region src/StandaloneContext.d.ts
101
- declare class StandaloneContext extends AbstractEggContext {
102
- id: string;
103
- constructor();
104
- }
105
- //#endregion
106
- //#region src/StandaloneInnerObject.d.ts
107
- declare const OBJ: unique symbol;
108
- declare class StandaloneInnerObject implements EggObject {
109
- readonly isReady: boolean;
110
- private [OBJ];
111
- readonly proto: StandaloneInnerObjectProto;
112
- readonly name: EggObjectName;
113
- readonly id: string;
114
- constructor(name: EggObjectName, proto: StandaloneInnerObjectProto);
115
- get obj(): object;
116
- injectProperty(): void;
117
- static createObject(name: EggObjectName, proto: EggPrototype): Promise<StandaloneInnerObject>;
118
- }
119
- //#endregion
120
- export { EggModuleLoader, EggModuleLoaderOptions, ModuleDependency, Runner, RunnerOptions, StandaloneContext, StandaloneInnerObject, StandaloneInnerObjectProto, main, preLoad };
1
+ export * from './EggModuleLoader.ts';
2
+ export * from './Runner.ts';
3
+ export * from './main.ts';
4
+ export * from './StandaloneInnerObjectProto.ts';
5
+ export * from './StandaloneContext.ts';
6
+ export * from './StandaloneInnerObject.ts';
package/dist/index.js CHANGED
@@ -1,415 +1,7 @@
1
- import { EggLoadUnitType, EggPrototypeFactory, EggPrototypeLifecycleUtil, GlobalGraph, LoadUnitFactory, LoadUnitLifecycleUtil, LoadUnitMultiInstanceProtoHook, ModuleDescriptorDumper } from "@eggjs/tegg-metadata";
2
- import { LoaderFactory } from "@eggjs/tegg-loader";
3
- import { MapUtil, ModuleConfigUtil } from "@eggjs/tegg-common-util";
4
- import { AbstractEggContext, ContextHandler, EggContainerFactory, EggObjectFactory, EggObjectLifecycleUtil, LoadUnitInstanceFactory, ModuleLoadUnitInstance } from "@eggjs/tegg-runtime";
5
- import { AccessLevel, ConfigSourceQualifier, ConfigSourceQualifierAttribute, IdenticalUtil, MetadataUtil, ModuleConfigs, ObjectInitType, PrototypeUtil, QualifierUtil } from "@eggjs/tegg";
6
- import { StandaloneUtil } from "@eggjs/tegg/standalone";
7
- import { CrosscutAdviceFactory } from "@eggjs/tegg/aop";
8
- import { EggObjectAopHook, EggPrototypeCrossCutHook, LoadUnitAopHook, crossCutGraphHook, pointCutGraphHook } from "@eggjs/tegg-aop-runtime";
9
- import { DalModuleLoadUnitHook, DalTableEggPrototypeHook, MysqlDataSourceManager, SqlMapManager, TableModelManager, TransactionPrototypeHook } from "@eggjs/tegg-dal-plugin";
10
- import { IdenticalUtil as IdenticalUtil$1 } from "@eggjs/tegg-lifecycle";
11
- import { AsyncLocalStorage } from "node:async_hooks";
12
-
13
- //#region src/EggModuleLoader.ts
14
- var EggModuleLoader = class EggModuleLoader {
15
- moduleReferences;
16
- globalGraph;
17
- options;
18
- constructor(moduleReferences, options) {
19
- this.moduleReferences = moduleReferences;
20
- this.options = options;
21
- }
22
- async init() {
23
- GlobalGraph.instance = this.globalGraph = await EggModuleLoader.generateAppGraph(this.moduleReferences, this.options);
24
- }
25
- static async generateAppGraph(moduleReferences, options) {
26
- const moduleDescriptors = await LoaderFactory.loadApp(moduleReferences);
27
- if (options.dump !== false) for (const moduleDescriptor of moduleDescriptors) ModuleDescriptorDumper.dump(moduleDescriptor, { dumpDir: options.baseDir }).catch((e) => {
28
- e.message = "dump module descriptor failed: " + e.message;
29
- options.logger.warn(e);
30
- });
31
- return await GlobalGraph.create(moduleDescriptors);
32
- }
33
- async load() {
34
- const loadUnits = [];
35
- this.globalGraph.build();
36
- this.globalGraph.sort();
37
- const moduleConfigList = GlobalGraph.instance.moduleConfigList;
38
- for (const moduleConfig of moduleConfigList) {
39
- const modulePath = moduleConfig.path;
40
- const loader = LoaderFactory.createLoader(modulePath, EggLoadUnitType.MODULE);
41
- const loadUnit = await LoadUnitFactory.createLoadUnit(modulePath, EggLoadUnitType.MODULE, loader);
42
- loadUnits.push(loadUnit);
43
- }
44
- return loadUnits;
45
- }
46
- static async preLoad(moduleReferences, options) {
47
- const loadUnits = [];
48
- const loaderCache = /* @__PURE__ */ new Map();
49
- const globalGraph = GlobalGraph.instance = await EggModuleLoader.generateAppGraph(moduleReferences, options);
50
- globalGraph.sort();
51
- const moduleConfigList = globalGraph.moduleConfigList;
52
- for (const moduleConfig of moduleConfigList) {
53
- const modulePath = moduleConfig.path;
54
- const loader = loaderCache.get(modulePath);
55
- const loadUnit = await LoadUnitFactory.createPreloadLoadUnit(modulePath, EggLoadUnitType.MODULE, loader);
56
- loadUnits.push(loadUnit);
57
- }
58
- for (const load of loadUnits) await load.preLoad?.();
59
- }
60
- };
61
-
62
- //#endregion
63
- //#region src/StandaloneInnerObjectProto.ts
64
- var StandaloneInnerObjectProto = class StandaloneInnerObjectProto {
65
- clazz;
66
- qualifiers;
67
- id;
68
- name;
69
- initType;
70
- accessLevel;
71
- injectObjects;
72
- loadUnitId;
73
- constructor(id, name, clazz, initType, loadUnitId, qualifiers) {
74
- this.id = id;
75
- this.clazz = clazz;
76
- this.name = name;
77
- this.initType = initType;
78
- this.accessLevel = AccessLevel.PUBLIC;
79
- this.injectObjects = [];
80
- this.loadUnitId = loadUnitId;
81
- this.qualifiers = qualifiers;
82
- }
83
- verifyQualifiers(qualifiers) {
84
- for (const qualifier of qualifiers) if (!this.verifyQualifier(qualifier)) return false;
85
- return true;
86
- }
87
- verifyQualifier(qualifier) {
88
- return this.qualifiers.find((t) => t.attribute === qualifier.attribute)?.value === qualifier.value;
89
- }
90
- constructEggObject() {
91
- return Reflect.apply(this.clazz, null, []);
92
- }
93
- getMetaData(metadataKey) {
94
- return MetadataUtil.getMetaData(metadataKey, this.clazz);
95
- }
96
- getQualifier(attribute) {
97
- return this.qualifiers.find((t) => t.attribute === attribute)?.value;
98
- }
99
- static create(ctx) {
100
- const { clazz, loadUnit } = ctx;
101
- const name = ctx.prototypeInfo.name;
102
- const id = IdenticalUtil.createProtoId(loadUnit.id, name);
103
- return new StandaloneInnerObjectProto(id, name, clazz, ctx.prototypeInfo.initType, loadUnit.id, QualifierUtil.getProtoQualifiers(clazz));
104
- }
105
- };
106
-
107
- //#endregion
108
- //#region src/StandaloneLoadUnit.ts
109
- const StandaloneLoadUnitType = "StandaloneLoadUnitType";
110
- var StandaloneLoadUnit = class {
111
- id = "StandaloneLoadUnit";
112
- name = "StandaloneLoadUnit";
113
- unitPath = "MockStandaloneLoadUnitPath";
114
- type = StandaloneLoadUnitType;
115
- innerObject;
116
- protoMap = /* @__PURE__ */ new Map();
117
- constructor(innerObject) {
118
- this.innerObject = innerObject;
119
- }
120
- async init() {
121
- for (const [name, objs] of Object.entries(this.innerObject)) for (const { obj, qualifiers } of objs) {
122
- const proto = new StandaloneInnerObjectProto(IdenticalUtil$1.createProtoId(this.id, name), name, (() => obj), ObjectInitType.SINGLETON, this.id, qualifiers || []);
123
- EggPrototypeFactory.instance.registerPrototype(proto, this);
124
- }
125
- }
126
- containPrototype(proto) {
127
- return !!this.protoMap.get(proto.name)?.find((t) => t === proto);
128
- }
129
- getEggPrototype(name, qualifiers) {
130
- return this.protoMap.get(name)?.filter((proto) => proto.verifyQualifiers(qualifiers)) || [];
131
- }
132
- registerEggPrototype(proto) {
133
- MapUtil.getOrStore(this.protoMap, proto.name, []).push(proto);
134
- }
135
- deletePrototype(proto) {
136
- const protos = this.protoMap.get(proto.name);
137
- if (protos) {
138
- const index = protos.indexOf(proto);
139
- if (index !== -1) protos.splice(index, 1);
140
- }
141
- }
142
- async destroy() {
143
- for (const namedProtoMap of this.protoMap.values()) for (const proto of namedProtoMap.values()) EggPrototypeFactory.instance.deletePrototype(proto, this);
144
- this.protoMap.clear();
145
- }
146
- iterateEggPrototype() {
147
- return Array.from(this.protoMap.values()).reduce((p, c) => {
148
- p = p.concat(c);
149
- return p;
150
- }, []).values();
151
- }
152
- };
153
-
154
- //#endregion
155
- //#region src/StandaloneContext.ts
156
- var StandaloneContext = class extends AbstractEggContext {
157
- id;
158
- constructor() {
159
- super();
160
- this.id = IdenticalUtil$1.createContextId();
161
- }
162
- };
163
-
164
- //#endregion
165
- //#region src/StandaloneContextHandler.ts
166
- var StandaloneContextHandler = class StandaloneContextHandler {
167
- static storage = new AsyncLocalStorage();
168
- static register() {
169
- ContextHandler.getContextCallback = () => {
170
- return StandaloneContextHandler.storage.getStore();
171
- };
172
- ContextHandler.runInContextCallback = (context, fn) => {
173
- return StandaloneContextHandler.storage.run(context, fn);
174
- };
175
- }
176
- };
177
-
178
- //#endregion
179
- //#region src/ConfigSourceLoadUnitHook.ts
180
- /**
181
- * Hook for inject moduleConfig.
182
- * Add default qualifier value is current module name.
183
- */
184
- var ConfigSourceLoadUnitHook = class {
185
- async preCreate(ctx, loadUnit) {
186
- const classList = await ctx.loader.load();
187
- for (const clazz of classList) {
188
- const moduleConfigObject = PrototypeUtil.getInjectObjects(clazz).find((t) => t.objName === "moduleConfig");
189
- const configSourceQualifier = QualifierUtil.getProperQualifier(clazz, "moduleConfig", ConfigSourceQualifierAttribute);
190
- if (moduleConfigObject && !configSourceQualifier) ConfigSourceQualifier(loadUnit.name)(clazz.prototype, moduleConfigObject.refName);
191
- }
192
- }
193
- };
194
-
195
- //#endregion
196
- //#region src/Runner.ts
197
- var Runner = class Runner {
198
- cwd;
199
- moduleReferences;
200
- moduleConfigs;
201
- env;
202
- name;
203
- options;
204
- loadUnitLoader;
205
- runnerProto;
206
- configSourceEggPrototypeHook;
207
- loadUnitMultiInstanceProtoHook;
208
- dalTableEggPrototypeHook;
209
- dalModuleLoadUnitHook;
210
- transactionPrototypeHook;
211
- crosscutAdviceFactory;
212
- loadUnitAopHook;
213
- eggPrototypeCrossCutHook;
214
- eggObjectAopHook;
215
- loadUnits = [];
216
- loadUnitInstances = [];
217
- innerObjects;
218
- constructor(cwd, options) {
219
- this.cwd = cwd;
220
- this.env = options?.env;
221
- this.name = options?.name;
222
- this.options = options;
223
- this.moduleReferences = Runner.getModuleReferences(this.cwd, options?.dependencies);
224
- this.moduleConfigs = {};
225
- this.innerObjects = {
226
- moduleConfigs: [{ obj: new ModuleConfigs(this.moduleConfigs) }],
227
- moduleConfig: []
228
- };
229
- const runtimeConfig = {
230
- baseDir: this.cwd,
231
- name: this.name,
232
- env: this.env
233
- };
234
- this.innerObjects.runtimeConfig = [{ obj: runtimeConfig }];
235
- if (!ModuleConfigUtil.configNames) ModuleConfigUtil.configNames = ["module.default", `module.${this.env}`];
236
- for (const reference of this.moduleReferences) {
237
- const absoluteRef = {
238
- path: ModuleConfigUtil.resolveModuleDir(reference.path, this.cwd),
239
- name: reference.name
240
- };
241
- const moduleName = ModuleConfigUtil.readModuleNameSync(absoluteRef.path);
242
- this.moduleConfigs[moduleName] = {
243
- name: moduleName,
244
- reference: absoluteRef,
245
- config: ModuleConfigUtil.loadModuleConfigSync(absoluteRef.path)
246
- };
247
- }
248
- for (const moduleConfig of Object.values(this.moduleConfigs)) this.innerObjects.moduleConfig.push({
249
- obj: moduleConfig.config,
250
- qualifiers: [{
251
- attribute: ConfigSourceQualifierAttribute,
252
- value: moduleConfig.name
253
- }]
254
- });
255
- if (options?.innerObjects) for (const [name, obj] of Object.entries(options.innerObjects)) this.innerObjects[name] = [{ obj }];
256
- else if (options?.innerObjectHandlers) Object.assign(this.innerObjects, options.innerObjectHandlers);
257
- }
258
- async load() {
259
- StandaloneContextHandler.register();
260
- LoadUnitFactory.registerLoadUnitCreator(StandaloneLoadUnitType, () => {
261
- return new StandaloneLoadUnit(this.innerObjects);
262
- });
263
- LoadUnitInstanceFactory.registerLoadUnitInstanceClass(StandaloneLoadUnitType, ModuleLoadUnitInstance.createModuleLoadUnitInstance);
264
- const standaloneLoadUnit = await LoadUnitFactory.createLoadUnit("MockStandaloneLoadUnitPath", StandaloneLoadUnitType, { async load() {
265
- return [];
266
- } });
267
- const loadUnits = await this.loadUnitLoader.load();
268
- return [standaloneLoadUnit, ...loadUnits];
269
- }
270
- static getModuleReferences(cwd, dependencies) {
271
- return (dependencies || []).concat(cwd).reduce((list, baseDir) => {
272
- const module = typeof baseDir === "string" ? { baseDir } : baseDir;
273
- return list.concat(...ModuleConfigUtil.readModuleReference(module.baseDir, module));
274
- }, []);
275
- }
276
- static async preLoad(cwd, dependencies) {
277
- const moduleReferences = Runner.getModuleReferences(cwd, dependencies);
278
- await EggModuleLoader.preLoad(moduleReferences, {
279
- baseDir: cwd,
280
- logger: console,
281
- dump: false
282
- });
283
- }
284
- async initLoaderInstance() {
285
- this.loadUnitLoader = new EggModuleLoader(this.moduleReferences, {
286
- logger: (this.innerObjects.logger && this.innerObjects.logger[0])?.obj || console,
287
- baseDir: this.cwd,
288
- dump: this.options?.dump
289
- });
290
- await this.loadUnitLoader.init();
291
- GlobalGraph.instance.registerBuildHook(crossCutGraphHook);
292
- GlobalGraph.instance.registerBuildHook(pointCutGraphHook);
293
- const configSourceEggPrototypeHook = new ConfigSourceLoadUnitHook();
294
- LoadUnitLifecycleUtil.registerLifecycle(configSourceEggPrototypeHook);
295
- this.crosscutAdviceFactory = new CrosscutAdviceFactory();
296
- this.loadUnitAopHook = new LoadUnitAopHook(this.crosscutAdviceFactory);
297
- this.eggPrototypeCrossCutHook = new EggPrototypeCrossCutHook(this.crosscutAdviceFactory);
298
- this.eggObjectAopHook = new EggObjectAopHook();
299
- EggPrototypeLifecycleUtil.registerLifecycle(this.eggPrototypeCrossCutHook);
300
- LoadUnitLifecycleUtil.registerLifecycle(this.loadUnitAopHook);
301
- EggObjectLifecycleUtil.registerLifecycle(this.eggObjectAopHook);
302
- this.loadUnitMultiInstanceProtoHook = new LoadUnitMultiInstanceProtoHook();
303
- LoadUnitLifecycleUtil.registerLifecycle(this.loadUnitMultiInstanceProtoHook);
304
- const logger = (this.innerObjects.logger && this.innerObjects.logger[0])?.obj || console;
305
- this.dalModuleLoadUnitHook = new DalModuleLoadUnitHook(this.env ?? "", this.moduleConfigs, logger);
306
- this.dalTableEggPrototypeHook = new DalTableEggPrototypeHook(logger);
307
- this.transactionPrototypeHook = new TransactionPrototypeHook(this.moduleConfigs, logger);
308
- EggPrototypeLifecycleUtil.registerLifecycle(this.dalTableEggPrototypeHook);
309
- EggPrototypeLifecycleUtil.registerLifecycle(this.transactionPrototypeHook);
310
- LoadUnitLifecycleUtil.registerLifecycle(this.dalModuleLoadUnitHook);
311
- }
312
- async init() {
313
- await this.initLoaderInstance();
314
- this.loadUnits = await this.load();
315
- const instances = [];
316
- for (const loadUnit of this.loadUnits) {
317
- const instance = await LoadUnitInstanceFactory.createLoadUnitInstance(loadUnit);
318
- instances.push(instance);
319
- }
320
- this.loadUnitInstances = instances;
321
- const runnerClass = StandaloneUtil.getMainRunner();
322
- if (!runnerClass) throw new Error("not found runner class. Do you add @Runner decorator?");
323
- const proto = PrototypeUtil.getClazzProto(runnerClass);
324
- if (!proto) throw new Error(`can not get proto for clazz ${runnerClass.name}`);
325
- this.runnerProto = proto;
326
- }
327
- async run(aCtx) {
328
- const lifecycle = {};
329
- const ctx = aCtx || new StandaloneContext();
330
- return await ContextHandler.run(ctx, async () => {
331
- if (ctx.init) await ctx.init(lifecycle);
332
- const runner = (await EggContainerFactory.getOrCreateEggObject(this.runnerProto)).obj;
333
- try {
334
- return await runner.main();
335
- } finally {
336
- if (ctx.destroy) ctx.destroy(lifecycle).catch((e) => {
337
- e.message = `[tegg/standalone] destroy tegg context failed: ${e.message}`;
338
- console.warn(e);
339
- });
340
- }
341
- });
342
- }
343
- async destroy() {
344
- if (this.loadUnitInstances) for (const instance of this.loadUnitInstances) await LoadUnitInstanceFactory.destroyLoadUnitInstance(instance);
345
- if (this.loadUnits) for (const loadUnit of this.loadUnits) await LoadUnitFactory.destroyLoadUnit(loadUnit);
346
- if (this.configSourceEggPrototypeHook) LoadUnitLifecycleUtil.deleteLifecycle(this.configSourceEggPrototypeHook);
347
- if (this.eggPrototypeCrossCutHook) EggPrototypeLifecycleUtil.deleteLifecycle(this.eggPrototypeCrossCutHook);
348
- if (this.loadUnitAopHook) LoadUnitLifecycleUtil.deleteLifecycle(this.loadUnitAopHook);
349
- if (this.eggObjectAopHook) EggObjectLifecycleUtil.deleteLifecycle(this.eggObjectAopHook);
350
- if (this.loadUnitMultiInstanceProtoHook) LoadUnitLifecycleUtil.deleteLifecycle(this.loadUnitMultiInstanceProtoHook);
351
- if (this.dalTableEggPrototypeHook) EggPrototypeLifecycleUtil.deleteLifecycle(this.dalTableEggPrototypeHook);
352
- if (this.dalModuleLoadUnitHook) LoadUnitLifecycleUtil.deleteLifecycle(this.dalModuleLoadUnitHook);
353
- if (this.transactionPrototypeHook) EggPrototypeLifecycleUtil.deleteLifecycle(this.transactionPrototypeHook);
354
- MysqlDataSourceManager.instance.clear();
355
- SqlMapManager.instance.clear();
356
- TableModelManager.instance.clear();
357
- ModuleConfigUtil.setConfigNames(void 0);
358
- }
359
- };
360
-
361
- //#endregion
362
- //#region src/main.ts
363
- async function preLoad(cwd, dependencies) {
364
- try {
365
- await Runner.preLoad(cwd, dependencies);
366
- } catch (e) {
367
- if (e instanceof Error) e.message = `[tegg/standalone] bootstrap standalone preLoad failed: ${e.message}`;
368
- throw e;
369
- }
370
- }
371
- async function main(cwd, options) {
372
- const runner = new Runner(cwd, options);
373
- try {
374
- await runner.init();
375
- } catch (e) {
376
- if (e instanceof Error) e.message = `[tegg/standalone] bootstrap tegg failed: ${e.message}`;
377
- throw e;
378
- }
379
- try {
380
- return await runner.run();
381
- } finally {
382
- runner.destroy().catch((e) => {
383
- e.message = `[tegg/standalone] destroy tegg failed: ${e.message}`;
384
- console.warn(e);
385
- });
386
- }
387
- }
388
-
389
- //#endregion
390
- //#region src/StandaloneInnerObject.ts
391
- const OBJ = Symbol("EggCompatibleObject#obj");
392
- var StandaloneInnerObject = class StandaloneInnerObject {
393
- isReady = true;
394
- [OBJ];
395
- proto;
396
- name;
397
- id;
398
- constructor(name, proto) {
399
- this.proto = proto;
400
- this.name = name;
401
- this.id = IdenticalUtil.createObjectId(this.proto.id);
402
- }
403
- get obj() {
404
- if (!this[OBJ]) this[OBJ] = this.proto.constructEggObject();
405
- return this[OBJ];
406
- }
407
- injectProperty() {}
408
- static async createObject(name, proto) {
409
- return new StandaloneInnerObject(name, proto);
410
- }
411
- };
412
- EggObjectFactory.registerEggObjectCreateMethod(StandaloneInnerObjectProto, StandaloneInnerObject.createObject);
413
-
414
- //#endregion
415
- export { EggModuleLoader, Runner, StandaloneContext, StandaloneInnerObject, StandaloneInnerObjectProto, main, preLoad };
1
+ export * from "./EggModuleLoader.js";
2
+ export * from "./Runner.js";
3
+ export * from "./main.js";
4
+ export * from "./StandaloneInnerObjectProto.js";
5
+ export * from "./StandaloneContext.js";
6
+ export * from "./StandaloneInnerObject.js";
7
+ //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi9zcmMvaW5kZXgudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBQUEsY0FBYyxzQkFBc0IsQ0FBQztBQUNyQyxjQUFjLGFBQWEsQ0FBQztBQUM1QixjQUFjLFdBQVcsQ0FBQztBQUMxQixjQUFjLGlDQUFpQyxDQUFDO0FBQ2hELGNBQWMsd0JBQXdCLENBQUM7QUFDdkMsY0FBYyw0QkFBNEIsQ0FBQyJ9
package/dist/main.d.ts ADDED
@@ -0,0 +1,3 @@
1
+ import { type RunnerOptions } from './Runner.ts';
2
+ export declare function preLoad(cwd: string, dependencies?: RunnerOptions['dependencies']): Promise<void>;
3
+ export declare function main<T = void>(cwd: string, options?: RunnerOptions): Promise<T>;
package/dist/main.js ADDED
@@ -0,0 +1,34 @@
1
+ import { Runner } from "./Runner.js";
2
+ export async function preLoad(cwd, dependencies) {
3
+ try {
4
+ await Runner.preLoad(cwd, dependencies);
5
+ }
6
+ catch (e) {
7
+ if (e instanceof Error) {
8
+ e.message = `[tegg/standalone] bootstrap standalone preLoad failed: ${e.message}`;
9
+ }
10
+ throw e;
11
+ }
12
+ }
13
+ export async function main(cwd, options) {
14
+ const runner = new Runner(cwd, options);
15
+ try {
16
+ await runner.init();
17
+ }
18
+ catch (e) {
19
+ if (e instanceof Error) {
20
+ e.message = `[tegg/standalone] bootstrap tegg failed: ${e.message}`;
21
+ }
22
+ throw e;
23
+ }
24
+ try {
25
+ return await runner.run();
26
+ }
27
+ finally {
28
+ runner.destroy().catch(e => {
29
+ e.message = `[tegg/standalone] destroy tegg failed: ${e.message}`;
30
+ console.warn(e);
31
+ });
32
+ }
33
+ }
34
+ //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoibWFpbi5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uL3NyYy9tYWluLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUFBLE9BQU8sRUFBRSxNQUFNLEVBQXNCLE1BQU0sYUFBYSxDQUFDO0FBRXpELE1BQU0sQ0FBQyxLQUFLLFVBQVUsT0FBTyxDQUFDLEdBQVcsRUFBRSxZQUE0QztJQUNyRixJQUFJLENBQUM7UUFDSCxNQUFNLE1BQU0sQ0FBQyxPQUFPLENBQUMsR0FBRyxFQUFFLFlBQVksQ0FBQyxDQUFDO0lBQzFDLENBQUM7SUFBQyxPQUFPLENBQUMsRUFBRSxDQUFDO1FBQ1gsSUFBSSxDQUFDLFlBQVksS0FBSyxFQUFFLENBQUM7WUFDdkIsQ0FBQyxDQUFDLE9BQU8sR0FBRywwREFBMEQsQ0FBQyxDQUFDLE9BQU8sRUFBRSxDQUFDO1FBQ3BGLENBQUM7UUFDRCxNQUFNLENBQUMsQ0FBQztJQUNWLENBQUM7QUFDSCxDQUFDO0FBRUQsTUFBTSxDQUFDLEtBQUssVUFBVSxJQUFJLENBQVcsR0FBVyxFQUFFLE9BQXVCO0lBQ3ZFLE1BQU0sTUFBTSxHQUFHLElBQUksTUFBTSxDQUFDLEdBQUcsRUFBRSxPQUFPLENBQUMsQ0FBQztJQUN4QyxJQUFJLENBQUM7UUFDSCxNQUFNLE1BQU0sQ0FBQyxJQUFJLEVBQUUsQ0FBQztJQUN0QixDQUFDO0lBQUMsT0FBTyxDQUFDLEVBQUUsQ0FBQztRQUNYLElBQUksQ0FBQyxZQUFZLEtBQUssRUFBRSxDQUFD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