@xrystal/core 3.12.2 → 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
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "author": "Yusuf Yasir KAYGUSUZ",
3
3
  "name": "@xrystal/core",
4
- "version": "3.12.2",
4
+ "version": "3.12.3",
5
5
  "description": "Project core for xrystal",
6
6
  "publishConfig": {
7
7
  "access": "public",
@@ -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
  };
@@ -11,6 +11,7 @@ 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 {
@@ -18,8 +19,7 @@ const coreLoader = async ({}) => {
18
19
  const rootFolderPath = configs.rootFolderPath;
19
20
  x
20
21
  .load([
21
- path.join(__dirname, 'source', 'loader', '**/*.{ts,js}'),
22
- path.join(__dirname, 'source', 'utils', '**/class.*.{ts,js}')
22
+ path.join(cwd, 'source', 'loader', '**/*.{ts,js}'),
23
23
  ])
24
24
  .initialize([
25
25
  {
@@ -1,7 +1,7 @@
1
1
  export declare class X {
2
2
  private container;
3
3
  constructor();
4
- load(patterns: string | string[]): this;
4
+ load(patterns: string | string[], verbose?: boolean): this;
5
5
  register(Dependency: any): this;
6
6
  registerAll(dependencies: any[]): this;
7
7
  registerInstance(name: string, instance: any): this;
@@ -14,6 +14,7 @@ export declare class X {
14
14
  }[]): Promise<this>;
15
15
  get<T>(target: string | any): T;
16
16
  get cradle(): any;
17
+ private isRegistered;
17
18
  }
18
19
  declare const _default: X;
19
20
  export default _default;
@@ -1,4 +1,5 @@
1
- import { createContainer, asClass, asValue, InjectionMode, Lifetime } 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,19 +8,45 @@ export class X {
7
8
  strict: true
8
9
  });
9
10
  }
10
- load(patterns) {
11
- const resolvedPatterns = Array.isArray(patterns) ? patterns : [patterns];
12
- this.container.loadModules(resolvedPatterns, {
13
- formatName: 'camelCase',
14
- resolverOptions: {
15
- lifetime: Lifetime.SINGLETON,
16
- register: asClass
17
- }
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, '/');
18
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));
19
44
  return this;
20
45
  }
21
46
  register(Dependency) {
22
47
  const name = Dependency.name.charAt(0).toLowerCase() + Dependency.name.slice(1);
48
+ if (this.isRegistered(name))
49
+ return this;
23
50
  this.container.register({
24
51
  [name]: asClass(Dependency).singleton()
25
52
  });
@@ -30,6 +57,8 @@ export class X {
30
57
  return this;
31
58
  }
32
59
  registerInstance(name, instance) {
60
+ if (this.isRegistered(name))
61
+ return this;
33
62
  this.container.register({
34
63
  [name]: asValue(instance)
35
64
  });
@@ -67,5 +96,8 @@ export class X {
67
96
  get cradle() {
68
97
  return this.container.cradle;
69
98
  }
99
+ isRegistered(name) {
100
+ return !!this.container.registrations[name];
101
+ }
70
102
  }
71
103
  export default new X();