Skip to content

Commit 4ecde45

Browse files
devversionjosephperrott
authored andcommitted
refactor(schematics): detect package version from node modules (#12535)
1 parent 3357e70 commit 4ecde45

File tree

7 files changed

+65
-24
lines changed

7 files changed

+65
-24
lines changed

src/lib/schematics/BUILD.bazel

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package(default_visibility = ["//visibility:public"])
22

33
load("@build_bazel_rules_typescript//:defs.bzl", "ts_library")
44
load("@build_bazel_rules_nodejs//:defs.bzl", "npm_package", "jasmine_node_test")
5+
load("//:packages.bzl", "VERSION_PLACEHOLDER_REPLACEMENTS")
56

67
filegroup(
78
name = "schematics_assets",
@@ -20,6 +21,7 @@ npm_package(
2021
name = "npm_package",
2122
srcs = [":schematics_assets"],
2223
deps = [":schematics"],
24+
replacements = VERSION_PLACEHOLDER_REPLACEMENTS,
2325
)
2426

2527
### Testing rules

src/lib/schematics/install/index.spec.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {SchematicTestRunner} from '@angular-devkit/schematics/testing';
33
import {getProjectFromWorkspace} from '../utils/get-project';
44
import {getFileContent} from '@schematics/angular/utility/test';
55
import {collectionPath, createTestApp} from '../utils/testing';
6-
import {getConfig, getAppFromConfig, getWorkspace} from '@schematics/angular/utility/config';
6+
import {getWorkspace} from '@schematics/angular/utility/config';
77
import {getIndexHtmlPath} from '../utils/ast';
88
import {normalize} from '@angular-devkit/core';
99

@@ -19,9 +19,12 @@ describe('material-install-schematic', () => {
1919
it('should update package.json', () => {
2020
const tree = runner.runSchematic('ng-add', {}, appTree);
2121
const packageJson = JSON.parse(getFileContent(tree, '/package.json'));
22+
const angularCoreVersion = packageJson.dependencies['@angular/core'];
2223

2324
expect(packageJson.dependencies['@angular/material']).toBeDefined();
2425
expect(packageJson.dependencies['@angular/cdk']).toBeDefined();
26+
expect(packageJson.dependencies['@angular/animations']).toBe(angularCoreVersion,
27+
'Expected the @angular/animations package to have the same version as @angular/core.');
2528
});
2629

2730
it('should add default theme', () => {

src/lib/schematics/install/index.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@ import {
1515
Tree,
1616
} from '@angular-devkit/schematics';
1717
import {NodePackageInstallTask} from '@angular-devkit/schematics/tasks';
18-
import {addModuleImportToRootModule, getStylesPath} from '../utils/ast';
1918
import {InsertChange} from '@schematics/angular/utility/change';
2019
import {getWorkspace} from '@schematics/angular/utility/config';
20+
import {materialVersion, requiredAngularVersion} from './version-names';
21+
import {addModuleImportToRootModule, getStylesPath} from '../utils/ast';
2122
import {getProjectFromWorkspace} from '../utils/get-project';
2223
import {addHeadLink} from '../utils/html';
23-
import {angularVersion, materialVersion} from '../utils/lib-versions';
24-
import {addPackageToPackageJson} from '../utils/package';
24+
import {addPackageToPackageJson, getPackageVersionFromPackageJson} from '../utils/package';
2525
import {Schema} from './schema';
2626
import {addThemeToAppStyles} from './theming';
2727
import * as parse5 from 'parse5';
@@ -30,7 +30,7 @@ import * as parse5 from 'parse5';
3030
* Scaffolds the basics of a Angular Material application, this includes:
3131
* - Add Packages to package.json
3232
* - Adds pre-built themes to styles.ext
33-
* - Adds Browser Animation to app.momdule
33+
* - Adds Browser Animation to app.module
3434
*/
3535
export default function(options: Schema): Rule {
3636
if (!parse5) {
@@ -47,13 +47,21 @@ export default function(options: Schema): Rule {
4747
]);
4848
}
4949

50-
/** Add material, cdk, annimations to package.json if not already present. */
50+
/** Add material, cdk, animations to package.json if not already present. */
5151
function addMaterialToPackageJson() {
5252
return (host: Tree, context: SchematicContext) => {
53+
// Version tag of the `@angular/core` dependency that has been loaded from the `package.json`
54+
// of the CLI project. This tag should be preferred because all Angular dependencies should
55+
// have the same version tag if possible.
56+
const ngCoreVersionTag = getPackageVersionFromPackageJson(host, '@angular/core');
57+
5358
addPackageToPackageJson(host, 'dependencies', '@angular/cdk', materialVersion);
5459
addPackageToPackageJson(host, 'dependencies', '@angular/material', materialVersion);
55-
addPackageToPackageJson(host, 'dependencies', '@angular/animations', angularVersion);
60+
addPackageToPackageJson(host, 'dependencies', '@angular/animations',
61+
ngCoreVersionTag || requiredAngularVersion);
62+
5663
context.addTask(new NodePackageInstallTask());
64+
5765
return host;
5866
};
5967
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* @license
3+
* Copyright Google LLC All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
9+
/** Name of the Material version that is shipped together with the schematics. */
10+
export const materialVersion =
11+
loadPackageVersionGracefully('@angular/cdk') ||
12+
loadPackageVersionGracefully('@angular/material');
13+
14+
/** Angular version that is needed for the Material version that comes with the schematics. */
15+
export const requiredAngularVersion = '0.0.0-NG';
16+
17+
/** Loads the full version from the given Angular package gracefully. */
18+
function loadPackageVersionGracefully(packageName: string): string | null {
19+
try {
20+
return require(packageName).VERSION.full;
21+
} catch {
22+
return null;
23+
}
24+
}

src/lib/schematics/utils/lib-versions.ts

Lines changed: 0 additions & 10 deletions
This file was deleted.

src/lib/schematics/utils/package.ts

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,10 @@
88

99
import {Tree} from '@angular-devkit/schematics';
1010

11-
/**
12-
* Adds a package to the package.json
13-
*/
14-
export function addPackageToPackageJson(
15-
host: Tree, type: string, pkg: string, version: string): Tree {
11+
/** Adds a package to the package.json in the given host tree. */
12+
export function addPackageToPackageJson(host: Tree, type: string, pkg: string,
13+
version: string): Tree {
14+
1615
if (host.exists('package.json')) {
1716
const sourceText = host.read('package.json')!.toString('utf-8');
1817
const json = JSON.parse(sourceText);
@@ -29,3 +28,18 @@ export function addPackageToPackageJson(
2928

3029
return host;
3130
}
31+
32+
/** Gets the version of the specified package by looking at the package.json in the given tree. */
33+
export function getPackageVersionFromPackageJson(tree: Tree, name: string): string | null {
34+
if (!tree.exists('package.json')) {
35+
return null;
36+
}
37+
38+
const packageJson = JSON.parse(tree.read('package.json')!.toString('utf8'));
39+
40+
if (packageJson.dependencies && packageJson.dependencies[name]) {
41+
return packageJson.dependencies[name];
42+
}
43+
44+
return null;
45+
}

tools/gulp/tasks/material-release.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,12 @@ task('material:prepare-release', sequenceTask(
6464
));
6565

6666
task('material:copy-schematics', () => {
67-
src(schematicsGlobs).pipe(dest(join(releasePath, 'schematics')));
67+
return src(schematicsGlobs).pipe(dest(join(releasePath, 'schematics')));
6868
});
6969

7070
/** Copies all prebuilt themes into the release package under `prebuilt-themes/` */
7171
task('material:copy-prebuilt-themes', () => {
72-
src(prebuiltThemeGlob)
72+
return src(prebuiltThemeGlob)
7373
.pipe(gulpRename({dirname: ''}))
7474
.pipe(dest(join(releasePath, 'prebuilt-themes')));
7575
});

0 commit comments

Comments
 (0)