-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(angular): add backwards compat support for the ngrx generator (#…
- Loading branch information
1 parent
7bedb39
commit 1c21e0c
Showing
23 changed files
with
373 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
22 changes: 22 additions & 0 deletions
22
...angular/src/generators/ngrx/files/no-inject/__directory__/__fileName__.effects.ts__tmpl__
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { Injectable } from '@angular/core'; | ||
import { createEffect, Actions, ofType } from '@ngrx/effects'; | ||
|
||
import * as <%= className %>Actions from './<%= fileName %>.actions'; | ||
import * as <%= className %>Feature from './<%= fileName %>.reducer'; | ||
|
||
import {switchMap, catchError, of} from 'rxjs'; | ||
|
||
@Injectable() | ||
export class <%= className %>Effects { | ||
init$ = createEffect(() => this.actions$.pipe( | ||
ofType(<%= className %>Actions.init<%= className %>), | ||
switchMap(() => of(<%= className %>Actions.load<%= className %>Success({ <%= propertyName %>: [] }))), | ||
catchError((error) => { | ||
console.error('Error', error); | ||
return of(<%= className %>Actions.load<%= className %>Failure({ error })); | ||
} | ||
) | ||
)); | ||
|
||
constructor(private readonly actions$: Actions) {} | ||
} |
27 changes: 27 additions & 0 deletions
27
.../angular/src/generators/ngrx/files/no-inject/__directory__/__fileName__.facade.ts__tmpl__
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { Injectable } from '@angular/core'; | ||
import { select, Store, Action } from '@ngrx/store'; | ||
|
||
import * as <%= className %>Actions from './<%= fileName %>.actions'; | ||
import * as <%= className %>Feature from './<%= fileName %>.reducer'; | ||
import * as <%= className %>Selectors from './<%= fileName %>.selectors'; | ||
|
||
@Injectable() | ||
export class <%= className %>Facade { | ||
/** | ||
* Combine pieces of state using createSelector, | ||
* and expose them as observables through the facade. | ||
*/ | ||
loaded$ = this.store.pipe(select(<%= className %>Selectors.select<%= className %>Loaded)); | ||
all<%= className %>$ = this.store.pipe(select(<%= className %>Selectors.selectAll<%= className %>)); | ||
selected<%= className %>$ = this.store.pipe(select(<%= className %>Selectors.selectEntity)); | ||
|
||
constructor(private readonly store: Store) {} | ||
|
||
/** | ||
* Use the initialization action to perform one | ||
* or more tasks in your Effects. | ||
*/ | ||
init() { | ||
this.store.dispatch(<%= className %>Actions.init<%= className %>()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
packages/angular/src/generators/ngrx/lib/validate-options.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import type { Tree } from '@nrwl/devkit'; | ||
import { tsquery } from '@phenomnomnominal/tsquery'; | ||
import { lt } from 'semver'; | ||
import { getInstalledAngularVersion } from '../../utils/angular-version-utils'; | ||
import type { NgRxGeneratorOptions } from '../schema'; | ||
|
||
export function validateOptions( | ||
tree: Tree, | ||
options: NgRxGeneratorOptions | ||
): void { | ||
if (!options.module && !options.parent) { | ||
throw new Error('Please provide a value for "--parent"!'); | ||
} | ||
if (options.module && !tree.exists(options.module)) { | ||
throw new Error(`Module does not exist: ${options.module}.`); | ||
} | ||
if (options.parent && !tree.exists(options.parent)) { | ||
throw new Error(`Parent does not exist: ${options.parent}.`); | ||
} | ||
|
||
const angularVersion = getInstalledAngularVersion(tree); | ||
const parentPath = options.parent ?? options.module; | ||
if (parentPath && lt(angularVersion, '14.1.0')) { | ||
const parentContent = tree.read(parentPath, 'utf-8'); | ||
const ast = tsquery.ast(parentContent); | ||
|
||
const NG_MODULE_DECORATOR_SELECTOR = | ||
'ClassDeclaration > Decorator > CallExpression:has(Identifier[name=NgModule])'; | ||
const nodes = tsquery(ast, NG_MODULE_DECORATOR_SELECTOR, { | ||
visitAllChildren: true, | ||
}); | ||
if (nodes.length === 0) { | ||
throw new Error( | ||
`The provided parent path "${parentPath}" does not contain an "NgModule". ` + | ||
'Please make sure to provide a path to an "NgModule" where the state will be registered. ' + | ||
'If you are trying to use a "Routes" definition file (for Standalone API usage), ' + | ||
'please note this is not supported in Angular versions lower than 14.1.0.' | ||
); | ||
} | ||
} | ||
} |
Oops, something went wrong.