From 62e160ffcd3a93b9d117af04d4305da9bf62fd71 Mon Sep 17 00:00:00 2001 From: "Guilherme Siquinelli (guiseek)" Date: Mon, 4 Oct 2021 13:10:00 -0300 Subject: [PATCH] test(plugin-core): interfaces --- .../src/generators/data/generator.spec.ts | 10 +--- .../core/src/generators/data/generator.ts | 2 +- .../src/generators/domain/generator.spec.ts | 2 +- .../core/src/generators/domain/generator.ts | 2 +- .../generators/presentation/generator.spec.ts | 2 +- .../src/generators/presentation/generator.ts | 2 +- .../core/src/interfaces/generator-schema.ts | 16 ++++++- .../core/src/interfaces/normalized-schema.ts | 29 ++++++++++-- libs/plugin/core/src/utils/add-files.ts | 20 +++++++- .../core/src/utils/normalize-options.ts | 46 ++++++++++++++++--- 10 files changed, 103 insertions(+), 28 deletions(-) diff --git a/libs/plugin/core/src/generators/data/generator.spec.ts b/libs/plugin/core/src/generators/data/generator.spec.ts index 30da01e..834e5e1 100644 --- a/libs/plugin/core/src/generators/data/generator.spec.ts +++ b/libs/plugin/core/src/generators/data/generator.spec.ts @@ -1,15 +1,14 @@ +import { DataGeneratorSchema, DataPluginCoreNormalizedSchema } from '../../interfaces'; import { createTreeWithEmptyWorkspace } from '@nrwl/devkit/testing'; import { Tree, readProjectConfiguration } from '@nrwl/devkit'; import { default as generator } from './generator'; -import { DataGeneratorSchema } from './schema'; import { normalizeOptions } from '../../utils'; describe('data generator', () => { let appTree: Tree; const options: DataGeneratorSchema = { name: 'test', - project: 'core-common', domain: 'account-domain', entity: 'user' }; @@ -24,13 +23,8 @@ describe('data generator', () => { expect(config).toBeDefined(); }); - it('should normalize core path', async () => { - const normalized = normalizeOptions(appTree, options); - expect(normalized.projectCore).toBe('core/common'); - }); - it('should normalize domain path', async () => { - const normalized = normalizeOptions(appTree, options); + const normalized = normalizeOptions(appTree, options) as DataPluginCoreNormalizedSchema; expect(normalized.projectDomain).toBe('account/domain'); }); }); diff --git a/libs/plugin/core/src/generators/data/generator.ts b/libs/plugin/core/src/generators/data/generator.ts index fc9ff51..efbae09 100644 --- a/libs/plugin/core/src/generators/data/generator.ts +++ b/libs/plugin/core/src/generators/data/generator.ts @@ -1,7 +1,7 @@ import { Tree, formatFiles, installPackagesTask } from '@nrwl/devkit'; import { libraryGenerator } from '@nrwl/workspace/generators'; import { addFiles, normalizeOptions } from '../../utils'; -import { DataGeneratorSchema } from './schema'; +import { DataGeneratorSchema } from '../../interfaces'; export default async function (host: Tree, options: DataGeneratorSchema) { const normalizedOptions = normalizeOptions(host, options); diff --git a/libs/plugin/core/src/generators/domain/generator.spec.ts b/libs/plugin/core/src/generators/domain/generator.spec.ts index 052f9ae..abc3cc1 100644 --- a/libs/plugin/core/src/generators/domain/generator.spec.ts +++ b/libs/plugin/core/src/generators/domain/generator.spec.ts @@ -1,8 +1,8 @@ import { createTreeWithEmptyWorkspace } from '@nrwl/devkit/testing'; import { Tree, readProjectConfiguration } from '@nrwl/devkit'; +import { DomainGeneratorSchema } from '../../interfaces'; import { default as generator } from './generator'; -import { DomainGeneratorSchema } from './schema'; describe('domain generator', () => { let appTree: Tree; diff --git a/libs/plugin/core/src/generators/domain/generator.ts b/libs/plugin/core/src/generators/domain/generator.ts index aeb57c0..87ccb64 100644 --- a/libs/plugin/core/src/generators/domain/generator.ts +++ b/libs/plugin/core/src/generators/domain/generator.ts @@ -1,7 +1,7 @@ import { runTasksInSerial } from '@nrwl/workspace/src/utilities/run-tasks-in-serial'; import { normalizeOptions, addFiles, nxCleanCoreVersion } from '../../utils'; import { libraryGenerator } from '@nrwl/workspace/generators'; -import { DomainGeneratorSchema } from './schema'; +import { DomainGeneratorSchema } from '../../interfaces'; import { Tree, formatFiles, diff --git a/libs/plugin/core/src/generators/presentation/generator.spec.ts b/libs/plugin/core/src/generators/presentation/generator.spec.ts index 7e2ceb9..0de99be 100644 --- a/libs/plugin/core/src/generators/presentation/generator.spec.ts +++ b/libs/plugin/core/src/generators/presentation/generator.spec.ts @@ -6,7 +6,7 @@ import { PresentationGeneratorSchema } from './schema'; describe('presentation generator', () => { let appTree: Tree; - const options: PresentationGeneratorSchema = { name: 'test', project: 'core', domain: 'todo-domain', data: 'todo-data-access', entity: 'seek' }; + const options: PresentationGeneratorSchema = { name: 'test', domain: 'todo-domain', data: 'todo-data-access', entity: 'seek' }; beforeEach(() => { appTree = createTreeWithEmptyWorkspace(); diff --git a/libs/plugin/core/src/generators/presentation/generator.ts b/libs/plugin/core/src/generators/presentation/generator.ts index 80078a8..bc931bc 100644 --- a/libs/plugin/core/src/generators/presentation/generator.ts +++ b/libs/plugin/core/src/generators/presentation/generator.ts @@ -1,7 +1,7 @@ import { Tree, formatFiles, installPackagesTask } from '@nrwl/devkit'; +import { PresentationGeneratorSchema } from '../../interfaces'; import { libraryGenerator } from '@nrwl/workspace/generators'; import { addFiles, normalizeOptions } from '../../utils'; -import { PresentationGeneratorSchema } from './schema'; export default async function ( host: Tree, diff --git a/libs/plugin/core/src/interfaces/generator-schema.ts b/libs/plugin/core/src/interfaces/generator-schema.ts index 5925e90..5c724d2 100644 --- a/libs/plugin/core/src/interfaces/generator-schema.ts +++ b/libs/plugin/core/src/interfaces/generator-schema.ts @@ -1,8 +1,20 @@ export interface GeneratorSchema { name: string; + tags?: string; + directory?: string; +} + +export interface DomainGeneratorSchema extends GeneratorSchema { entity?: string; domain?: string; + repository?: boolean; + usecases?: boolean; +} + +export interface DataGeneratorSchema extends DomainGeneratorSchema { + inmemory?: boolean; +} + +export interface PresentationGeneratorSchema extends DataGeneratorSchema { data?: string; - tags?: string; - directory?: string; } diff --git a/libs/plugin/core/src/interfaces/normalized-schema.ts b/libs/plugin/core/src/interfaces/normalized-schema.ts index 72cfb38..8bd0169 100644 --- a/libs/plugin/core/src/interfaces/normalized-schema.ts +++ b/libs/plugin/core/src/interfaces/normalized-schema.ts @@ -1,10 +1,31 @@ -import { GeneratorSchema } from './generator-schema'; +import { + GeneratorSchema, + DataGeneratorSchema, + DomainGeneratorSchema, + PresentationGeneratorSchema, +} from './generator-schema'; + export interface PluginCoreNormalizedSchema extends GeneratorSchema { projectName: string; projectRoot: string; projectDirectory: string; - projectDomain?: string; - projectData?: string; parsedTags: string[]; - npmScope?: string; +} + +export interface DomainPluginCoreNormalizedSchema + extends PluginCoreNormalizedSchema, + DomainGeneratorSchema { + npmScope: string; +} + +export interface DataPluginCoreNormalizedSchema + extends DomainPluginCoreNormalizedSchema, + DataGeneratorSchema { + projectDomain: string; +} + +export interface PresentationPluginCoreNormalizedSchema + extends DataPluginCoreNormalizedSchema, + PresentationGeneratorSchema { + projectData: string; } diff --git a/libs/plugin/core/src/utils/add-files.ts b/libs/plugin/core/src/utils/add-files.ts index 59e5e2d..c211525 100644 --- a/libs/plugin/core/src/utils/add-files.ts +++ b/libs/plugin/core/src/utils/add-files.ts @@ -1,10 +1,26 @@ import { Tree, names, generateFiles, offsetFromRoot } from '@nrwl/devkit'; -import { PluginCoreNormalizedSchema } from '../interfaces'; +import { + DataPluginCoreNormalizedSchema, + DomainPluginCoreNormalizedSchema, + PresentationPluginCoreNormalizedSchema, +} from '../interfaces'; import { join } from 'path'; export function addFiles( host: Tree, - options: PluginCoreNormalizedSchema, + options: DomainPluginCoreNormalizedSchema, + dir: string +); + +export function addFiles( + host: Tree, + options: DataPluginCoreNormalizedSchema, + dir: string +); + +export function addFiles( + host: Tree, + options: PresentationPluginCoreNormalizedSchema, dir: string ) { const libNames = names(options.name); diff --git a/libs/plugin/core/src/utils/normalize-options.ts b/libs/plugin/core/src/utils/normalize-options.ts index ef54750..d86bd61 100644 --- a/libs/plugin/core/src/utils/normalize-options.ts +++ b/libs/plugin/core/src/utils/normalize-options.ts @@ -1,7 +1,30 @@ -import { GeneratorSchema, PluginCoreNormalizedSchema } from '../interfaces'; -import { getWorkspaceLayout, names, readWorkspaceConfiguration, Tree } from "@nrwl/devkit"; +import { + DomainGeneratorSchema, + DataGeneratorSchema, + PresentationGeneratorSchema, + DomainPluginCoreNormalizedSchema, + DataPluginCoreNormalizedSchema, + PresentationPluginCoreNormalizedSchema, +} from '../interfaces'; +import { + Tree, + names, + getWorkspaceLayout, + readWorkspaceConfiguration, +} from '@nrwl/devkit'; -export function normalizeOptions(host: Tree, options: T): PluginCoreNormalizedSchema { +export function normalizeOptions( + host: Tree, + options: T +): DomainPluginCoreNormalizedSchema; +export function normalizeOptions( + host: Tree, + options: T +): DataPluginCoreNormalizedSchema; +export function normalizeOptions( + host: Tree, + options: T +): PresentationPluginCoreNormalizedSchema { const name = names(options.name).fileName; const projectDirectory = options.directory ? `${names(options.directory).fileName}/${name}` @@ -13,21 +36,30 @@ export function normalizeOptions(host: Tree, options: ? options.tags.split(',').map((s) => s.trim()) : []; - const normalized: PluginCoreNormalizedSchema = { + + options.repository = options.repository ? options.repository : false; + options.usecases = options.usecases ? options.usecases : false; + options.inmemory = options.inmemory ? options.inmemory : false; + + const normalized: PresentationPluginCoreNormalizedSchema = { ...options, projectName, projectRoot, projectDirectory, + projectDomain: '', + projectData: '', parsedTags, npmScope, - } + }; if (options.domain) { - normalized.projectDomain = options.domain.replace(new RegExp('-', 'g'), '/'); + (normalized as DataPluginCoreNormalizedSchema).projectDomain = + options.domain.replace(new RegExp('-', 'g'), '/'); } if (options.data) { - normalized.projectData = options.data.replace(new RegExp('-', 'g'), '/'); + (normalized as PresentationPluginCoreNormalizedSchema).projectData = + options.data.replace(new RegExp('-', 'g'), '/'); } return normalized;