@koalarx/nest 4.0.2 → 4.0.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.
@@ -1,61 +1,81 @@
1
- export function patchInfraModuleForCache(content) {
2
- if (content.includes("CacheServiceProvider")) {
3
- return content;
4
- }
5
- const withAuth = content.includes("ILoggedUserInfoService");
6
- let patched = content.replace("import { ILoggingService } from '@/domain/common/ilogging.service';", `import { ICacheService } from '@/domain/common/icache.service';
7
- import { ILoggingService } from '@/domain/common/ilogging.service';
8
- import { IRedLockService } from '@/domain/common/ired-lock.service';`).replace("import { LoggingService } from '@/infra/common/logging.service';", `import { CacheServiceProvider } from '@/infra/common/cache-service.provider';
9
- import { LoggingService } from '@/infra/common/logging.service';
10
- import { RedLockService } from '@/infra/common/red-lock.service';`).replace(` providers: [
11
- { provide: ILoggingService, useClass: LoggingService },`, ` providers: [
12
- CacheServiceProvider,
13
- { provide: ICacheService, useExisting: CacheServiceProvider },
14
- { provide: ILoggingService, useClass: LoggingService },
15
- { provide: IRedLockService, useClass: RedLockService },`);
16
- if (withAuth) {
17
- return patched.replace(" exports: [RepositoryModule, ILoggingService, ILoggedUserInfoService],", ` exports: [
18
- RepositoryModule,
19
- ICacheService,
20
- ILoggingService,
21
- IRedLockService,
22
- ILoggedUserInfoService,
23
- ],`);
24
- }
25
- return patched.replace(" exports: [RepositoryModule, ILoggingService],", ` exports: [
26
- RepositoryModule,
27
- ICacheService,
28
- ILoggingService,
29
- IRedLockService,
30
- ],`);
1
+ function hasCacheProviders(content) {
2
+ return content.includes("{ provide: ICacheService, useExisting: CacheServiceProvider }");
3
+ }
4
+ function hasAuthProviders(content) {
5
+ return content.includes("{ provide: ILoggedUserInfoService, useClass: LoggedUserInfoService }");
31
6
  }
32
- export const SLIM_INFRA_MODULE = `import { ILoggingService } from '@/domain/common/ilogging.service';
33
- import { Module } from '@nestjs/common';
34
- import { LoggingService } from '@/infra/common/logging.service';
35
- import { RepositoryModule } from '@/infra/repositories/repository.module';
7
+ export function buildInfraModule({ cache, auth }) {
8
+ const importLines = [
9
+ ...cache ? ["import { ICacheService } from '@/domain/common/icache.service';"] : [],
10
+ "import { ILoggingService } from '@/domain/common/ilogging.service';",
11
+ ...auth ? [
12
+ "import { ILoggedUserInfoService } from '@/domain/services/ilogged-user-info.service';"
13
+ ] : [],
14
+ ...cache ? ["import { IRedLockService } from '@/domain/common/ired-lock.service';"] : [],
15
+ "import { Module } from '@nestjs/common';",
16
+ ...cache ? [
17
+ "import { CacheServiceProvider } from '@/infra/common/cache-service.provider';"
18
+ ] : [],
19
+ "import { LoggingService } from '@/infra/common/logging.service';",
20
+ ...auth ? [
21
+ "import { LoggedUserInfoService } from '@/infra/services/logged-user-info.service';"
22
+ ] : [],
23
+ ...cache ? ["import { RedLockService } from '@/infra/common/red-lock.service';"] : [],
24
+ "import { RepositoryModule } from '@/infra/repositories/repository.module';"
25
+ ];
26
+ const providerLines = [
27
+ ...cache ? [
28
+ " CacheServiceProvider,",
29
+ " { provide: ICacheService, useExisting: CacheServiceProvider },"
30
+ ] : [],
31
+ " { provide: ILoggingService, useClass: LoggingService },",
32
+ ...cache ? [" { provide: IRedLockService, useClass: RedLockService },"] : [],
33
+ ...auth ? [
34
+ " { provide: ILoggedUserInfoService, useClass: LoggedUserInfoService },"
35
+ ] : []
36
+ ];
37
+ const exportLines = [
38
+ " RepositoryModule,",
39
+ ...cache ? [" ICacheService,"] : [],
40
+ " ILoggingService,",
41
+ ...cache ? [" IRedLockService,"] : [],
42
+ ...auth ? [" ILoggedUserInfoService,"] : []
43
+ ];
44
+ return `${importLines.join(`
45
+ `)}
36
46
 
37
47
  @Module({
38
48
  imports: [RepositoryModule],
39
- providers: [{ provide: ILoggingService, useClass: LoggingService }],
40
- exports: [RepositoryModule, ILoggingService],
49
+ providers: [
50
+ ${providerLines.join(`
51
+ `)}
52
+ ],
53
+ exports: [
54
+ ${exportLines.join(`
55
+ `)}
56
+ ],
41
57
  })
42
58
  export class InfraModule {}
43
59
  `;
44
- export function patchInfraModuleForAuth(content) {
45
- if (content.includes("ILoggedUserInfoService")) {
60
+ }
61
+ export function patchInfraModuleForCache(content) {
62
+ if (hasCacheProviders(content)) {
46
63
  return content;
47
64
  }
48
- let patched = content.replace("import { ILoggingService } from '@/domain/common/ilogging.service';", `import { ILoggingService } from '@/domain/common/ilogging.service';
49
- import { ILoggedUserInfoService } from '@/domain/services/ilogged-user-info.service';`).replace("import { LoggingService } from '@/infra/common/logging.service';", `import { LoggingService } from '@/infra/common/logging.service';
50
- import { LoggedUserInfoService } from '@/infra/services/logged-user-info.service';`).replace(" { provide: ILoggingService, useClass: LoggingService },", ` { provide: ILoggingService, useClass: LoggingService },
51
- { provide: ILoggedUserInfoService, useClass: LoggedUserInfoService },`);
52
- if (patched.includes("IRedLockService,")) {
53
- return patched.replace(` IRedLockService,
54
- ],`, ` IRedLockService,
55
- ILoggedUserInfoService,
56
- ],`);
65
+ return buildInfraModule({
66
+ cache: true,
67
+ auth: hasAuthProviders(content) || content.includes("ILoggedUserInfoService")
68
+ });
69
+ }
70
+ export const SLIM_INFRA_MODULE = buildInfraModule({ cache: false, auth: false });
71
+ export function patchInfraModuleForAuth(content) {
72
+ if (hasAuthProviders(content)) {
73
+ return content;
57
74
  }
58
- return patched.replace(" exports: [RepositoryModule, ILoggingService],", " exports: [RepositoryModule, ILoggingService, ILoggedUserInfoService],");
75
+ return buildInfraModule({
76
+ cache: hasCacheProviders(content) || content.includes("ICacheService"),
77
+ auth: true
78
+ });
59
79
  }
60
80
  export function stripInfraModuleCache(_content) {
61
81
  return SLIM_INFRA_MODULE;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@koalarx/nest",
3
- "version": "4.0.2",
3
+ "version": "4.0.3",
4
4
  "description": "CLI para criar APIs NestJS com arquitetura DDD — copia módulos prontos para o seu repositório.",
5
5
  "license": "MIT",
6
6
  "type": "module",