Skip to content

Commit b169320

Browse files
authoredMar 4, 2024··
fix(material/schematics): don't interrupt ng add if adding the animations module fails (#28675)
In some custom scenarios adding the animations module can fail and the code that does it is outside of our control. These changes add some error handling so the rest of the schematic isn't interrupted. Fixes #28640.
1 parent 979e903 commit b169320

File tree

2 files changed

+34
-9
lines changed

2 files changed

+34
-9
lines changed
 

‎src/material/schematics/BUILD.bazel

+1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ ts_library(
4848
# TODO(devversion): Only include jasmine for test sources (See: tsconfig types).
4949
"@npm//@types/jasmine",
5050
"@npm//@types/node",
51+
"@npm//rxjs",
5152
"@npm//tslint",
5253
"@npm//typescript",
5354
],

‎src/material/schematics/ng-add/setup-project.ts

+33-9
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@
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';
1010
import {getProjectFromWorkspace, getProjectStyleFile} from '@angular/cdk/schematics';
1111
import {getWorkspace} from '@schematics/angular/utility/workspace';
1212
import {addRootProvider} from '@schematics/angular/utility';
1313
import {ProjectType} from '@schematics/angular/utility/workspace-models';
14+
import {of as observableOf} from 'rxjs';
15+
import {catchError} from 'rxjs/operators';
1416
import {addFontsToIndex} from './fonts/material-fonts';
1517
import {Schema} from './schema';
1618
import {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

Comments
 (0)
Please sign in to comment.