@xrystal/core 3.12.1 → 3.12.3
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
|
@@ -20,6 +20,7 @@ export default class ConfigsService {
|
|
|
20
20
|
baseApiUri: process.env.HTTPS === 'true' ? process.env.SYSTEM_HTTPS_BASE_API_URI : process.env.SYSTEM_BASE_API_URI,
|
|
21
21
|
internalSecret: process.env.INTERNAL_SECRET,
|
|
22
22
|
secret: process.env.SECRET,
|
|
23
|
+
cwd: process.cwd(),
|
|
23
24
|
env: process.env.NODE_ENV,
|
|
24
25
|
...rawConfigs
|
|
25
26
|
};
|
package/source/project/index.js
CHANGED
|
@@ -11,21 +11,16 @@ export const coreInit = async (params) => {
|
|
|
11
11
|
globalThis.__LOCAL_MESSAGES__ = locales;
|
|
12
12
|
};
|
|
13
13
|
const coreLoader = async ({}) => {
|
|
14
|
+
const cwd = process.cwd();
|
|
14
15
|
if (coreHasRun)
|
|
15
16
|
return {};
|
|
16
17
|
try {
|
|
17
18
|
const { configs } = core._;
|
|
18
19
|
const rootFolderPath = configs.rootFolderPath;
|
|
19
|
-
const services = [
|
|
20
|
-
SystemService,
|
|
21
|
-
ConfigsService,
|
|
22
|
-
LoggerService,
|
|
23
|
-
EventsService,
|
|
24
|
-
LocalizationsService,
|
|
25
|
-
ClientsService
|
|
26
|
-
];
|
|
27
20
|
x
|
|
28
|
-
.
|
|
21
|
+
.load([
|
|
22
|
+
path.join(cwd, 'source', 'loader', '**/*.{ts,js}'),
|
|
23
|
+
])
|
|
29
24
|
.initialize([
|
|
30
25
|
{
|
|
31
26
|
service: SystemService,
|
|
@@ -1,18 +1,20 @@
|
|
|
1
1
|
export declare class X {
|
|
2
2
|
private container;
|
|
3
3
|
constructor();
|
|
4
|
-
|
|
5
|
-
|
|
4
|
+
load(patterns: string | string[], verbose?: boolean): this;
|
|
5
|
+
register(Dependency: any): this;
|
|
6
|
+
registerAll(dependencies: any[]): this;
|
|
6
7
|
registerInstance(name: string, instance: any): this;
|
|
7
|
-
initialize(input
|
|
8
|
+
initialize(input?: {
|
|
8
9
|
service: any;
|
|
9
10
|
props?: any;
|
|
10
11
|
} | {
|
|
11
12
|
service: any;
|
|
12
13
|
props?: any;
|
|
13
14
|
}[]): Promise<this>;
|
|
14
|
-
get<T>(
|
|
15
|
+
get<T>(target: string | any): T;
|
|
15
16
|
get cradle(): any;
|
|
17
|
+
private isRegistered;
|
|
16
18
|
}
|
|
17
19
|
declare const _default: X;
|
|
18
20
|
export default _default;
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import { createContainer, asClass, asValue, InjectionMode } from 'awilix';
|
|
1
|
+
import { createContainer, asClass, asValue, InjectionMode, listModules } from 'awilix';
|
|
2
|
+
import path from 'node:path';
|
|
2
3
|
export class X {
|
|
3
4
|
container;
|
|
4
5
|
constructor() {
|
|
@@ -7,43 +8,96 @@ export class X {
|
|
|
7
8
|
strict: true
|
|
8
9
|
});
|
|
9
10
|
}
|
|
10
|
-
|
|
11
|
-
const
|
|
12
|
-
|
|
13
|
-
|
|
11
|
+
load(patterns, verbose = false) {
|
|
12
|
+
const cwd = process.cwd();
|
|
13
|
+
const resolvedPatterns = (Array.isArray(patterns) ? patterns : [patterns]).map(p => {
|
|
14
|
+
const resolved = path.isAbsolute(p) ? p : path.join(cwd, p);
|
|
15
|
+
return resolved.replace(/\\/g, '/');
|
|
14
16
|
});
|
|
17
|
+
const modules = listModules(resolvedPatterns);
|
|
18
|
+
for (const m of modules) {
|
|
19
|
+
if (m.path === __filename || m.path.includes('.d.ts'))
|
|
20
|
+
continue;
|
|
21
|
+
try {
|
|
22
|
+
const loaded = require(m.path);
|
|
23
|
+
let dependency = loaded.default;
|
|
24
|
+
if (!dependency) {
|
|
25
|
+
dependency = Object.values(loaded).find(val => typeof val === 'function' && !!val.prototype && !!val.name);
|
|
26
|
+
}
|
|
27
|
+
const isClass = typeof dependency === 'function' && !!dependency.prototype && !!dependency.name;
|
|
28
|
+
if (isClass) {
|
|
29
|
+
const className = dependency.name;
|
|
30
|
+
const name = className.charAt(0).toLowerCase() + className.slice(1);
|
|
31
|
+
if (!this.container.registrations[name]) {
|
|
32
|
+
this.container.register({
|
|
33
|
+
[name]: asClass(dependency).singleton()
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
catch (err) {
|
|
39
|
+
continue;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
if (verbose)
|
|
43
|
+
console.log('[DI] Registered Keys:', Object.keys(this.container.registrations));
|
|
15
44
|
return this;
|
|
16
45
|
}
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
46
|
+
register(Dependency) {
|
|
47
|
+
const name = Dependency.name.charAt(0).toLowerCase() + Dependency.name.slice(1);
|
|
48
|
+
if (this.isRegistered(name))
|
|
49
|
+
return this;
|
|
50
|
+
this.container.register({
|
|
51
|
+
[name]: asClass(Dependency).singleton()
|
|
20
52
|
});
|
|
21
53
|
return this;
|
|
22
54
|
}
|
|
55
|
+
registerAll(dependencies) {
|
|
56
|
+
dependencies.forEach(dep => this.register(dep));
|
|
57
|
+
return this;
|
|
58
|
+
}
|
|
23
59
|
registerInstance(name, instance) {
|
|
60
|
+
if (this.isRegistered(name))
|
|
61
|
+
return this;
|
|
24
62
|
this.container.register({
|
|
25
63
|
[name]: asValue(instance)
|
|
26
64
|
});
|
|
27
65
|
return this;
|
|
28
66
|
}
|
|
29
67
|
async initialize(input) {
|
|
30
|
-
const
|
|
31
|
-
|
|
32
|
-
|
|
68
|
+
const cradle = this.container.cradle;
|
|
69
|
+
const inputList = input ? (Array.isArray(input) ? input : [input]) : [];
|
|
70
|
+
const propsMap = new Map();
|
|
71
|
+
inputList.forEach(item => {
|
|
72
|
+
const name = typeof item.service === 'function'
|
|
73
|
+
? item.service.name.charAt(0).toLowerCase() + item.service.name.slice(1)
|
|
74
|
+
: item.service;
|
|
75
|
+
propsMap.set(name, item.props);
|
|
76
|
+
});
|
|
77
|
+
for (const key of Object.keys(cradle)) {
|
|
78
|
+
const instance = cradle[key];
|
|
33
79
|
if (instance && typeof instance.load === 'function') {
|
|
34
|
-
|
|
80
|
+
if (propsMap.has(key)) {
|
|
81
|
+
await instance.load(propsMap.get(key));
|
|
82
|
+
}
|
|
83
|
+
else {
|
|
84
|
+
await instance.load();
|
|
85
|
+
}
|
|
35
86
|
}
|
|
36
87
|
}
|
|
37
88
|
return this;
|
|
38
89
|
}
|
|
39
|
-
get(
|
|
40
|
-
const resolveName = typeof
|
|
41
|
-
?
|
|
42
|
-
:
|
|
90
|
+
get(target) {
|
|
91
|
+
const resolveName = typeof target === 'function'
|
|
92
|
+
? target.name.charAt(0).toLowerCase() + target.name.slice(1)
|
|
93
|
+
: target;
|
|
43
94
|
return this.container.resolve(resolveName);
|
|
44
95
|
}
|
|
45
96
|
get cradle() {
|
|
46
97
|
return this.container.cradle;
|
|
47
98
|
}
|
|
99
|
+
isRegistered(name) {
|
|
100
|
+
return !!this.container.registrations[name];
|
|
101
|
+
}
|
|
48
102
|
}
|
|
49
103
|
export default new X();
|