Skip to content

Commit 64163ad

Browse files
devversionjelbourn
authored andcommitted
chore(schematics): improve messages for ng-add (#13840)
In order to make the "failure" or "warning" messages with `ng-add` look more friendly and structured, we manually wrap the messages. Additionally `chalk` is being used more often in order to make it clear if something failed/passed or is just a warning.
1 parent 7cf821b commit 64163ad

File tree

3 files changed

+22
-13
lines changed

3 files changed

+22
-13
lines changed

src/lib/schematics/ng-add/index.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ describe('ng-add schematic', () => {
257257
expect(styles).not.toContain(defaultPrebuiltThemePath,
258258
'Expected the default prebuilt theme to be not configured.');
259259
expect(console.warn).toHaveBeenCalledWith(
260-
jasmine.stringMatching(/Cannot add.*already a custom theme/));
260+
jasmine.stringMatching(/Could not add the selected theme/));
261261
});
262262

263263
it('should not add a theme file multiple times', () => {

src/lib/schematics/ng-add/setup-project.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import {
1414
getProjectStyleFile,
1515
hasNgModuleImport,
1616
} from '@angular/cdk/schematics';
17-
import {red, bold} from 'chalk';
17+
import {red, bold, italic} from 'chalk';
1818
import {getWorkspace} from '@schematics/angular/utility/config';
1919
import {getAppModulePath} from '@schematics/angular/utility/ng-ast-utils';
2020
import {addFontsToIndex} from './fonts/material-fonts';
@@ -88,12 +88,20 @@ function addMaterialAppStyles(options: Schema) {
8888
const workspace = getWorkspace(host);
8989
const project = getProjectFromWorkspace(workspace, options.project);
9090
const styleFilePath = getProjectStyleFile(project);
91-
const buffer = host.read(styleFilePath!);
9291

93-
if (!styleFilePath || !buffer) {
94-
return console.warn(`Could not find styles file: "${styleFilePath}". Skipping styles ` +
95-
`generation. Please consider manually adding the "Roboto" font and resetting the ` +
96-
`body margin.`);
92+
if (!styleFilePath) {
93+
console.warn(red(`Could not find the default style file for this project.`));
94+
console.warn(red(`Please consider manually setting up the Roboto font in your CSS.`));
95+
return;
96+
}
97+
98+
const buffer = host.read(styleFilePath);
99+
100+
if (!buffer) {
101+
console.warn(red(`Could not read the default style file within the project ` +
102+
`(${italic(styleFilePath)})`));
103+
console.warn(red(`Please consider manually setting up the Robot font.`));
104+
return;
97105
}
98106

99107
const htmlContent = buffer.toString();

src/lib/schematics/ng-add/theming/theming.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import {getWorkspace} from '@schematics/angular/utility/config';
1919
import {join} from 'path';
2020
import {Schema} from '../schema';
2121
import {createCustomTheme} from './custom-theme';
22-
import {red, bold} from 'chalk';
22+
import {red, bold, yellow} from 'chalk';
2323

2424
/** Path segment that can be found in paths that refer to a prebuilt theme. */
2525
const prebuiltThemePathSegment = '@angular/material/prebuilt-themes';
@@ -69,8 +69,8 @@ function insertCustomTheme(project: WorkspaceProject, projectName: string, host:
6969
const customThemePath = normalize(join(project.sourceRoot, defaultCustomThemeFilename));
7070

7171
if (host.exists(customThemePath)) {
72-
console.warn(red(`Cannot create a custom Angular Material theme because
73-
"${customThemePath}" already exists. Skipping custom theme generation.`));
72+
console.warn(yellow(`Cannot create a custom Angular Material theme because
73+
${bold(customThemePath)} already exists. Skipping custom theme generation.`));
7474
return;
7575
}
7676

@@ -119,9 +119,10 @@ function addThemeStyleToTarget(project: WorkspaceProject, targetName: string, ho
119119
// theme because these files can contain custom styles, while prebuilt themes are
120120
// always packaged and considered replaceable.
121121
if (stylePath.includes(defaultCustomThemeFilename)) {
122-
console.warn(red(`Cannot add "${bold(assetPath)} to the CLI project configuration ` +
123-
`because there is already a custom theme file referenced. Please manually add ` +
124-
`the "${bold(assetPath)}" style file to your configuration.`));
122+
console.warn(red(`Could not add the selected theme to the CLI project configuration ` +
123+
`because there is already a custom theme file referenced.`));
124+
console.warn(red(`Please manually add the following style file to your configuration:`));
125+
console.warn(yellow(` ${bold(assetPath)}`));
125126
return;
126127
} else if (stylePath.includes(prebuiltThemePathSegment)) {
127128
targetOptions.styles.splice(index, 1);

0 commit comments

Comments
 (0)