6
6
* found in the LICENSE file at https://angular.io/license
7
7
*/
8
8
9
- import { normalize } from '@angular-devkit/core' ;
9
+ import { normalize , logging } from '@angular-devkit/core' ;
10
10
import { 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' ;
12
12
import {
13
13
addBodyClass ,
14
14
defaultTargetBuilders ,
@@ -19,7 +19,6 @@ import {
19
19
} from '@angular/cdk/schematics' ;
20
20
import { InsertChange } from '@schematics/angular/utility/change' ;
21
21
import { getWorkspace } from '@schematics/angular/utility/config' ;
22
- import chalk from 'chalk' ;
23
22
import { join } from 'path' ;
24
23
import { Schema } from '../schema' ;
25
24
import { createCustomTheme } from './create-custom-theme' ;
@@ -31,16 +30,16 @@ const prebuiltThemePathSegment = '@angular/material/prebuilt-themes';
31
30
const defaultCustomThemeFilename = 'custom-theme.scss' ;
32
31
33
32
/** 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 {
36
35
const workspace = getWorkspace ( host ) ;
37
36
const project = getProjectFromWorkspace ( workspace , options . project ) ;
38
37
const themeName = options . theme || 'indigo-pink' ;
39
38
40
39
if ( themeName === 'custom' ) {
41
- insertCustomTheme ( project , options . project , host , workspace ) ;
40
+ insertCustomTheme ( project , options . project , host , workspace , context . logger ) ;
42
41
} else {
43
- insertPrebuiltTheme ( project , host , themeName , workspace ) ;
42
+ insertPrebuiltTheme ( project , host , themeName , workspace , context . logger ) ;
44
43
}
45
44
46
45
return host ;
@@ -69,7 +68,7 @@ export function addTypographyClass(options: Schema): (host: Tree) => Tree {
69
68
* Scss file for the custom theme will be created.
70
69
*/
71
70
function insertCustomTheme ( project : WorkspaceProject , projectName : string , host : Tree ,
72
- workspace : WorkspaceSchema ) {
71
+ workspace : WorkspaceSchema , logger : logging . LoggerApi ) {
73
72
74
73
const stylesPath = getProjectStyleFile ( project , 'scss' ) ;
75
74
const themeContent = createCustomTheme ( projectName ) ;
@@ -85,13 +84,13 @@ function insertCustomTheme(project: WorkspaceProject, projectName: string, host:
85
84
const customThemePath = normalize ( join ( project . sourceRoot , defaultCustomThemeFilename ) ) ;
86
85
87
86
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.` ) ;
90
89
return ;
91
90
}
92
91
93
92
host . create ( customThemePath , themeContent ) ;
94
- addThemeStyleToTarget ( project , 'build' , host , customThemePath , workspace ) ;
93
+ addThemeStyleToTarget ( project , 'build' , host , customThemePath , workspace , logger ) ;
95
94
return ;
96
95
}
97
96
@@ -104,20 +103,21 @@ function insertCustomTheme(project: WorkspaceProject, projectName: string, host:
104
103
105
104
/** Insert a pre-built theme into the angular.json file. */
106
105
function insertPrebuiltTheme ( project : WorkspaceProject , host : Tree , theme : string ,
107
- workspace : WorkspaceSchema ) {
106
+ workspace : WorkspaceSchema , logger : logging . LoggerApi ) {
108
107
109
108
// Path needs to be always relative to the `package.json` or workspace root.
110
109
const themePath = `./node_modules/@angular/material/prebuilt-themes/${ theme } .css` ;
111
110
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 ) ;
114
113
}
115
114
116
115
/** Adds a theming style entry to the given project target options. */
117
116
function addThemeStyleToTarget ( project : WorkspaceProject , targetName : 'test' | 'build' , host : Tree ,
118
- assetPath : string , workspace : WorkspaceSchema ) {
117
+ assetPath : string , workspace : WorkspaceSchema ,
118
+ logger : logging . LoggerApi ) {
119
119
// 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 ) ) {
121
121
return ;
122
122
}
123
123
@@ -139,11 +139,10 @@ function addThemeStyleToTarget(project: WorkspaceProject, targetName: 'test' | '
139
139
// theme because these files can contain custom styles, while prebuilt themes are
140
140
// always packaged and considered replaceable.
141
141
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 } ` ) ;
147
146
return ;
148
147
} else if ( stylePath . includes ( prebuiltThemePathSegment ) ) {
149
148
targetOptions . styles . splice ( index , 1 ) ;
@@ -161,7 +160,8 @@ function addThemeStyleToTarget(project: WorkspaceProject, targetName: 'test' | '
161
160
* provided by the Angular CLI. If the configured builder does not match the default builder,
162
161
* this function can either throw or just show a warning.
163
162
*/
164
- function validateDefaultTargetBuilder ( project : WorkspaceProject , targetName : 'build' | 'test' ) {
163
+ function validateDefaultTargetBuilder ( project : WorkspaceProject , targetName : 'build' | 'test' ,
164
+ logger : logging . LoggerApi ) {
165
165
const defaultBuilder = defaultTargetBuilders [ targetName ] ;
166
166
const targetConfig = project . architect && project . architect [ targetName ] ||
167
167
project . targets && project . targets [ targetName ] ;
@@ -178,7 +178,9 @@ function validateDefaultTargetBuilder(project: WorkspaceProject, targetName: 'bu
178
178
`"${ targetName } ". The Angular Material schematics cannot add a theme to the workspace ` +
179
179
`configuration if the builder has been changed.` ) ;
180
180
} 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 ` +
182
184
`means that we cannot add the configured theme to the "${ targetName } " target.` ) ;
183
185
}
184
186
0 commit comments