Skip to content

Commit a027ae5

Browse files
devversionjelbourn
authored andcommitted
fix(ng-add): material version could not be determined (#12751)
Due to the fact that the CLI **only** installs `@angular/material` when running `ng add` and expects us to add the entries to the `package.json`, we cannot use the `VERSION` object from `@angular/material` because the Material package tries to `require` the CDK (which is not present at this point). This results in a runtime exception that will cause the `materialVersion` to be `null`. In order to properly determine the Material version that comes with the `ng add` command, we should gracefully inspect the according `package.json` file of the `@angular/material` or `@angular/cdk` package.
1 parent b9651df commit a027ae5

File tree

2 files changed

+10
-7
lines changed

2 files changed

+10
-7
lines changed

src/lib/schematics/install/index.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import {getProjectStyleFile} from '../utils/project-style-file';
2424
import {addFontsToIndex} from './fonts/material-fonts';
2525
import {Schema} from './schema';
2626
import {addThemeToAppStyles} from './theming/theming';
27-
import {materialVersion, requiredAngularVersion} from './version-names';
27+
import {materialVersion, requiredAngularVersionRange} from './version-names';
2828

2929
/**
3030
* Scaffolds the basics of a Angular Material application, this includes:
@@ -55,10 +55,10 @@ function addMaterialToPackageJson() {
5555
// have the same version tag if possible.
5656
const ngCoreVersionTag = getPackageVersionFromPackageJson(host, '@angular/core');
5757

58-
addPackageToPackageJson(host, 'dependencies', '@angular/cdk', materialVersion);
59-
addPackageToPackageJson(host, 'dependencies', '@angular/material', materialVersion);
58+
addPackageToPackageJson(host, 'dependencies', '@angular/cdk', `^${materialVersion}`);
59+
addPackageToPackageJson(host, 'dependencies', '@angular/material', `^${materialVersion}`);
6060
addPackageToPackageJson(host, 'dependencies', '@angular/animations',
61-
ngCoreVersionTag || requiredAngularVersion);
61+
ngCoreVersionTag || requiredAngularVersionRange);
6262

6363
context.addTask(new NodePackageInstallTask());
6464

src/lib/schematics/install/version-names.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,16 @@ export const materialVersion =
1111
loadPackageVersionGracefully('@angular/cdk') ||
1212
loadPackageVersionGracefully('@angular/material');
1313

14-
/** Angular version that is needed for the Material version that comes with the schematics. */
15-
export const requiredAngularVersion = '0.0.0-NG';
14+
/**
15+
* Range of Angular versions that can be used together with the Angular Material version
16+
* that provides these schematics.
17+
*/
18+
export const requiredAngularVersionRange = '0.0.0-NG';
1619

1720
/** Loads the full version from the given Angular package gracefully. */
1821
function loadPackageVersionGracefully(packageName: string): string | null {
1922
try {
20-
return require(packageName).VERSION.full;
23+
return require(`${packageName}/package.json`).version;
2124
} catch {
2225
return null;
2326
}

0 commit comments

Comments
 (0)