Skip to content

Commit

Permalink
feat(reducer): add error handling for selectors and facade, changed c…
Browse files Browse the repository at this point in the history
…reator-files imports

feat(reducer): add error handling for selectors and facade, changed creator-files imports

feat(reducer): add error handling for selectors and facade, changed creator-files imports

feat(reducer): add error handling for selectors and facade, changed creator-files imports

feat(reducer): add error handling for selectors and facade

feat(reducer): add error handling for selectors and facade

feat(reducer): add error handling for selectors and facade

feat(reducer): add error handling for selectors and facade

feat(reducer): add error handling for selectors and facade
  • Loading branch information
filip-va authored and va-stefanek committed Jul 2, 2021
1 parent 4ef6173 commit 23e92d6
Show file tree
Hide file tree
Showing 13 changed files with 37 additions and 27 deletions.
3 changes: 2 additions & 1 deletion src/collection/action/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ describe('action', () => {
const ngrxOpts: NgrxSchema = {
name: 'test',
module: '/libs/testlib/src/lib/testlib.module.ts',
facade: true
facade: true,
creators: false
};

const classOpts: ClassSchema = {
Expand Down
3 changes: 2 additions & 1 deletion src/collection/crud/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ const collectionPath = path.join(__dirname, '../../collection.json');
const ngrxOpts: NgrxSchema = {
name: 'test',
module: '/libs/data-access-test/src/lib/data-access-test.module.ts',
facade: true
facade: true,
creators: false
};

const classOpts: ClassSchema = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { Action } from '@ngrx/store';
import { createAction, props } from '@ngrx/store';

export enum Types {}

export type CollectiveType = Action;
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { Injectable } from '@angular/core';
import { Effect } from '@ngrx/effects';
import * as from<%= className %>Actions from './<%= fileName %>.actions';
import { <%= className %>PartialState } from './<%= fileName %>.reducer';
import { Actions } from '@ngrx/store';
import { Actions, createEffect, ofType } from '@ngrx/effects';

@Injectable()
export class <%= className %>Effects {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as from<%= className %>Actions from './<%= fileName %>.actions';
import { <%= className %>State, initialState, reducer } from './<%= fileName %>.reducer';
import { <%= className %>State, initialState, <%= propertyName %>Reducer } from './<%= fileName %>.reducer';

describe('<%= className %> Reducer', () => {
let state: <%= className %>State;
Expand All @@ -11,7 +11,7 @@ describe('<%= className %> Reducer', () => {
describe('unknown action', () => {
test('returns the initial state', () => {
const action = {} as any;
const result = reducer(state, action);
const result = <%= propertyName %>Reducer(state, action);

expect(result).toBe(state);
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as from<%= className %>Actions from './<%= fileName %>.actions';
import { createReducer, ActionReducer } from '@ngrx/store';
import { on, createReducer, ActionReducer } from '@ngrx/store';

export const <%= className.toUpperCase() %>_FEATURE_KEY = '<%= propertyName %>';

Expand All @@ -12,11 +12,4 @@ export interface <%= className %>PartialState {

export const initialState: <%= className %>State = {};

const <%= propertyName %>Reducer = createReducer(initialState);

export function reducer(
state: <%= className %>State = initialState,
action: from<%= className %>Actions.CollectiveType
): ActionReducer<<%= className %>State, from<%= className %>Actions.CollectiveType> {
return <%= propertyName %>Reducer(state, action);
}
export const <%= propertyName %>Reducer = createReducer(initialState);
3 changes: 2 additions & 1 deletion src/collection/ngrx/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ describe('ngrx', () => {
{
name: 'state',
module: 'apps/myapp/src/app/app.module.ts',
onlyEmptyRoot: true
onlyEmptyRoot: true,
creators: false
} as NgrxSchema,
appTree
);
Expand Down
2 changes: 1 addition & 1 deletion src/collection/ngrx/ngrx-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
"default": false,
"description": "Do not add NgRx dependencies to package.json (e.g., --skipPackageJson)"
},
"creator": {
"creators": {
"type": "boolean",
"default": true,
"description": "Generate store using Creators",
Expand Down
3 changes: 2 additions & 1 deletion src/collection/reducer/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ describe('reducer', () => {
const ngrxOpts: NgrxSchema = {
name: 'test',
module: '/libs/testlib/src/lib/testlib.module.ts',
facade: true
facade: true,
creators: false
};

const classOpts: ClassSchema = {
Expand Down
3 changes: 2 additions & 1 deletion src/collection/reducer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export function reducer(options: ReducerSchema): Rule {
const reducerSpecSourceFile = readIntoSourceFile(host, stateDir.reducerSpec);
const selectorsSourceFile = readIntoSourceFile(host, stateDir.selectors);
const selectorsSpecSourceFile = readIntoSourceFile(host, stateDir.selectorsSpec);
const facadeSourceFile = readIntoSourceFile(host, stateDir.facade);
const parsedReducerFile = options.creators
? parseReducerWithCreator(reducerSourceFile)
: parseReducerWithSwitch(reducerSourceFile);
Expand All @@ -55,7 +56,7 @@ export function reducer(options: ReducerSchema): Rule {
updateSelectorsSpec(selectorsSpecSourceFile, stateDir, options)
]
: []),
...(options.facade ? [updateFacade(options)] : []),
...(options.facade ? [updateFacade(facadeSourceFile, stateDir, options)] : []),
formatFiles({ skipFormat: !!options.skipFormat })
];

Expand Down
17 changes: 12 additions & 5 deletions src/collection/reducer/rules/update-facade.rule.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { Rule, SchematicContext, Tree } from '@angular-devkit/schematics';
import { Rule, Tree } from '@angular-devkit/schematics';
import { Change, InsertChange } from '@schematics/angular/utility/change';
import * as ts from 'typescript';
import { getSourceNodes, insert } from '../../../utils/ast.utils';
import { names, toPropertyName } from '../../../utils/name.utils';
import { createActionAliasName } from '../../../utils/naming.utils';
import {
parsePropsToUpdate,
parseStateDir,
StateFilePaths,
StateProperty
} from '../../../utils/options-parsing.utils';
import { classify } from '../../../utils/string.utils';
Expand All @@ -30,9 +30,16 @@ function getMethodTemplate(
}`;
}

export function updateFacade(options: ReducerSchema): Rule {
return (host: Tree, context: SchematicContext) => {
const stateDir = parseStateDir(options.stateDir, host);
export function updateFacade(
facadeSourceFile: ts.SourceFile,
stateDir: StateFilePaths,
options: ReducerSchema
): Rule {
if (!facadeSourceFile && options.facade) {
throw new Error('You try to update a facade that does not exist.');
}

return (host: Tree) => {
const namespace = createActionAliasName(stateDir.actions);
const stateProperties = parsePropsToUpdate(options.propsToUpdate);
const facadeClass = findClassBodyInFile(host, stateDir.facade);
Expand Down
4 changes: 4 additions & 0 deletions src/collection/reducer/rules/update-selectors-spec.rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ export function updateSelectorsSpec(
options: ReducerSchema
): Rule {
return (host: Tree) => {
if (!selectorsSpecSourceFile && options.selectors) {
throw new Error('You try to update selectors specs that do not exist.');
}

const stateProperties = parsePropsToUpdate(options.propsToUpdate);

const changes = createSelectorSpec(
Expand Down
4 changes: 4 additions & 0 deletions src/collection/reducer/rules/update-selectors.rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ export function updateSelectors(
stateDir: StateFilePaths,
options: ReducerSchema
): Rule {
if (!selectorsSourceFile && options.selectors) {
throw new Error('You try to update selectors that do not exist.');
}

return (host: Tree) => {
const stateProperties = parsePropsToUpdate(options.propsToUpdate);

Expand Down

0 comments on commit 23e92d6

Please sign in to comment.