Skip to content

Commit

Permalink
feat(Schematics): Add support for custom store interface name (#810)
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonroberts authored and MikeRyanDev committed Feb 16, 2018
1 parent 3e25543 commit 3e31894
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 4 deletions.
8 changes: 7 additions & 1 deletion docs/schematics/store.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,13 @@ Provide the folder where the state files will be created.

- `--statePath`
- Type: `string`
- Default: `reducers`
- Default: `reducers`

Provide the name of the interface exported for your state. When defining with the `--root` option, the name of the store will be used to define the interface name.

- `--stateInterface`
- Type: `string`
- Default: `State`

#### Examples

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ import {
} from '@ngrx/store';
import { environment } from '<%= environmentsPath %>';

export interface State {
export interface <%= classify(stateInterface) %> {

}

export const reducers: ActionReducerMap<State> = {
export const reducers: ActionReducerMap<<%= classify(stateInterface) %>> = {

};


export const metaReducers: MetaReducer<State>[] = !environment.production ? [] : [];
export const metaReducers: MetaReducer<<%= classify(stateInterface) %>>[] = !environment.production ? [] : [];
41 changes: 41 additions & 0 deletions modules/schematics/src/store/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,45 @@ describe('Store Schematic', () => {
const content = getFileContent(tree, '/src/app/app.module.ts');
expect(content).toMatch(/import \* as fromFoo from '\.\/reducers';/);
});

it('should support a default root state interface name', () => {
const options = { ...defaultOptions, name: 'State' };

const tree = schematicRunner.runSchematic('store', options, appTree);
const content = getFileContent(tree, '/src/app/reducers/index.ts');
expect(content).toMatch(/export interface State {/);
});

it('should support a custom root state interface name', () => {
const options = {
...defaultOptions,
name: 'State',
stateInterface: 'AppState',
};

const tree = schematicRunner.runSchematic('store', options, appTree);
const content = getFileContent(tree, '/src/app/reducers/index.ts');
expect(content).toMatch(/export interface AppState {/);
});

it('should support a default feature state interface name', () => {
const options = { ...defaultOptions, root: false, name: 'Feature' };

const tree = schematicRunner.runSchematic('store', options, appTree);
const content = getFileContent(tree, '/src/app/reducers/index.ts');
expect(content).toMatch(/export interface State {/);
});

it('should support a custom feature state interface name', () => {
const options = {
...defaultOptions,
root: false,
name: 'Feature',
stateInterface: 'FeatureState',
};

const tree = schematicRunner.runSchematic('store', options, appTree);
const content = getFileContent(tree, '/src/app/reducers/index.ts');
expect(content).toMatch(/export interface FeatureState {/);
});
});
8 changes: 8 additions & 0 deletions modules/schematics/src/store/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,14 @@ export default function(options: ServiceOptions): Rule {
options.module = findModuleFromOptions(host, options);
}

if (
options.root &&
options.stateInterface &&
options.stateInterface !== 'State'
) {
options.stateInterface = stringUtils.classify(options.stateInterface);
}

const templateSource = apply(url('./files'), [
template({
...stringUtils,
Expand Down
4 changes: 4 additions & 0 deletions modules/schematics/src/store/schema.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,8 @@ export interface Schema {
module?: string;
statePath?: string;
root?: boolean;
/**
* Specifies the interface for the state
*/
stateInterface?: string;
}
6 changes: 6 additions & 0 deletions modules/schematics/src/store/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@
"type": "boolean",
"default": false,
"description": "Flag to setup the root state or feature state."
},
"stateInterface": {
"type": "string",
"default": "State",
"description": "Specifies the interface for the state.",
"alias": "si"
}
},
"required": [
Expand Down

0 comments on commit 3e31894

Please sign in to comment.