@xrystal/core 3.14.3 ā 3.14.7
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/package.json
CHANGED
package/source/project/index.js
CHANGED
|
@@ -16,14 +16,11 @@ const coreLoader = async ({}) => {
|
|
|
16
16
|
try {
|
|
17
17
|
const { configs } = core._;
|
|
18
18
|
const rootFolderPath = configs.rootFolderPath;
|
|
19
|
-
x
|
|
20
|
-
.load([
|
|
19
|
+
await (await x.load([
|
|
21
20
|
path.join(__dirname, '..', 'loader', '**/*.{ts,js}'),
|
|
22
21
|
], {
|
|
23
|
-
exclude: [
|
|
24
|
-
|
|
25
|
-
]
|
|
26
|
-
})
|
|
22
|
+
exclude: [path.join(__dirname, '..', 'utils', '**/class.x.{ts,js}')]
|
|
23
|
+
}))
|
|
27
24
|
.initialize([
|
|
28
25
|
{
|
|
29
26
|
service: SystemService,
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { LifetimeType } from 'awilix';
|
|
2
2
|
export declare class X {
|
|
3
3
|
private container;
|
|
4
4
|
private initializedNames;
|
|
@@ -8,7 +8,7 @@ export declare class X {
|
|
|
8
8
|
verbose?: boolean;
|
|
9
9
|
exclude?: string | Function | (string | Function)[];
|
|
10
10
|
lifetime?: LifetimeType;
|
|
11
|
-
}): this
|
|
11
|
+
}): Promise<this>;
|
|
12
12
|
register(Dependency: any, lifetime?: LifetimeType): this;
|
|
13
13
|
registerAll(dependencies: any[], lifetime?: LifetimeType): this;
|
|
14
14
|
registerInstance(name: string, instance: any): this;
|
|
@@ -20,10 +20,8 @@ export declare class X {
|
|
|
20
20
|
props?: any;
|
|
21
21
|
}[], verbose?: boolean): Promise<this>;
|
|
22
22
|
get<T>(target: string | any): T;
|
|
23
|
-
createScope(): AwilixContainer<any>;
|
|
24
23
|
get cradle(): any;
|
|
25
24
|
private isRegistered;
|
|
26
|
-
private getSourceByInstance;
|
|
27
25
|
}
|
|
28
26
|
declare const _default: X;
|
|
29
27
|
export default _default;
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { createContainer, asClass, asValue, InjectionMode, listModules, Lifetime } from 'awilix';
|
|
2
2
|
import path from 'node:path';
|
|
3
|
+
import { pathToFileURL } from 'node:url';
|
|
3
4
|
export class X {
|
|
4
5
|
container;
|
|
5
6
|
initializedNames = new Set();
|
|
@@ -14,7 +15,7 @@ export class X {
|
|
|
14
15
|
const normalizedPath = filePath.replace(/\\/g, '/');
|
|
15
16
|
return normalizedPath.includes('node_modules') || !normalizedPath.startsWith(projectRoot) ? 'LIB' : 'APP';
|
|
16
17
|
}
|
|
17
|
-
load(patterns, options = {}) {
|
|
18
|
+
async load(patterns, options = {}) {
|
|
18
19
|
const { verbose = false, exclude = [], lifetime = Lifetime.SINGLETON } = options;
|
|
19
20
|
const cwd = process.cwd();
|
|
20
21
|
const excludeList = Array.isArray(exclude) ? exclude : [exclude];
|
|
@@ -32,26 +33,31 @@ export class X {
|
|
|
32
33
|
}
|
|
33
34
|
for (const m of modules) {
|
|
34
35
|
const source = this.getSource(m.path);
|
|
35
|
-
|
|
36
|
+
const normalizedMPath = m.path.replace(/\\/g, '/');
|
|
37
|
+
if (normalizedMPath === __filename.replace(/\\/g, '/') || normalizedMPath.endsWith('.d.ts'))
|
|
36
38
|
continue;
|
|
39
|
+
const isPathExcluded = excludeList.some(ex => {
|
|
40
|
+
if (typeof ex === 'string') {
|
|
41
|
+
const normalizedEx = ex.replace(/\\/g, '/');
|
|
42
|
+
return normalizedMPath.includes(normalizedEx) || m.name === ex;
|
|
43
|
+
}
|
|
44
|
+
return false;
|
|
45
|
+
});
|
|
46
|
+
if (isPathExcluded) {
|
|
47
|
+
if (verbose)
|
|
48
|
+
console.log(`[DI][${source}] Excluded: ${m.name}`);
|
|
49
|
+
continue;
|
|
50
|
+
}
|
|
37
51
|
try {
|
|
38
|
-
const
|
|
52
|
+
const fileUrl = pathToFileURL(m.path).href;
|
|
53
|
+
const loaded = await import(fileUrl);
|
|
39
54
|
let dependency = loaded.default;
|
|
40
55
|
if (!dependency) {
|
|
41
56
|
dependency = Object.values(loaded).find(val => typeof val === 'function' && !!val.prototype && !!val.name);
|
|
42
57
|
}
|
|
43
|
-
const
|
|
44
|
-
|
|
45
|
-
return m.path.includes(ex) || m.name === ex;
|
|
46
|
-
if (typeof ex === 'function')
|
|
47
|
-
return dependency === ex;
|
|
48
|
-
return false;
|
|
49
|
-
});
|
|
50
|
-
if (isExcluded) {
|
|
51
|
-
if (verbose)
|
|
52
|
-
console.log(`[DI][${source}] Excluded: ${m.name}`);
|
|
58
|
+
const isClassExcluded = excludeList.some(ex => typeof ex === 'function' && dependency === ex);
|
|
59
|
+
if (isClassExcluded)
|
|
53
60
|
continue;
|
|
54
|
-
}
|
|
55
61
|
const isClass = typeof dependency === 'function' && !!dependency.prototype && !!dependency.name;
|
|
56
62
|
if (isClass) {
|
|
57
63
|
const className = dependency.name;
|
|
@@ -61,12 +67,13 @@ export class X {
|
|
|
61
67
|
[name]: asClass(dependency).setLifetime(lifetime)
|
|
62
68
|
});
|
|
63
69
|
if (verbose)
|
|
64
|
-
console.log(`[DI][${source}] Registered
|
|
70
|
+
console.log(`[DI][${source}] Registered: ${name}`);
|
|
65
71
|
}
|
|
66
72
|
}
|
|
67
73
|
}
|
|
68
74
|
catch (err) {
|
|
69
|
-
|
|
75
|
+
if (verbose)
|
|
76
|
+
console.error(`[DI][${source}] Load Error in ${m.name}:`, err.message);
|
|
70
77
|
}
|
|
71
78
|
}
|
|
72
79
|
return this;
|
|
@@ -77,9 +84,7 @@ export class X {
|
|
|
77
84
|
const name = Dependency.name.charAt(0).toLowerCase() + Dependency.name.slice(1);
|
|
78
85
|
if (this.isRegistered(name))
|
|
79
86
|
return this;
|
|
80
|
-
this.container.register({
|
|
81
|
-
[name]: asClass(Dependency).setLifetime(lifetime)
|
|
82
|
-
});
|
|
87
|
+
this.container.register({ [name]: asClass(Dependency).setLifetime(lifetime) });
|
|
83
88
|
return this;
|
|
84
89
|
}
|
|
85
90
|
registerAll(dependencies, lifetime = Lifetime.SINGLETON) {
|
|
@@ -117,16 +122,15 @@ export class X {
|
|
|
117
122
|
continue;
|
|
118
123
|
const instance = cradle[key];
|
|
119
124
|
if (instance && typeof instance.load === 'function') {
|
|
120
|
-
const source = this.getSourceByInstance(instance);
|
|
121
125
|
try {
|
|
122
126
|
const props = propsMap.get(key) || {};
|
|
123
127
|
await instance.load(props);
|
|
124
128
|
this.initializedNames.add(key);
|
|
125
129
|
if (verbose)
|
|
126
|
-
console.log(`[DI]
|
|
130
|
+
console.log(`[DI] Initialized: ${key}`);
|
|
127
131
|
}
|
|
128
132
|
catch (err) {
|
|
129
|
-
console.error(`[DI]
|
|
133
|
+
console.error(`[DI] Initialization Failed: ${key} ->`, err.message);
|
|
130
134
|
}
|
|
131
135
|
}
|
|
132
136
|
}
|
|
@@ -134,28 +138,19 @@ export class X {
|
|
|
134
138
|
}
|
|
135
139
|
get(target) {
|
|
136
140
|
try {
|
|
137
|
-
const resolveName = typeof target === 'function'
|
|
138
|
-
? target.name.charAt(0).toLowerCase() + target.name.slice(1)
|
|
139
|
-
: target;
|
|
141
|
+
const resolveName = typeof target === 'function' ? target.name.charAt(0).toLowerCase() + target.name.slice(1) : target;
|
|
140
142
|
return this.container.resolve(resolveName);
|
|
141
143
|
}
|
|
142
144
|
catch (err) {
|
|
143
145
|
if (err.message.includes('Cyclic dependencies')) {
|
|
144
|
-
console.error(
|
|
145
|
-
console.error(`š Resolution Path: ${err.resolutionStack}`);
|
|
146
|
+
console.error(`\nā [DI][CRITICAL] Cyclic dependency detected!\nš Path: ${err.resolutionStack}`);
|
|
146
147
|
}
|
|
147
148
|
throw err;
|
|
148
149
|
}
|
|
149
150
|
}
|
|
150
|
-
createScope() {
|
|
151
|
-
return this.container.createScope();
|
|
152
|
-
}
|
|
153
151
|
get cradle() { return this.container.cradle; }
|
|
154
152
|
isRegistered(name) {
|
|
155
153
|
return !!this.container.registrations[name] && this.container.registrations[name].resolve !== undefined;
|
|
156
154
|
}
|
|
157
|
-
getSourceByInstance(instance) {
|
|
158
|
-
return instance.constructor?.name?.includes('Service') || instance.constructor?.name?.includes('Controller') ? 'APP' : 'LIB';
|
|
159
|
-
}
|
|
160
155
|
}
|
|
161
156
|
export default new X();
|