Skip to content

chore(schematics): improve messages for ng-add #13840

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/lib/schematics/ng-add/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ describe('ng-add schematic', () => {
expect(styles).not.toContain(defaultPrebuiltThemePath,
'Expected the default prebuilt theme to be not configured.');
expect(console.warn).toHaveBeenCalledWith(
jasmine.stringMatching(/Cannot add.*already a custom theme/));
jasmine.stringMatching(/Could not add the selected theme/));
});

it('should not add a theme file multiple times', () => {
Expand Down
20 changes: 14 additions & 6 deletions src/lib/schematics/ng-add/setup-project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
getProjectStyleFile,
hasNgModuleImport,
} from '@angular/cdk/schematics';
import {red, bold} from 'chalk';
import {red, bold, italic} from 'chalk';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I hadn't thought about this before, but is it a problem that chalk isn't a dependency of our packages? We went through this with parse5

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's technically a dependency but chalk has been used before my changes AFAIK and we just assumed that chalk is always present due to the CLI coming with Chalk.

It might be better to be explicit though. Same applies for the @schematics/angular package we depend on.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, doesn't look like the CLI brings chalk in. It's required by:

    "/@babel/highlight",
    "/inquirer",
    "/postcss",
    "/speed-measure-webpack-plugin",
    "/tslint"

So kind of brittle. But the same applies for tslint (for ng-update) and the aforementioned @schematics/angular package. I can look in a follow-up for some API's the devkit provides for terminal colors (I think I saw something about that)

import {getWorkspace} from '@schematics/angular/utility/config';
import {getAppModulePath} from '@schematics/angular/utility/ng-ast-utils';
import {addFontsToIndex} from './fonts/material-fonts';
Expand Down Expand Up @@ -88,12 +88,20 @@ function addMaterialAppStyles(options: Schema) {
const workspace = getWorkspace(host);
const project = getProjectFromWorkspace(workspace, options.project);
const styleFilePath = getProjectStyleFile(project);
const buffer = host.read(styleFilePath!);

if (!styleFilePath || !buffer) {
return console.warn(`Could not find styles file: "${styleFilePath}". Skipping styles ` +
`generation. Please consider manually adding the "Roboto" font and resetting the ` +
`body margin.`);
if (!styleFilePath) {
console.warn(red(`Could not find the default style file for this project.`));
console.warn(red(`Please consider manually setting up the Roboto font in your CSS.`));
return;
}

const buffer = host.read(styleFilePath);

if (!buffer) {
console.warn(red(`Could not read the default style file within the project ` +
`(${italic(styleFilePath)})`));
console.warn(red(`Please consider manually setting up the Robot font.`));
return;
}

const htmlContent = buffer.toString();
Expand Down
13 changes: 7 additions & 6 deletions src/lib/schematics/ng-add/theming/theming.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {getWorkspace} from '@schematics/angular/utility/config';
import {join} from 'path';
import {Schema} from '../schema';
import {createCustomTheme} from './custom-theme';
import {red, bold} from 'chalk';
import {red, bold, yellow} from 'chalk';

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

if (host.exists(customThemePath)) {
console.warn(red(`Cannot create a custom Angular Material theme because
"${customThemePath}" already exists. Skipping custom theme generation.`));
console.warn(yellow(`Cannot create a custom Angular Material theme because
${bold(customThemePath)} already exists. Skipping custom theme generation.`));
return;
}

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