Skip to content

Commit

Permalink
test(plugin-core): interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
guiseek committed Oct 4, 2021
1 parent 8400dff commit 62e160f
Show file tree
Hide file tree
Showing 10 changed files with 103 additions and 28 deletions.
10 changes: 2 additions & 8 deletions libs/plugin/core/src/generators/data/generator.spec.ts
Original file line number Diff line number Diff line change
@@ -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'
};
Expand All @@ -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');
});
});
2 changes: 1 addition & 1 deletion libs/plugin/core/src/generators/data/generator.ts
Original file line number Diff line number Diff line change
@@ -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);
Expand Down
2 changes: 1 addition & 1 deletion libs/plugin/core/src/generators/domain/generator.spec.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
2 changes: 1 addition & 1 deletion libs/plugin/core/src/generators/domain/generator.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion libs/plugin/core/src/generators/presentation/generator.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
16 changes: 14 additions & 2 deletions libs/plugin/core/src/interfaces/generator-schema.ts
Original file line number Diff line number Diff line change
@@ -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;
}
29 changes: 25 additions & 4 deletions libs/plugin/core/src/interfaces/normalized-schema.ts
Original file line number Diff line number Diff line change
@@ -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;
}
20 changes: 18 additions & 2 deletions libs/plugin/core/src/utils/add-files.ts
Original file line number Diff line number Diff line change
@@ -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);
Expand Down
46 changes: 39 additions & 7 deletions libs/plugin/core/src/utils/normalize-options.ts
Original file line number Diff line number Diff line change
@@ -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<T extends GeneratorSchema>(host: Tree, options: T): PluginCoreNormalizedSchema {
export function normalizeOptions<T extends DomainGeneratorSchema>(
host: Tree,
options: T
): DomainPluginCoreNormalizedSchema;
export function normalizeOptions<T extends DataGeneratorSchema>(
host: Tree,
options: T
): DataPluginCoreNormalizedSchema;
export function normalizeOptions<T extends PresentationGeneratorSchema>(
host: Tree,
options: T
): PresentationPluginCoreNormalizedSchema {
const name = names(options.name).fileName;
const projectDirectory = options.directory
? `${names(options.directory).fileName}/${name}`
Expand All @@ -13,21 +36,30 @@ export function normalizeOptions<T extends GeneratorSchema>(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;
Expand Down

0 comments on commit 62e160f

Please sign in to comment.