Skip to content

Commit

Permalink
fix(material/schematics): don't interrupt ng add if adding the animat…
Browse files Browse the repository at this point in the history
…ions 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.

(cherry picked from commit b169320)
  • Loading branch information
crisbeto committed Mar 4, 2024
1 parent 65aaaf3 commit 6d8160c
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 9 deletions.
1 change: 1 addition & 0 deletions src/material/schematics/BUILD.bazel
Expand Up @@ -48,6 +48,7 @@ ts_library(
# TODO(devversion): Only include jasmine for test sources (See: tsconfig types).
"@npm//@types/jasmine",
"@npm//@types/node",
"@npm//rxjs",
"@npm//tslint",
"@npm//typescript",
],
Expand Down
42 changes: 33 additions & 9 deletions src/material/schematics/ng-add/setup-project.ts
Expand Up @@ -6,11 +6,13 @@
* found in the LICENSE file at https://angular.io/license
*/

import {chain, noop, Rule, SchematicContext, Tree} from '@angular-devkit/schematics';
import {chain, noop, Rule, SchematicContext, Tree, callRule} from '@angular-devkit/schematics';
import {getProjectFromWorkspace, getProjectStyleFile} from '@angular/cdk/schematics';
import {getWorkspace} from '@schematics/angular/utility/workspace';
import {addRootProvider} from '@schematics/angular/utility';
import {ProjectType} from '@schematics/angular/utility/workspace-models';
import {of as observableOf} from 'rxjs';
import {catchError} from 'rxjs/operators';
import {addFontsToIndex} from './fonts/material-fonts';
import {Schema} from './schema';
import {addThemeToAppStyles, addTypographyClass} from './theming/theming';
Expand All @@ -28,14 +30,7 @@ export default function (options: Schema): Rule {

if (project.extensions['projectType'] === ProjectType.Application) {
return chain([
options.animations === 'excluded'
? noop()
: addRootProvider(options.project, ({code, external}) => {
return code`${external(
'provideAnimationsAsync',
'@angular/platform-browser/animations/async',
)}(${options.animations === 'disabled' ? `'noop'` : ''})`;
}),
addAnimations(options),
addThemeToAppStyles(options),
addFontsToIndex(options),
addMaterialAppStyles(options),
Expand Down Expand Up @@ -96,3 +91,32 @@ function addMaterialAppStyles(options: Schema) {
host.commitUpdate(recorder);
};
}

/** Adds the animations package to the project based on the conffiguration. */
function addAnimations(options: Schema): Rule {
return (host: Tree, context: SchematicContext) => {
const animationsRule =
options.animations === 'excluded'
? noop()
: addRootProvider(options.project, ({code, external}) => {
return code`${external(
'provideAnimationsAsync',
'@angular/platform-browser/animations/async',
)}(${options.animations === 'disabled' ? `'noop'` : ''})`;
});

// The `addRootProvider` rule can throw in some custom scenarios (see #28640).
// Add some error handling around it so the setup isn't interrupted.
return callRule(animationsRule, host, context).pipe(
catchError(() => {
context.logger.error(
'Failed to add animations to project. Continuing with the Angular Material setup.',
);
context.logger.info(
'Read more about setting up the animations manually: https://angular.io/guide/animations',
);
return observableOf(host);
}),
);
};
}

0 comments on commit 6d8160c

Please sign in to comment.