66 * found in the LICENSE file at https://angular.io/license
77 */
88
9- import { chain , noop , Rule , SchematicContext , Tree } from '@angular-devkit/schematics' ;
9+ import { chain , noop , Rule , SchematicContext , Tree , callRule } from '@angular-devkit/schematics' ;
1010import { getProjectFromWorkspace , getProjectStyleFile } from '@angular/cdk/schematics' ;
1111import { getWorkspace } from '@schematics/angular/utility/workspace' ;
1212import { addRootProvider } from '@schematics/angular/utility' ;
1313import { ProjectType } from '@schematics/angular/utility/workspace-models' ;
14+ import { of as observableOf } from 'rxjs' ;
15+ import { catchError } from 'rxjs/operators' ;
1416import { addFontsToIndex } from './fonts/material-fonts' ;
1517import { Schema } from './schema' ;
1618import { addThemeToAppStyles , addTypographyClass } from './theming/theming' ;
@@ -28,14 +30,7 @@ export default function (options: Schema): Rule {
2830
2931 if ( project . extensions [ 'projectType' ] === ProjectType . Application ) {
3032 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 ) ,
3934 addThemeToAppStyles ( options ) ,
4035 addFontsToIndex ( options ) ,
4136 addMaterialAppStyles ( options ) ,
@@ -96,3 +91,32 @@ function addMaterialAppStyles(options: Schema) {
9691 host . commitUpdate ( recorder ) ;
9792 } ;
9893}
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