Skip to content

Commit

Permalink
feat(Schematics): Add option to group feature blueprints in respectiv…
Browse files Browse the repository at this point in the history
…e folders (#736)
  • Loading branch information
brandonroberts authored and MikeRyanDev committed Jan 22, 2018
1 parent d73989e commit b82c35d
Show file tree
Hide file tree
Showing 20 changed files with 106 additions and 10 deletions.
11 changes: 11 additions & 0 deletions modules/schematics/src/action/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ describe('Action Schematic', () => {
path: 'app',
sourceDir: 'src',
spec: false,
group: false,
flat: true,
};

it('should create one file', () => {
Expand Down Expand Up @@ -44,4 +46,13 @@ describe('Action Schematic', () => {
expect(fileContent).toMatch(/export enum FooActionTypes/);
}
});

it('should group within an "actions" folder if group is set', () => {
const tree = schematicRunner.runSchematic('action', {
...defaultOptions,
group: true,
});
expect(tree.files.length).toEqual(1);
expect(tree.files[0]).toEqual('/src/app/actions/foo.actions.ts');
});
});
6 changes: 5 additions & 1 deletion modules/schematics/src/action/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@ export default function(options: ActionOptions): Rule {
const templateSource = apply(url('./files'), [
options.spec ? noop() : filter(path => !path.endsWith('__spec.ts')),
template({
'if-flat': (s: string) => (options.flat ? '' : s),
'if-flat': (s: string) =>
stringUtils.group(
options.flat ? '' : s,
options.group ? 'actions' : ''
),
...stringUtils,
...(options as object),
dot: () => '.',
Expand Down
1 change: 1 addition & 0 deletions modules/schematics/src/action/schema.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ export interface Schema {
*/
spec?: boolean;
flat?: boolean;
group?: boolean;
}
5 changes: 5 additions & 0 deletions modules/schematics/src/action/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@
"type": "boolean",
"default": true,
"description": "Flag to indicate if a dir is created."
},
"group": {
"type": "boolean",
"default": false,
"description": "Group actions file within 'actions' folder"
}
},
"required": [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Injectable } from '@angular/core';
import { Actions, Effect } from '@ngrx/effects';
<% if(feature) { %>import { <%= classify(name) %>Actions, <%= classify(name) %>ActionTypes } from './<%= dasherize(name) %>.actions';<% } %>
<% if(feature) { %>import { <%= classify(name) %>Actions, <%= classify(name) %>ActionTypes } from '<%= featurePath(group, "actions") %><%= dasherize(name) %>.actions';<% } %>

@Injectable()
export class <%= classify(name) %>Effects {
Expand Down
11 changes: 11 additions & 0 deletions modules/schematics/src/effect/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ describe('Effect Schematic', () => {
flat: false,
feature: false,
root: false,
group: false,
};

let appTree: Tree;
Expand Down Expand Up @@ -173,4 +174,14 @@ describe('Effect Schematic', () => {

expect(content).not.toMatch(/EffectsModule\.forRoot\(\[FooEffects\]\)/);
});

it('should group within an "effects" folder if group is set', () => {
const options = { ...defaultOptions, flat: true, spec: false, group: true };

const tree = schematicRunner.runSchematic('effect', options, appTree);
const files = tree.files;
expect(
files.indexOf('/src/app/effects/foo.effects.ts')
).toBeGreaterThanOrEqual(0);
});
});
7 changes: 6 additions & 1 deletion modules/schematics/src/effect/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ function addImportToNgModule(options: EffectOptions): Rule {

const effectsPath =
`/${options.sourceDir}/${options.path}/` +
(options.group ? 'effects/' : '') +
(options.flat ? '' : stringUtils.dasherize(options.name) + '/') +
stringUtils.dasherize(options.name) +
'.effects';
Expand Down Expand Up @@ -108,7 +109,11 @@ export default function(options: EffectOptions): Rule {
options.spec ? noop() : filter(path => !path.endsWith('__spec.ts')),
template({
...stringUtils,
'if-flat': (s: string) => (options.flat ? '' : s),
'if-flat': (s: string) =>
stringUtils.group(
options.flat ? '' : s,
options.group ? 'effects' : ''
),
...(options as object),
dot: () => '.',
}),
Expand Down
1 change: 1 addition & 0 deletions modules/schematics/src/effect/schema.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ export interface Schema {
module?: string;
root?: boolean;
feature?: boolean;
group?: boolean;
}
7 changes: 6 additions & 1 deletion modules/schematics/src/effect/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,12 @@
"type": "boolean",
"default": false,
"description": "Flag to indicate if part of a feature schematic."
}
},
"group": {
"type": "boolean",
"default": false,
"description": "Group effects file within 'effects' folder"
}
},
"required": [
"name"
Expand Down
23 changes: 23 additions & 0 deletions modules/schematics/src/feature/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ describe('Feature Schematic', () => {
sourceDir: 'src',
module: '',
spec: true,
group: false,
};

it('should create all files of a feature', () => {
Expand All @@ -34,4 +35,26 @@ describe('Feature Schematic', () => {
files.indexOf('/src/app/foo.effects.spec.ts')
).toBeGreaterThanOrEqual(0);
});

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

const tree = schematicRunner.runSchematic('feature', options);
const files = tree.files;
expect(
files.indexOf('/src/app/actions/foo.actions.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);
expect(
files.indexOf('/src/app/effects/foo.effects.ts')
).toBeGreaterThanOrEqual(0);
expect(
files.indexOf('/src/app/effects/foo.effects.spec.ts')
).toBeGreaterThanOrEqual(0);
});
});
3 changes: 3 additions & 0 deletions modules/schematics/src/feature/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,15 @@ export default function(options: FeatureOptions): Rule {
return chain([
schematic('action', {
flat: options.flat,
group: options.group,
name: options.name,
path: options.path,
sourceDir: options.sourceDir,
spec: false,
}),
schematic('reducer', {
flat: options.flat,
group: options.group,
module: options.module,
name: options.name,
path: options.path,
Expand All @@ -44,6 +46,7 @@ export default function(options: FeatureOptions): Rule {
}),
schematic('effect', {
flat: options.flat,
group: options.group,
module: options.module,
name: options.name,
path: options.path,
Expand Down
1 change: 1 addition & 0 deletions modules/schematics/src/feature/schema.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ export interface Schema {
flat?: boolean;
spec?: boolean;
reducers?: string;
group?: boolean;
}
5 changes: 5 additions & 0 deletions modules/schematics/src/feature/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@
"type": "string",
"description": "Specifies the reducers file.",
"aliases": ["r"]
},
"group": {
"type": "boolean",
"default": false,
"description": "Group actions, reducers and effects within relative subfolders"
}
},
"required": [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Action } from '@ngrx/store';
<% if(feature) { %>import { <%= classify(name) %>Actions, <%= classify(name) %>ActionTypes } from './<%= dasherize(name) %>.actions';<% } %>
<% if(feature) { %>import { <%= classify(name) %>Actions, <%= classify(name) %>ActionTypes } from '<%= featurePath(group, "actions") %><%= dasherize(name) %>.actions';<% } %>

export interface State {

Expand Down
9 changes: 9 additions & 0 deletions modules/schematics/src/reducer/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,13 @@ describe('Reducer Schematic', () => {

expect(reducers).toMatch(/foo\: fromFoo.reducer/);
});

it('should group within a "reducers" folder if group is set', () => {
const tree = schematicRunner.runSchematic('reducer', {
...defaultOptions,
group: true,
});
expect(tree.files.length).toEqual(1);
expect(tree.files[0]).toEqual('/src/app/reducers/foo.reducer.ts');
});
});
6 changes: 5 additions & 1 deletion modules/schematics/src/reducer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,11 @@ export default function(options: ReducerOptions): Rule {
options.spec ? noop() : filter(path => !path.endsWith('__spec.ts')),
template({
...stringUtils,
'if-flat': (s: string) => (options.flat ? '' : s),
'if-flat': (s: string) =>
stringUtils.group(
options.flat ? '' : s,
options.group ? 'reducers' : ''
),
...(options as object),
dot: () => '.',
}),
Expand Down
1 change: 1 addition & 0 deletions modules/schematics/src/reducer/schema.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ export interface Schema {
module?: string;
feature?: boolean;
reducers?: string;
group?: boolean;
}
5 changes: 5 additions & 0 deletions modules/schematics/src/reducer/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@
"type": "string",
"description": "Specifies the reducers file.",
"aliases": ["r"]
},
"group": {
"type": "boolean",
"default": false,
"description": "Group reducer file within 'reducers' folder"
}
},
"required": [
Expand Down
8 changes: 4 additions & 4 deletions modules/schematics/src/strings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,10 +134,10 @@ export function capitalize(str: string): string {
return str.charAt(0).toUpperCase() + str.substr(1);
}

export function uppercase(str: string): string {
return str.toUpperCase();
export function group(path: string, group: string | undefined) {
return group ? `${group}/${path}` : path;
}

export function lowercase(str: string): string {
return str.toLowerCase();
export function featurePath(group: boolean | undefined, path: string) {
return group ? `../${path}/` : './';
}
2 changes: 2 additions & 0 deletions modules/schematics/src/utility/ngrx-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export function addReducerToState(options: ReducerOptions): Rule {

const reducerPath =
`/${options.sourceDir}/${options.path}/` +
(options.group ? 'reducers/' : '') +
(options.flat ? '' : stringUtils.dasherize(options.name) + '/') +
stringUtils.dasherize(options.name) +
'.reducer';
Expand Down Expand Up @@ -216,6 +217,7 @@ export function addReducerImportToNgModule(options: ReducerOptions): Rule {

const reducerPath =
`/${options.sourceDir}/${options.path}/` +
(options.group ? 'reducers/' : '') +
(options.flat ? '' : stringUtils.dasherize(options.name) + '/') +
stringUtils.dasherize(options.name) +
'.reducer';
Expand Down

0 comments on commit b82c35d

Please sign in to comment.