66 * found in the LICENSE file at https://angular.io/license
77 */
88
9- import { normalize } from '@angular-devkit/core' ;
9+ import { normalize , logging } from '@angular-devkit/core' ;
1010import { WorkspaceProject , WorkspaceSchema } from '@angular-devkit/core/src/experimental/workspace' ;
11- import { SchematicsException , Tree } from '@angular-devkit/schematics' ;
11+ import { Rule , SchematicContext , SchematicsException , Tree } from '@angular-devkit/schematics' ;
1212import {
1313 addBodyClass ,
1414 defaultTargetBuilders ,
@@ -19,7 +19,6 @@ import {
1919} from '@angular/cdk/schematics' ;
2020import { InsertChange } from '@schematics/angular/utility/change' ;
2121import { getWorkspace } from '@schematics/angular/utility/config' ;
22- import chalk from 'chalk' ;
2322import { join } from 'path' ;
2423import { Schema } from '../schema' ;
2524import { createCustomTheme } from './create-custom-theme' ;
@@ -31,16 +30,16 @@ const prebuiltThemePathSegment = '@angular/material/prebuilt-themes';
3130const defaultCustomThemeFilename = 'custom-theme.scss' ;
3231
3332/** Add pre-built styles to the main project style file. */
34- export function addThemeToAppStyles ( options : Schema ) : ( host : Tree ) => Tree {
35- return function ( host : Tree ) : Tree {
33+ export function addThemeToAppStyles ( options : Schema ) : Rule {
34+ return function ( host : Tree , context : SchematicContext ) : Tree {
3635 const workspace = getWorkspace ( host ) ;
3736 const project = getProjectFromWorkspace ( workspace , options . project ) ;
3837 const themeName = options . theme || 'indigo-pink' ;
3938
4039 if ( themeName === 'custom' ) {
41- insertCustomTheme ( project , options . project , host , workspace ) ;
40+ insertCustomTheme ( project , options . project , host , workspace , context . logger ) ;
4241 } else {
43- insertPrebuiltTheme ( project , host , themeName , workspace ) ;
42+ insertPrebuiltTheme ( project , host , themeName , workspace , context . logger ) ;
4443 }
4544
4645 return host ;
@@ -69,7 +68,7 @@ export function addTypographyClass(options: Schema): (host: Tree) => Tree {
6968 * Scss file for the custom theme will be created.
7069 */
7170function insertCustomTheme ( project : WorkspaceProject , projectName : string , host : Tree ,
72- workspace : WorkspaceSchema ) {
71+ workspace : WorkspaceSchema , logger : logging . LoggerApi ) {
7372
7473 const stylesPath = getProjectStyleFile ( project , 'scss' ) ;
7574 const themeContent = createCustomTheme ( projectName ) ;
@@ -85,13 +84,13 @@ function insertCustomTheme(project: WorkspaceProject, projectName: string, host:
8584 const customThemePath = normalize ( join ( project . sourceRoot , defaultCustomThemeFilename ) ) ;
8685
8786 if ( host . exists ( customThemePath ) ) {
88- console . warn ( chalk . yellow ( `Cannot create a custom Angular Material theme because
89- ${ chalk . bold ( customThemePath ) } already exists. Skipping custom theme generation.` ) ) ;
87+ logger . warn ( `Cannot create a custom Angular Material theme because
88+ ${ customThemePath } already exists. Skipping custom theme generation.` ) ;
9089 return ;
9190 }
9291
9392 host . create ( customThemePath , themeContent ) ;
94- addThemeStyleToTarget ( project , 'build' , host , customThemePath , workspace ) ;
93+ addThemeStyleToTarget ( project , 'build' , host , customThemePath , workspace , logger ) ;
9594 return ;
9695 }
9796
@@ -104,20 +103,21 @@ function insertCustomTheme(project: WorkspaceProject, projectName: string, host:
104103
105104/** Insert a pre-built theme into the angular.json file. */
106105function insertPrebuiltTheme ( project : WorkspaceProject , host : Tree , theme : string ,
107- workspace : WorkspaceSchema ) {
106+ workspace : WorkspaceSchema , logger : logging . LoggerApi ) {
108107
109108 // Path needs to be always relative to the `package.json` or workspace root.
110109 const themePath = `./node_modules/@angular/material/prebuilt-themes/${ theme } .css` ;
111110
112- addThemeStyleToTarget ( project , 'build' , host , themePath , workspace ) ;
113- addThemeStyleToTarget ( project , 'test' , host , themePath , workspace ) ;
111+ addThemeStyleToTarget ( project , 'build' , host , themePath , workspace , logger ) ;
112+ addThemeStyleToTarget ( project , 'test' , host , themePath , workspace , logger ) ;
114113}
115114
116115/** Adds a theming style entry to the given project target options. */
117116function addThemeStyleToTarget ( project : WorkspaceProject , targetName : 'test' | 'build' , host : Tree ,
118- assetPath : string , workspace : WorkspaceSchema ) {
117+ assetPath : string , workspace : WorkspaceSchema ,
118+ logger : logging . LoggerApi ) {
119119 // Do not update the builder options in case the target does not use the default CLI builder.
120- if ( ! validateDefaultTargetBuilder ( project , targetName ) ) {
120+ if ( ! validateDefaultTargetBuilder ( project , targetName , logger ) ) {
121121 return ;
122122 }
123123
@@ -139,11 +139,10 @@ function addThemeStyleToTarget(project: WorkspaceProject, targetName: 'test' | '
139139 // theme because these files can contain custom styles, while prebuilt themes are
140140 // always packaged and considered replaceable.
141141 if ( stylePath . includes ( defaultCustomThemeFilename ) ) {
142- console . warn ( chalk . red ( `Could not add the selected theme to the CLI project ` +
143- `configuration because there is already a custom theme file referenced.` ) ) ;
144- console . warn ( chalk . red (
145- `Please manually add the following style file to your configuration:` ) ) ;
146- console . warn ( chalk . yellow ( ` ${ chalk . bold ( assetPath ) } ` ) ) ;
142+ logger . error ( `Could not add the selected theme to the CLI project ` +
143+ `configuration because there is already a custom theme file referenced.` ) ;
144+ logger . info ( `Please manually add the following style file to your configuration:` ) ;
145+ logger . info ( ` ${ assetPath } ` ) ;
147146 return ;
148147 } else if ( stylePath . includes ( prebuiltThemePathSegment ) ) {
149148 targetOptions . styles . splice ( index , 1 ) ;
@@ -161,7 +160,8 @@ function addThemeStyleToTarget(project: WorkspaceProject, targetName: 'test' | '
161160 * provided by the Angular CLI. If the configured builder does not match the default builder,
162161 * this function can either throw or just show a warning.
163162 */
164- function validateDefaultTargetBuilder ( project : WorkspaceProject , targetName : 'build' | 'test' ) {
163+ function validateDefaultTargetBuilder ( project : WorkspaceProject , targetName : 'build' | 'test' ,
164+ logger : logging . LoggerApi ) {
165165 const defaultBuilder = defaultTargetBuilders [ targetName ] ;
166166 const targetConfig = project . architect && project . architect [ targetName ] ||
167167 project . targets && project . targets [ targetName ] ;
@@ -178,7 +178,9 @@ function validateDefaultTargetBuilder(project: WorkspaceProject, targetName: 'bu
178178 `"${ targetName } ". The Angular Material schematics cannot add a theme to the workspace ` +
179179 `configuration if the builder has been changed.` ) ;
180180 } else if ( ! isDefaultBuilder ) {
181- console . warn ( `Your project is not using the default builders for "${ targetName } ". This ` +
181+ // for non-build targets we gracefully report the error without actually aborting the
182+ // setup schematic. This is because a theme is not mandatory for running tests.
183+ logger . warn ( `Your project is not using the default builders for "${ targetName } ". This ` +
182184 `means that we cannot add the configured theme to the "${ targetName } " target.` ) ;
183185 }
184186
0 commit comments