Skip to content

Commit 9723844

Browse files
clydinalan-agius4
authored andcommitted
refactor(@schematics/angular): make application and library deps declarative
Moves the dependency information for the `application` and `library` schematics to module-level constants. This makes the `addDependenciesToPackageJson` functions in both schematics more declarative and easier to read and maintain.
1 parent 6713cd1 commit 9723844

File tree

2 files changed

+22
-31
lines changed

2 files changed

+22
-31
lines changed

packages/schematics/angular/application/index.ts

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import { JsonObject, join, normalize } from '@angular-devkit/core';
1010
import {
1111
MergeStrategy,
1212
Rule,
13-
SchematicContext,
1413
Tree,
1514
apply,
1615
applyTemplates,
@@ -23,7 +22,6 @@ import {
2322
strings,
2423
url,
2524
} from '@angular-devkit/schematics';
26-
import { NodePackageInstallTask } from '@angular-devkit/schematics/tasks';
2725
import { Schema as ComponentOptions } from '../component/schema';
2826
import {
2927
DependencyType,
@@ -38,6 +36,12 @@ import { getWorkspace, updateWorkspace } from '../utility/workspace';
3836
import { Builders, ProjectType } from '../utility/workspace-models';
3937
import { Schema as ApplicationOptions, Style } from './schema';
4038

39+
const APPLICATION_DEV_DEPENDENCIES = [
40+
{ name: '@angular/compiler-cli', version: latestVersions.Angular },
41+
{ name: '@angular/build', version: latestVersions.AngularBuild },
42+
{ name: 'typescript', version: latestVersions['typescript'] },
43+
];
44+
4145
function addTsProjectReference(...paths: string[]) {
4246
return (host: Tree) => {
4347
if (!host.exists('tsconfig.json')) {
@@ -136,23 +140,13 @@ export default function (options: ApplicationOptions): Rule {
136140
}
137141

138142
function addDependenciesToPackageJson(options: ApplicationOptions): Rule {
139-
const rules: Rule[] = [
140-
addDependency('@angular/compiler-cli', latestVersions.Angular, {
141-
type: DependencyType.Dev,
142-
existing: ExistingBehavior.Skip,
143-
install: options.skipInstall ? InstallBehavior.None : InstallBehavior.Auto,
144-
}),
145-
addDependency('@angular/build', latestVersions.AngularBuild, {
146-
type: DependencyType.Dev,
147-
existing: ExistingBehavior.Skip,
148-
install: options.skipInstall ? InstallBehavior.None : InstallBehavior.Auto,
149-
}),
150-
addDependency('typescript', latestVersions['typescript'], {
143+
const rules: Rule[] = APPLICATION_DEV_DEPENDENCIES.map((dependency) =>
144+
addDependency(dependency.name, dependency.version, {
151145
type: DependencyType.Dev,
152146
existing: ExistingBehavior.Skip,
153147
install: options.skipInstall ? InstallBehavior.None : InstallBehavior.Auto,
154148
}),
155-
];
149+
);
156150

157151
if (!options.zoneless) {
158152
rules.push(

packages/schematics/angular/library/index.ts

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,13 @@ import { getWorkspace, updateWorkspace } from '../utility/workspace';
3535
import { Builders, ProjectType } from '../utility/workspace-models';
3636
import { Schema as LibraryOptions } from './schema';
3737

38+
const LIBRARY_DEV_DEPENDENCIES = [
39+
{ name: '@angular/compiler-cli', version: latestVersions.Angular },
40+
{ name: '@angular/build', version: latestVersions.AngularBuild },
41+
{ name: 'ng-packagr', version: latestVersions.NgPackagr },
42+
{ name: 'typescript', version: latestVersions['typescript'] },
43+
];
44+
3845
function updateTsConfig(packageName: string, ...paths: string[]) {
3946
return (host: Tree) => {
4047
if (!host.exists('tsconfig.json')) {
@@ -65,26 +72,16 @@ function addTsProjectReference(...paths: string[]) {
6572

6673
function addDependenciesToPackageJson(): Rule {
6774
return chain([
68-
addDependency('@angular/compiler-cli', latestVersions.Angular, {
69-
type: DependencyType.Dev,
70-
existing: ExistingBehavior.Skip,
71-
}),
72-
addDependency('@angular/build', latestVersions.AngularBuild, {
73-
type: DependencyType.Dev,
74-
existing: ExistingBehavior.Skip,
75-
}),
76-
addDependency('ng-packagr', latestVersions.NgPackagr, {
77-
type: DependencyType.Dev,
78-
existing: ExistingBehavior.Skip,
79-
}),
75+
...LIBRARY_DEV_DEPENDENCIES.map((dependency) =>
76+
addDependency(dependency.name, dependency.version, {
77+
type: DependencyType.Dev,
78+
existing: ExistingBehavior.Skip,
79+
}),
80+
),
8081
addDependency('tslib', latestVersions['tslib'], {
8182
type: DependencyType.Default,
8283
existing: ExistingBehavior.Skip,
8384
}),
84-
addDependency('typescript', latestVersions['typescript'], {
85-
type: DependencyType.Dev,
86-
existing: ExistingBehavior.Skip,
87-
}),
8885
]);
8986
}
9087

0 commit comments

Comments
 (0)