From 54f20f04d57e6aca808656dbf8a47b34d0161504 Mon Sep 17 00:00:00 2001 From: "Guilherme Siquinelli (guiseek)" Date: Tue, 5 Oct 2021 12:26:47 -0300 Subject: [PATCH] feat(plugin-core): add injectable option in generators --- .../inmemory/__model__.inmemory.repository.ts__template__ | 6 ++++++ .../src/lib/repository/__model__.repository.ts__template__ | 6 ++++++ libs/plugin/core/src/generators/domain/schema.json | 5 +++++ .../presenter/__model__-default.presenter.ts__template__ | 6 ++++++ .../src/lib/presenter/__model__.presenter.ts__template__ | 6 ++++++ .../__model__.__format__.repository.ts__template__ | 6 ++++++ .../lib/repository/__model__.repository.ts__template__ | 6 ++++++ libs/plugin/core/src/generators/repository/schema.json | 5 +++++ libs/plugin/core/src/utils/normalize-options.ts | 3 ++- 9 files changed, 48 insertions(+), 1 deletion(-) diff --git a/libs/plugin/core/src/generators/data/files/inmemory/files/src/lib/inmemory/__model__.inmemory.repository.ts__template__ b/libs/plugin/core/src/generators/data/files/inmemory/files/src/lib/inmemory/__model__.inmemory.repository.ts__template__ index d4d19b7..53f50ad 100644 --- a/libs/plugin/core/src/generators/data/files/inmemory/files/src/lib/inmemory/__model__.inmemory.repository.ts__template__ +++ b/libs/plugin/core/src/generators/data/files/inmemory/files/src/lib/inmemory/__model__.inmemory.repository.ts__template__ @@ -3,7 +3,13 @@ import { <%= entity.className %>MockMapper } from './mapper/<%= entity.fileName import { <%= entity.className %>MockDto } from './dto/<%= entity.fileName %>-mock.dto'; import { Observable, of } from 'rxjs'; import { map } from 'rxjs/operators'; +<% if (injectable) { %> +import { Injectable } from '@nx-clean/core'; +<% } %> +<% if (injectable) { %> +@Injectable() +<% } %> export class <%= entity.className %>InMemoryRepository implements <%= entity.className %>Repository { constructor(private data: <%= entity.className %>MockDto[] = []) {} diff --git a/libs/plugin/core/src/generators/domain/files/repository/files/src/lib/repository/__model__.repository.ts__template__ b/libs/plugin/core/src/generators/domain/files/repository/files/src/lib/repository/__model__.repository.ts__template__ index 21930aa..8c94e3c 100644 --- a/libs/plugin/core/src/generators/domain/files/repository/files/src/lib/repository/__model__.repository.ts__template__ +++ b/libs/plugin/core/src/generators/domain/files/repository/files/src/lib/repository/__model__.repository.ts__template__ @@ -1,6 +1,12 @@ import { <%= entity.className %>Entity } from '../entity/<%= entity.fileName %>.entity'; import { Observable } from 'rxjs'; +<% if (injectable) { %> +import { Injectable } from '@nx-clean/core'; +<% } %> +<% if (injectable) { %> +@Injectable() +<% } %> export abstract class <%= entity.className %>Repository { public abstract getAll<%= entity.className %>s(): Observable<<%= entity.className %>Entity[]>; public abstract add<%= entity.className %>(<%= entity.propertyName %>: Pick<<%= entity.className %>Entity, 'name'>): Observable<<%= entity.className %>Entity>; diff --git a/libs/plugin/core/src/generators/domain/schema.json b/libs/plugin/core/src/generators/domain/schema.json index 2a4012e..27a7084 100644 --- a/libs/plugin/core/src/generators/domain/schema.json +++ b/libs/plugin/core/src/generators/domain/schema.json @@ -24,6 +24,11 @@ "description": "Would you like to add a repository?", "default": false }, + "injectable": { + "type": "boolean", + "description": "Would you like to use dependency injection?", + "default": true + }, "usecases": { "type": "boolean", "description": "Would you like to add a usecases?", diff --git a/libs/plugin/core/src/generators/presentation/files/src/lib/presenter/__model__-default.presenter.ts__template__ b/libs/plugin/core/src/generators/presentation/files/src/lib/presenter/__model__-default.presenter.ts__template__ index ca48996..5161a29 100644 --- a/libs/plugin/core/src/generators/presentation/files/src/lib/presenter/__model__-default.presenter.ts__template__ +++ b/libs/plugin/core/src/generators/presentation/files/src/lib/presenter/__model__-default.presenter.ts__template__ @@ -9,7 +9,13 @@ import { GetAll<%= entity.className %>sUseCase, Remove<%= entity.className %>UseCase, } from '<%= projectDomain %>'; +<% if (injectable) { %> +import { Injectable } from '@nx-clean/core'; +<% } %> +<% if (injectable) { %> +@Injectable() +<% } %> export class <%= entity.className %>DefaultPresenter implements <%= entity.className %>Presenter { <%= entity.propertyName %>s$: Observable<<%= entity.className %>VM[]>; filter$: Observable; diff --git a/libs/plugin/core/src/generators/presentation/files/src/lib/presenter/__model__.presenter.ts__template__ b/libs/plugin/core/src/generators/presentation/files/src/lib/presenter/__model__.presenter.ts__template__ index 0acd3c8..5f8c199 100644 --- a/libs/plugin/core/src/generators/presentation/files/src/lib/presenter/__model__.presenter.ts__template__ +++ b/libs/plugin/core/src/generators/presentation/files/src/lib/presenter/__model__.presenter.ts__template__ @@ -1,6 +1,12 @@ import { <%= entity.className %>VM } from '../viewmodel/<%= entity.fileName %>s.viewmodel'; import { Observable } from 'rxjs'; +<% if (injectable) { %> +import { Injectable } from '@nx-clean/core'; +<% } %> +<% if (injectable) { %> +@Injectable() +<% } %> export abstract class <%= entity.className %>Presenter { abstract <%= entity.propertyName %>s$: Observable<<%= entity.className %>VM[]>; abstract filter$: Observable; diff --git a/libs/plugin/core/src/generators/repository/files/data/files/lib/__format__/__model__.__format__.repository.ts__template__ b/libs/plugin/core/src/generators/repository/files/data/files/lib/__format__/__model__.__format__.repository.ts__template__ index 7d24ef4..01d11c2 100644 --- a/libs/plugin/core/src/generators/repository/files/data/files/lib/__format__/__model__.__format__.repository.ts__template__ +++ b/libs/plugin/core/src/generators/repository/files/data/files/lib/__format__/__model__.__format__.repository.ts__template__ @@ -3,7 +3,13 @@ import { <%= entity.className %><%= type.className %>Mapper } from './mapper/<%= import { <%= entity.className %><%= type.className %>Dto } from './dto/<%= entity.fileName %>-<%= type.fileName %>.dto'; import { Observable, of } from 'rxjs'; import { map } from 'rxjs/operators'; +<% if (injectable) { %> +import { Injectable } from '@nx-clean/core'; +<% } %> +<% if (injectable) { %> +@Injectable() +<% } %> export class <%= entity.className %><%= type.className %>Repository implements <%= entity.className %>Repository { constructor(private data: <%= entity.className %><%= type.className %>Dto[] = []) {} diff --git a/libs/plugin/core/src/generators/repository/files/domain/files/lib/repository/__model__.repository.ts__template__ b/libs/plugin/core/src/generators/repository/files/domain/files/lib/repository/__model__.repository.ts__template__ index 21930aa..8c94e3c 100644 --- a/libs/plugin/core/src/generators/repository/files/domain/files/lib/repository/__model__.repository.ts__template__ +++ b/libs/plugin/core/src/generators/repository/files/domain/files/lib/repository/__model__.repository.ts__template__ @@ -1,6 +1,12 @@ import { <%= entity.className %>Entity } from '../entity/<%= entity.fileName %>.entity'; import { Observable } from 'rxjs'; +<% if (injectable) { %> +import { Injectable } from '@nx-clean/core'; +<% } %> +<% if (injectable) { %> +@Injectable() +<% } %> export abstract class <%= entity.className %>Repository { public abstract getAll<%= entity.className %>s(): Observable<<%= entity.className %>Entity[]>; public abstract add<%= entity.className %>(<%= entity.propertyName %>: Pick<<%= entity.className %>Entity, 'name'>): Observable<<%= entity.className %>Entity>; diff --git a/libs/plugin/core/src/generators/repository/schema.json b/libs/plugin/core/src/generators/repository/schema.json index 7518862..95dcb8c 100644 --- a/libs/plugin/core/src/generators/repository/schema.json +++ b/libs/plugin/core/src/generators/repository/schema.json @@ -37,6 +37,11 @@ "$default": { "$source": "projectName" } + }, + "injectable": { + "type": "boolean", + "description": "Would you like to use dependency injection?", + "default": true } }, "required": ["name", "domain"] diff --git a/libs/plugin/core/src/utils/normalize-options.ts b/libs/plugin/core/src/utils/normalize-options.ts index 2f89809..0928dd4 100644 --- a/libs/plugin/core/src/utils/normalize-options.ts +++ b/libs/plugin/core/src/utils/normalize-options.ts @@ -54,10 +54,11 @@ export function normalizeOptions(host: Tree, options: any): unknown { ? options.tags.split(',').map((s) => s.trim()) : []; + options.injectable = options.injectable ? options.injectable : false; options.repository = options.repository ? options.repository : false; options.usecases = options.usecases ? options.usecases : false; options.inmemory = options.inmemory ? options.inmemory : false; - + if (options.domain) { const config = readProjectConfiguration(host, options.domain); options.projectDomain = config