Skip to content

Commit 0429276

Browse files
brandonrobertsMikeRyanDev
authored andcommitted
feat(Schematics): Add group option to entity blueprint (#792)
Closes #779
1 parent 945bf40 commit 0429276

File tree

8 files changed

+56
-4
lines changed

8 files changed

+56
-4
lines changed

modules/schematics/src/entity/files/__path__/__name@dasherize@if-flat__/__name@dasherize__.actions.ts renamed to modules/schematics/src/entity/files/__path__/__name@dasherize@if-flat__/__name@dasherize@group-actions__.actions.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Action } from '@ngrx/store';
22
import { Update } from '@ngrx/entity';
3-
import { <%= classify(name) %> } from './<%= dasherize(name) %>.model';
3+
import { <%= classify(name) %> } from '<%= featurePath(group, flat, "models", dasherize(name)) %><%= dasherize(name) %>.model';
44

55
export enum <%= classify(name) %>ActionTypes {
66
Load<%= classify(name) %>s = '[<%= classify(name) %>] Load <%= classify(name) %>s',

modules/schematics/src/entity/files/__path__/__name@dasherize@if-flat__/__name@dasherize__.model.ts renamed to modules/schematics/src/entity/files/__path__/__name@dasherize@if-flat__/__name@dasherize@group-models__.model.ts

File renamed without changes.

modules/schematics/src/entity/files/__path__/__name@dasherize@if-flat__/__name@dasherize__.reducer.ts renamed to modules/schematics/src/entity/files/__path__/__name@dasherize@if-flat__/__name@dasherize@group-reducers__.reducer.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { EntityState, EntityAdapter, createEntityAdapter } from '@ngrx/entity';
2-
import { <%= classify(name) %> } from './<%= dasherize(name) %>.model';
3-
import { <%= classify(name) %>Actions, <%= classify(name) %>ActionTypes } from './<%= dasherize(name) %>.actions';
2+
import { <%= classify(name) %> } from '<%= featurePath(group, flat, "models", dasherize(name)) %><%= dasherize(name) %>.model';
3+
import { <%= classify(name) %>Actions, <%= classify(name) %>ActionTypes } from '<%= featurePath(group, flat, "actions", dasherize(name)) %><%= dasherize(name) %>.actions';
44

55
export interface State extends EntityState<<%= classify(name) %>> {
66
// additional entities state properties

modules/schematics/src/entity/files/__path__/__name@dasherize@if-flat__/__name@dasherize__.reducer__dot__spec.ts renamed to modules/schematics/src/entity/files/__path__/__name@dasherize@if-flat__/__name@dasherize@group-reducers__.reducer__dot__spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { reducer, initialState } from './<%= dasherize(name) %>.reducer';
1+
import { reducer, initialState } from '<%= featurePath(group, flat, "reducers", dasherize(name)) %><%= dasherize(name) %>.reducer';
22

33
describe('<%= classify(name) %> Reducer', () => {
44
describe('unknown action', () => {

modules/schematics/src/entity/index.spec.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,45 @@ describe('Entity Schematic', () => {
6060

6161
const tree = schematicRunner.runSchematic('entity', options, appTree);
6262
const content = getFileContent(tree, '/src/app/app.module.ts');
63+
6364
expect(content).toMatch(/import \* as fromFoo from '\.\/foo.reducer';/);
6465
});
66+
67+
it('should create all files of an entity within grouped and nested folders', () => {
68+
const options = { ...defaultOptions, flat: false, group: true, spec: true };
69+
70+
const tree = schematicRunner.runSchematic('entity', options);
71+
const files = tree.files;
72+
expect(
73+
files.indexOf('/src/app/foo/actions/foo.actions.ts')
74+
).toBeGreaterThanOrEqual(0);
75+
expect(
76+
files.indexOf('/src/app/foo/models/foo.model.ts')
77+
).toBeGreaterThanOrEqual(0);
78+
expect(
79+
files.indexOf('/src/app/foo/reducers/foo.reducer.ts')
80+
).toBeGreaterThanOrEqual(0);
81+
expect(
82+
files.indexOf('/src/app/foo/reducers/foo.reducer.spec.ts')
83+
).toBeGreaterThanOrEqual(0);
84+
});
85+
86+
it('should create all files of an entity within grouped folders if group is set', () => {
87+
const options = { ...defaultOptions, group: true, spec: true };
88+
89+
const tree = schematicRunner.runSchematic('entity', options);
90+
const files = tree.files;
91+
expect(
92+
files.indexOf('/src/app/actions/foo.actions.ts')
93+
).toBeGreaterThanOrEqual(0);
94+
expect(
95+
files.indexOf('/src/app/models/foo.model.ts')
96+
).toBeGreaterThanOrEqual(0);
97+
expect(
98+
files.indexOf('/src/app/reducers/foo.reducer.ts')
99+
).toBeGreaterThanOrEqual(0);
100+
expect(
101+
files.indexOf('/src/app/reducers/foo.reducer.spec.ts')
102+
).toBeGreaterThanOrEqual(0);
103+
});
65104
});

modules/schematics/src/entity/index.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,12 @@ export default function(options: EntityOptions): Rule {
3939
template({
4040
...stringUtils,
4141
'if-flat': (s: string) => (options.flat ? '' : s),
42+
'group-actions': (name: string) =>
43+
stringUtils.group(name, options.group ? 'actions' : ''),
44+
'group-models': (name: string) =>
45+
stringUtils.group(name, options.group ? 'models' : ''),
46+
'group-reducers': (s: string) =>
47+
stringUtils.group(s, options.group ? 'reducers' : ''),
4248
...(options as object),
4349
dot: () => '.',
4450
}),

modules/schematics/src/entity/schema.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@ export interface Schema {
1010
module?: string;
1111
reducers?: string;
1212
flat?: boolean;
13+
group?: boolean;
1314
}

modules/schematics/src/entity/schema.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@
3737
"type": "boolean",
3838
"default": true,
3939
"description": "Flag to indicate if a dir is created."
40+
},
41+
"group": {
42+
"type": "boolean",
43+
"default": false,
44+
"description": "Group actions, reducers and effects within relative subfolders",
45+
"aliases": ["g"]
4046
}
4147
},
4248
"required": [

0 commit comments

Comments
 (0)