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 group option to entity blueprint #792

Merged
merged 1 commit into from
Feb 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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