@xrystal/core 3.13.3 → 3.13.5
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
|
@@ -9,16 +9,25 @@ export class X {
|
|
|
9
9
|
});
|
|
10
10
|
}
|
|
11
11
|
load(patterns, verbose = false) {
|
|
12
|
+
if (!patterns || (Array.isArray(patterns) && patterns.length === 0))
|
|
13
|
+
return this;
|
|
12
14
|
const cwd = process.cwd();
|
|
13
|
-
const resolvedPatterns = (Array.isArray(patterns) ? patterns : [patterns])
|
|
15
|
+
const resolvedPatterns = (Array.isArray(patterns) ? patterns : [patterns])
|
|
16
|
+
.filter(p => typeof p === 'string')
|
|
17
|
+
.map(p => {
|
|
14
18
|
const resolved = path.isAbsolute(p) ? p : path.resolve(cwd, p);
|
|
15
19
|
return resolved.replace(/\\/g, '/');
|
|
16
20
|
});
|
|
17
|
-
|
|
21
|
+
let modules = [];
|
|
22
|
+
try {
|
|
23
|
+
modules = listModules(resolvedPatterns);
|
|
24
|
+
}
|
|
25
|
+
catch (err) {
|
|
26
|
+
console.error(`[DI] Critical: Path resolution failed: ${err.message}`);
|
|
27
|
+
return this;
|
|
28
|
+
}
|
|
18
29
|
for (const m of modules) {
|
|
19
|
-
if (m.path.endsWith('.d.ts') ||
|
|
20
|
-
m.path.endsWith('.map') ||
|
|
21
|
-
m.path === __filename)
|
|
30
|
+
if (m.path === __filename || m.path.endsWith('.d.ts') || m.path.endsWith('.map'))
|
|
22
31
|
continue;
|
|
23
32
|
try {
|
|
24
33
|
const loaded = require(m.path);
|
|
@@ -30,76 +39,91 @@ export class X {
|
|
|
30
39
|
if (isClass) {
|
|
31
40
|
const className = dependency.name;
|
|
32
41
|
const name = className.charAt(0).toLowerCase() + className.slice(1);
|
|
33
|
-
if (!this.
|
|
34
|
-
this.container.register({
|
|
35
|
-
|
|
36
|
-
|
|
42
|
+
if (!this.isRegistered(name)) {
|
|
43
|
+
this.container.register({ [name]: asClass(dependency).singleton() });
|
|
44
|
+
if (verbose)
|
|
45
|
+
console.log(`[DI] Registered: ${name}`);
|
|
37
46
|
}
|
|
38
47
|
}
|
|
39
48
|
}
|
|
40
49
|
catch (err) {
|
|
41
|
-
|
|
50
|
+
console.error(`[DI] Critical: Failed to load module at ${m.path}:`, err.message);
|
|
42
51
|
}
|
|
43
52
|
}
|
|
44
|
-
if (verbose)
|
|
45
|
-
console.log('[DI] Registered Keys:', Object.keys(this.container.registrations));
|
|
46
53
|
return this;
|
|
47
54
|
}
|
|
48
55
|
register(Dependency) {
|
|
56
|
+
if (!Dependency?.name)
|
|
57
|
+
return this;
|
|
49
58
|
const name = Dependency.name.charAt(0).toLowerCase() + Dependency.name.slice(1);
|
|
50
59
|
if (this.isRegistered(name))
|
|
51
60
|
return this;
|
|
52
|
-
this.container.register({
|
|
53
|
-
[name]: asClass(Dependency).singleton()
|
|
54
|
-
});
|
|
61
|
+
this.container.register({ [name]: asClass(Dependency).singleton() });
|
|
55
62
|
return this;
|
|
56
63
|
}
|
|
57
64
|
registerAll(dependencies) {
|
|
65
|
+
if (!Array.isArray(dependencies))
|
|
66
|
+
return this;
|
|
58
67
|
dependencies.forEach(dep => this.register(dep));
|
|
59
68
|
return this;
|
|
60
69
|
}
|
|
61
70
|
registerInstance(name, instance) {
|
|
62
|
-
if (this.isRegistered(name))
|
|
71
|
+
if (!name || this.isRegistered(name))
|
|
63
72
|
return this;
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
});
|
|
73
|
+
const formattedName = name.charAt(0).toLowerCase() + name.slice(1);
|
|
74
|
+
this.container.register({ [formattedName]: asValue(instance) });
|
|
67
75
|
return this;
|
|
68
76
|
}
|
|
69
|
-
async initialize(input, verbose =
|
|
77
|
+
async initialize(input, verbose = false) {
|
|
70
78
|
const cradle = this.container.cradle;
|
|
71
79
|
const inputList = input ? (Array.isArray(input) ? input : [input]) : [];
|
|
72
|
-
const
|
|
73
|
-
const
|
|
74
|
-
|
|
80
|
+
const initializedNames = new Set();
|
|
81
|
+
for (const item of inputList) {
|
|
82
|
+
if (!item?.service)
|
|
83
|
+
continue;
|
|
75
84
|
const name = typeof item.service === 'function'
|
|
76
85
|
? item.service.name.charAt(0).toLowerCase() + item.service.name.slice(1)
|
|
77
86
|
: item.service;
|
|
78
|
-
|
|
79
|
-
|
|
87
|
+
const instance = cradle[name];
|
|
88
|
+
if (instance && typeof instance.load === 'function') {
|
|
89
|
+
try {
|
|
90
|
+
await instance.load(item.props || {});
|
|
91
|
+
initializedNames.add(name);
|
|
92
|
+
if (verbose)
|
|
93
|
+
console.log(`[DI] Initialized (Priority): ${name}`);
|
|
94
|
+
}
|
|
95
|
+
catch (err) {
|
|
96
|
+
console.error(`[DI] Critical: Priority service "${name}" failed:`, err.message);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
}
|
|
80
100
|
for (const key of Object.keys(this.container.registrations)) {
|
|
101
|
+
if (initializedNames.has(key))
|
|
102
|
+
continue;
|
|
81
103
|
const instance = cradle[key];
|
|
82
104
|
if (instance && typeof instance.load === 'function') {
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
105
|
+
try {
|
|
106
|
+
await instance.load({});
|
|
107
|
+
initializedNames.add(key);
|
|
108
|
+
if (verbose)
|
|
109
|
+
console.log(`[DI] Initialized (Auto): ${key}`);
|
|
110
|
+
}
|
|
111
|
+
catch (err) {
|
|
112
|
+
console.error(`[DI] Critical: Auto service "${key}" failed:`, err.message);
|
|
113
|
+
}
|
|
86
114
|
}
|
|
87
115
|
}
|
|
88
|
-
if (verbose && initializedNames.length > 0)
|
|
89
|
-
console.log('[DI] Initialized Services:', initializedNames);
|
|
90
116
|
return this;
|
|
91
117
|
}
|
|
92
118
|
get(target) {
|
|
119
|
+
if (!target)
|
|
120
|
+
throw new Error('[DI] Target is required');
|
|
93
121
|
const resolveName = typeof target === 'function'
|
|
94
122
|
? target.name.charAt(0).toLowerCase() + target.name.slice(1)
|
|
95
123
|
: target;
|
|
96
124
|
return this.container.resolve(resolveName);
|
|
97
125
|
}
|
|
98
|
-
get cradle() {
|
|
99
|
-
|
|
100
|
-
}
|
|
101
|
-
isRegistered(name) {
|
|
102
|
-
return !!this.container.registrations[name];
|
|
103
|
-
}
|
|
126
|
+
get cradle() { return this.container.cradle; }
|
|
127
|
+
isRegistered(name) { return !!this.container.registrations[name]; }
|
|
104
128
|
}
|
|
105
129
|
export default new X();
|