Skip to content

Commit

Permalink
feat(Schematics): Add group option to entity blueprint
Browse files Browse the repository at this point in the history
Closes #779
  • Loading branch information
brandonroberts committed Feb 6, 2018
1 parent 945bf40 commit 5fac5b5
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Action } from '@ngrx/store';
import { Update } from '@ngrx/entity';
import { <%= classify(name) %> } from './<%= dasherize(name) %>.model';
import { <%= classify(name) %> } from '<%= featurePath(group, flat, "models", dasherize(name)) %><%= dasherize(name) %>.model';

export enum <%= classify(name) %>ActionTypes {
Load<%= classify(name) %>s = '[<%= classify(name) %>] Load <%= classify(name) %>s',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { EntityState, EntityAdapter, createEntityAdapter } from '@ngrx/entity';
import { <%= classify(name) %> } from './<%= dasherize(name) %>.model';
import { <%= classify(name) %>Actions, <%= classify(name) %>ActionTypes } from './<%= dasherize(name) %>.actions';
import { <%= classify(name) %> } from '<%= featurePath(group, flat, "models", dasherize(name)) %><%= dasherize(name) %>.model';
import { <%= classify(name) %>Actions, <%= classify(name) %>ActionTypes } from '<%= featurePath(group, flat, "actions", dasherize(name)) %><%= dasherize(name) %>.actions';

export interface State extends EntityState<<%= classify(name) %>> {
// additional entities state properties
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { reducer, initialState } from './<%= dasherize(name) %>.reducer';
import { reducer, initialState } from '<%= featurePath(group, flat, "reducers", dasherize(name)) %><%= dasherize(name) %>.reducer';

describe('<%= classify(name) %> Reducer', () => {
describe('unknown action', () => {
Expand Down
39 changes: 39 additions & 0 deletions modules/schematics/src/entity/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,45 @@ describe('Entity Schematic', () => {

const tree = schematicRunner.runSchematic('entity', options, appTree);
const content = getFileContent(tree, '/src/app/app.module.ts');

expect(content).toMatch(/import \* as fromFoo from '\.\/foo.reducer';/);
});

it('should create all files of an entity within grouped and nested folders', () => {
const options = { ...defaultOptions, flat: false, group: true, spec: true };

const tree = schematicRunner.runSchematic('entity', options);
const files = tree.files;
expect(
files.indexOf('/src/app/foo/actions/foo.actions.ts')
).toBeGreaterThanOrEqual(0);
expect(
files.indexOf('/src/app/foo/models/foo.model.ts')
).toBeGreaterThanOrEqual(0);
expect(
files.indexOf('/src/app/foo/reducers/foo.reducer.ts')
).toBeGreaterThanOrEqual(0);
expect(
files.indexOf('/src/app/foo/reducers/foo.reducer.spec.ts')
).toBeGreaterThanOrEqual(0);
});

it('should create all files of an entity within grouped folders if group is set', () => {
const options = { ...defaultOptions, group: true, spec: true };

const tree = schematicRunner.runSchematic('entity', options);
const files = tree.files;
expect(
files.indexOf('/src/app/actions/foo.actions.ts')
).toBeGreaterThanOrEqual(0);
expect(
files.indexOf('/src/app/models/foo.model.ts')
).toBeGreaterThanOrEqual(0);
expect(
files.indexOf('/src/app/reducers/foo.reducer.ts')
).toBeGreaterThanOrEqual(0);
expect(
files.indexOf('/src/app/reducers/foo.reducer.spec.ts')
).toBeGreaterThanOrEqual(0);
});
});
6 changes: 6 additions & 0 deletions modules/schematics/src/entity/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ export default function(options: EntityOptions): Rule {
template({
...stringUtils,
'if-flat': (s: string) => (options.flat ? '' : s),
'group-actions': (name: string) =>
stringUtils.group(name, options.group ? 'actions' : ''),
'group-models': (name: string) =>
stringUtils.group(name, options.group ? 'models' : ''),
'group-reducers': (s: string) =>
stringUtils.group(s, options.group ? 'reducers' : ''),
...(options as object),
dot: () => '.',
}),
Expand Down
1 change: 1 addition & 0 deletions modules/schematics/src/entity/schema.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ export interface Schema {
module?: string;
reducers?: string;
flat?: boolean;
group?: boolean;
}
6 changes: 6 additions & 0 deletions modules/schematics/src/entity/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@
"type": "boolean",
"default": true,
"description": "Flag to indicate if a dir is created."
},
"group": {
"type": "boolean",
"default": false,
"description": "Group actions, reducers and effects within relative subfolders",
"aliases": ["g"]
}
},
"required": [
Expand Down

0 comments on commit 5fac5b5

Please sign in to comment.