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 support for custom store interface name #810

Merged
merged 1 commit into from
Feb 16, 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
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