Skip to content

Commit 2483dd9

Browse files
clydincrisbeto
authored andcommitted
refactor(cdk/schematics): use schematics addDependency helper in cdk ng-add
The `addDependency` helper from `@schematics/angular/utility` reduces the amount of custom code to conditionally add the `@angular/cdk` dependency within the package's `ng add` schematic. This also allows remove of some of the additional helper code within the schematic. Unfortunately, not all can be removed as some is used within the unit tests for the schematic.
1 parent cc61697 commit 2483dd9

File tree

3 files changed

+48
-85
lines changed

3 files changed

+48
-85
lines changed

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

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,48 @@ import {Tree} from '@angular-devkit/schematics';
22
import {SchematicTestRunner} from '@angular-devkit/schematics/testing';
33
import {COLLECTION_PATH} from '../paths';
44
import {createTestApp, getFileContent} from '../testing';
5-
import {addPackageToPackageJson} from './package-config';
65

76
interface PackageJson {
87
dependencies: Record<string, string>;
98
}
109

10+
/**
11+
* Sorts the keys of the given object.
12+
* @returns A new object instance with sorted keys
13+
*/
14+
function sortObjectByKeys(obj: Record<string, string>) {
15+
return Object.keys(obj)
16+
.sort()
17+
.reduce(
18+
(result, key) => {
19+
result[key] = obj[key];
20+
return result;
21+
},
22+
{} as Record<string, string>,
23+
);
24+
}
25+
26+
/** Adds a package to the package.json in the given host tree. */
27+
export function addPackageToPackageJson(host: Tree, pkg: string, version: string): Tree {
28+
if (host.exists('package.json')) {
29+
const sourceText = host.read('package.json')!.toString('utf-8');
30+
const json = JSON.parse(sourceText) as PackageJson;
31+
32+
if (!json.dependencies) {
33+
json.dependencies = {};
34+
}
35+
36+
if (!json.dependencies[pkg]) {
37+
json.dependencies[pkg] = version;
38+
json.dependencies = sortObjectByKeys(json.dependencies);
39+
}
40+
41+
host.overwrite('package.json', JSON.stringify(json, null, 2));
42+
}
43+
44+
return host;
45+
}
46+
1147
describe('CDK ng-add', () => {
1248
let runner: SchematicTestRunner;
1349
let appTree: Tree;

src/cdk/schematics/ng-add/index.ts

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,8 @@
66
* found in the LICENSE file at https://angular.dev/license
77
*/
88

9-
import {Rule, SchematicContext, Tree} from '@angular-devkit/schematics';
10-
import {NodePackageInstallTask} from '@angular-devkit/schematics/tasks';
11-
import {addPackageToPackageJson, getPackageVersionFromPackageJson} from './package-config';
9+
import {Rule} from '@angular-devkit/schematics';
10+
import {addDependency, ExistingBehavior} from '@schematics/angular/utility';
1211

1312
/**
1413
* Schematic factory entry-point for the `ng-add` schematic. The ng-add schematic will be
@@ -19,21 +18,14 @@ import {addPackageToPackageJson, getPackageVersionFromPackageJson} from './packa
1918
* this ensures that there will be no error that says that the CDK does not support `ng add`.
2019
*/
2120
export default function (): Rule {
22-
return (host: Tree, context: SchematicContext) => {
23-
// The CLI inserts `@angular/cdk` into the `package.json` before this schematic runs. This
24-
// means that we do not need to insert the CDK into `package.json` files again. In some cases
25-
// though, it could happen that this schematic runs outside of the CLI `ng add` command, or
26-
// the CDK is only listed as a dev dependency. If that is the case, we insert a version based
27-
// on the current build version (substituted version placeholder).
28-
if (getPackageVersionFromPackageJson(host, '@angular/cdk') === null) {
29-
// In order to align the CDK version with other Angular dependencies that are setup by
30-
// `@schematics/angular`, we use tilde instead of caret. This is default for Angular
31-
// dependencies in new CLI projects.
32-
addPackageToPackageJson(host, '@angular/cdk', `~0.0.0-PLACEHOLDER`);
21+
// The CLI inserts `@angular/cdk` into the `package.json` before this schematic runs. This
22+
// means that we do not need to insert the CDK into `package.json` files again. In some cases
23+
// though, it could happen that this schematic runs outside of the CLI `ng add` command, or
24+
// the CDK is only listed as a dev dependency. If that is the case, we insert a version based
25+
// on the current build version (substituted version placeholder).
3326

34-
// Add a task to run the package manager. This is necessary because we updated the
35-
// workspace "package.json" file and we want lock files to reflect the new version range.
36-
context.addTask(new NodePackageInstallTask());
37-
}
38-
};
27+
// In order to align the CDK version with other Angular dependencies that are setup by
28+
// `@schematics/angular`, we use tilde instead of caret. This is default for Angular
29+
// dependencies in new CLI projects.
30+
return addDependency('@angular/cdk', `~0.0.0-PLACEHOLDER`, {existing: ExistingBehavior.Skip});
3931
}

src/cdk/schematics/ng-add/package-config.ts

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

0 commit comments

Comments
 (0)