Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(schematics): add entity generation as part of feature schematic #3850

Merged
merged 10 commits into from
Aug 5, 2023
6 changes: 6 additions & 0 deletions modules/schematics/src/entity/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@
"default": false,
"description": "Group actions, reducers and effects within relative subfolders",
"aliases": ["g"]
},
"feature": {
"type": "boolean",
"default": false,
"description": "Flag to indicate if part of a feature schematic.",
"visible": false
}
},
"required": []
Expand Down
5 changes: 5 additions & 0 deletions modules/schematics/src/entity/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,9 @@ export interface Schema {
*/

group?: boolean;

/**
* Specifies if this is grouped within a feature
*/
feature?: boolean;
}
38 changes: 38 additions & 0 deletions modules/schematics/src/feature/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ describe('Feature Schematic', () => {
project: 'bar',
module: '',
group: false,
entity: false,
};

const projectPath = getTestProjectPath();
Expand Down Expand Up @@ -64,6 +65,7 @@ describe('Feature Schematic', () => {
expect(
files.includes(`${projectPath}/src/app/foo.selectors.spec.ts`)
).toBeTruthy();
expect(files.includes(`${projectPath}/src/app/foo.model.ts`)).toBeFalsy();
});

it('should not create test files when skipTests is true', async () => {
Expand Down Expand Up @@ -269,4 +271,40 @@ describe('Feature Schematic', () => {

expect(fileContent).toMatchSnapshot();
});

it('should create all files of a feature with an entity', async () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about we use snapshot tests for this

const options = { ...defaultOptions, entity: true };

const tree = await schematicRunner.runSchematic(
'feature',
options,
appTree
);
const files = tree.files;
expect(
files.includes(`${projectPath}/src/app/foo.actions.ts`)
).toBeTruthy();
expect(
files.includes(`${projectPath}/src/app/foo.actions.spec.ts`)
).toBeFalsy();
expect(
files.includes(`${projectPath}/src/app/foo.reducer.ts`)
).toBeTruthy();
expect(
files.includes(`${projectPath}/src/app/foo.reducer.spec.ts`)
).toBeTruthy();
expect(
files.includes(`${projectPath}/src/app/foo.effects.ts`)
).toBeTruthy();
expect(
files.includes(`${projectPath}/src/app/foo.effects.spec.ts`)
).toBeTruthy();
expect(
files.includes(`${projectPath}/src/app/foo.selectors.ts`)
).toBeFalsy();
expect(
files.includes(`${projectPath}/src/app/foo.selectors.spec.ts`)
).toBeFalsy();
expect(files.includes(`${projectPath}/src/app/foo.model.ts`)).toBeTruthy();
wgd3 marked this conversation as resolved.
Show resolved Hide resolved
});
});
115 changes: 67 additions & 48 deletions modules/schematics/src/feature/index.ts
Original file line number Diff line number Diff line change
@@ -1,59 +1,78 @@
import {
chain,
Rule,
schematic,
SchematicContext,
Tree,
chain,
schematic,
} from '@angular-devkit/schematics';

import { Schema as FeatureOptions } from './schema';

export default function (options: FeatureOptions): Rule {
return (host: Tree, context: SchematicContext) => {
return chain([
schematic('action', {
flat: options.flat,
group: options.group,
name: options.name,
path: options.path,
project: options.project,
skipTests: options.skipTests,
api: options.api,
prefix: options.prefix,
}),
schematic('reducer', {
flat: options.flat,
group: options.group,
module: options.module,
name: options.name,
path: options.path,
project: options.project,
skipTests: options.skipTests,
reducers: options.reducers,
feature: true,
api: options.api,
prefix: options.prefix,
}),
schematic('effect', {
flat: options.flat,
group: options.group,
module: options.module,
name: options.name,
path: options.path,
project: options.project,
skipTests: options.skipTests,
feature: true,
api: options.api,
prefix: options.prefix,
}),
schematic('selector', {
flat: options.flat,
group: options.group,
name: options.name,
path: options.path,
project: options.project,
skipTests: options.skipTests,
feature: true,
}),
])(host, context);
return chain(
(options.entity
? [
schematic('entity', {
name: options.name,
path: options.path,
project: options.project,
flat: options.flat,
skipTests: options.skipTests,
module: options.module,
reducers: options.reducers,
group: options.group,
feature: true,
}),
]
: [
schematic('action', {
flat: options.flat,
group: options.group,
name: options.name,
path: options.path,
project: options.project,
skipTests: options.skipTests,
api: options.api,
prefix: options.prefix,
}),
schematic('reducer', {
flat: options.flat,
group: options.group,
module: options.module,
name: options.name,
path: options.path,
project: options.project,
skipTests: options.skipTests,
reducers: options.reducers,
feature: true,
api: options.api,
prefix: options.prefix,
}),
schematic('selector', {
flat: options.flat,
group: options.group,
name: options.name,
path: options.path,
project: options.project,
skipTests: options.skipTests,
feature: true,
}),
]
).concat([
schematic('effect', {
flat: options.flat,
group: options.group,
module: options.module,
name: options.name,
path: options.path,
project: options.project,
skipTests: options.skipTests,
feature: true,
api: options.api,
prefix: options.prefix,
}),
])
)(host, context);
};
}
7 changes: 7 additions & 0 deletions modules/schematics/src/feature/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,13 @@
"type": "string",
"default": "load",
"x-prompt": "What should be the prefix of the action, effect and reducer?"
},
"entity": {
"description": "Toggle whether an entity is created as part of the feature",
"type": "boolean",
"aliases": ["e"],
"x-prompt": "Should we use @ngrx/entity to create the reducer?",
"default": "false"
}
},
"required": []
Expand Down
2 changes: 2 additions & 0 deletions modules/schematics/src/feature/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,6 @@ export interface Schema {
api?: boolean;

prefix?: string;

entity?: boolean;
}