6
6
* found in the LICENSE file at https://angular.io/license
7
7
*/
8
8
9
- import { chain , noop , Rule , SchematicContext , Tree } from '@angular-devkit/schematics' ;
9
+ import { chain , noop , Rule , SchematicContext , Tree , callRule } from '@angular-devkit/schematics' ;
10
10
import { getProjectFromWorkspace , getProjectStyleFile } from '@angular/cdk/schematics' ;
11
11
import { getWorkspace } from '@schematics/angular/utility/workspace' ;
12
12
import { addRootProvider } from '@schematics/angular/utility' ;
13
13
import { ProjectType } from '@schematics/angular/utility/workspace-models' ;
14
+ import { of as observableOf } from 'rxjs' ;
15
+ import { catchError } from 'rxjs/operators' ;
14
16
import { addFontsToIndex } from './fonts/material-fonts' ;
15
17
import { Schema } from './schema' ;
16
18
import { addThemeToAppStyles , addTypographyClass } from './theming/theming' ;
@@ -28,14 +30,7 @@ export default function (options: Schema): Rule {
28
30
29
31
if ( project . extensions [ 'projectType' ] === ProjectType . Application ) {
30
32
return chain ( [
31
- options . animations === 'excluded'
32
- ? noop ( )
33
- : addRootProvider ( options . project , ( { code, external} ) => {
34
- return code `${ external (
35
- 'provideAnimationsAsync' ,
36
- '@angular/platform-browser/animations/async' ,
37
- ) } (${ options . animations === 'disabled' ? `'noop'` : '' } )`;
38
- } ) ,
33
+ addAnimations ( options ) ,
39
34
addThemeToAppStyles ( options ) ,
40
35
addFontsToIndex ( options ) ,
41
36
addMaterialAppStyles ( options ) ,
@@ -96,3 +91,32 @@ function addMaterialAppStyles(options: Schema) {
96
91
host . commitUpdate ( recorder ) ;
97
92
} ;
98
93
}
94
+
95
+ /** Adds the animations package to the project based on the conffiguration. */
96
+ function addAnimations ( options : Schema ) : Rule {
97
+ return ( host : Tree , context : SchematicContext ) => {
98
+ const animationsRule =
99
+ options . animations === 'excluded'
100
+ ? noop ( )
101
+ : addRootProvider ( options . project , ( { code, external} ) => {
102
+ return code `${ external (
103
+ 'provideAnimationsAsync' ,
104
+ '@angular/platform-browser/animations/async' ,
105
+ ) } (${ options . animations === 'disabled' ? `'noop'` : '' } )`;
106
+ } ) ;
107
+
108
+ // The `addRootProvider` rule can throw in some custom scenarios (see #28640).
109
+ // Add some error handling around it so the setup isn't interrupted.
110
+ return callRule ( animationsRule , host , context ) . pipe (
111
+ catchError ( ( ) => {
112
+ context . logger . error (
113
+ 'Failed to add animations to project. Continuing with the Angular Material setup.' ,
114
+ ) ;
115
+ context . logger . info (
116
+ 'Read more about setting up the animations manually: https://angular.io/guide/animations' ,
117
+ ) ;
118
+ return observableOf ( host ) ;
119
+ } ) ,
120
+ ) ;
121
+ } ;
122
+ }
0 commit comments