+96
-5
lines changedFilter options
+96
-5
lines changed Original file line number Diff line number Diff line change
@@ -20,6 +20,7 @@ import {
20
20
import {
21
21
AngularBuilder,
22
22
DependencyType,
23
+
ExistingBehavior,
23
24
addDependency,
24
25
updateWorkspace,
25
26
} from '@schematics/angular/utility';
@@ -92,7 +93,10 @@ export default function (options: E2eOptions): Rule {
92
93
]),
93
94
),
94
95
...E2E_DEV_DEPENDENCIES.map((name) =>
95
-
addDependency(name, latestVersions[name], { type: DependencyType.Dev }),
96
+
addDependency(name, latestVersions[name], {
97
+
type: DependencyType.Dev,
98
+
existing: ExistingBehavior.Skip,
99
+
}),
96
100
),
97
101
addScriptsToPackageJson(),
98
102
]);
Original file line number Diff line number Diff line change
@@ -52,6 +52,24 @@ export enum InstallBehavior {
52
52
Always,
53
53
}
54
54
55
+
/**
56
+
* An enum used to specify the existing dependency behavior for the {@link addDependency}
57
+
* schematics rule. The existing behavior affects whether the named dependency will be added
58
+
* to the `package.json` when the dependency is already present with a differing specifier.
59
+
*/
60
+
export enum ExistingBehavior {
61
+
/**
62
+
* The dependency will not be added or otherwise changed if it already exists.
63
+
*/
64
+
Skip,
65
+
/**
66
+
* The dependency's existing specifier will be replaced with the specifier provided in the
67
+
* {@link addDependency} call. A warning will also be shown during schematic execution to
68
+
* notify the user of the replacement.
69
+
*/
70
+
Replace,
71
+
}
72
+
55
73
/**
56
74
* Adds a package as a dependency to a `package.json`. By default the `package.json` located
57
75
* at the schematic's root will be used. The `manifestPath` option can be used to explicitly specify
@@ -88,12 +106,18 @@ export function addDependency(
88
106
* Defaults to {@link InstallBehavior.Auto}.
89
107
*/
90
108
install?: InstallBehavior;
109
+
/**
110
+
* The behavior to use when the dependency already exists within the `package.json`.
111
+
* Defaults to {@link ExistingBehavior.Replace}.
112
+
*/
113
+
existing?: ExistingBehavior;
91
114
} = {},
92
115
): Rule {
93
116
const {
94
117
type = DependencyType.Default,
95
118
packageJsonPath = '/package.json',
96
119
install = InstallBehavior.Auto,
120
+
existing = ExistingBehavior.Replace,
97
121
} = options;
98
122
99
123
return (tree, context) => {
@@ -113,7 +137,12 @@ export function addDependency(
113
137
114
138
if (existingSpecifier) {
115
139
// Already present but different specifier
116
-
// This warning may become an error in the future
140
+
141
+
if (existing === ExistingBehavior.Skip) {
142
+
return;
143
+
}
144
+
145
+
// ExistingBehavior.Replace is the only other behavior currently
117
146
context.logger.warn(
118
147
`Package dependency "${name}" already exists with a different specifier. ` +
119
148
`"${existingSpecifier}" will be replaced with "${specifier}".`,
Original file line number Diff line number Diff line change
@@ -15,7 +15,7 @@ import {
15
15
callRule,
16
16
chain,
17
17
} from '@angular-devkit/schematics';
18
-
import { DependencyType, InstallBehavior, addDependency } from './dependency';
18
+
import { DependencyType, ExistingBehavior, InstallBehavior, addDependency } from './dependency';
19
19
20
20
interface LogEntry {
21
21
type: 'warn';
@@ -63,7 +63,7 @@ describe('addDependency', () => {
63
63
});
64
64
});
65
65
66
-
it('warns if a package is already present with a different specifier', async () => {
66
+
it('warns if a package is already present with a different specifier by default', async () => {
67
67
const tree = new EmptyTree();
68
68
tree.create(
69
69
'/package.json',
@@ -85,6 +85,64 @@ describe('addDependency', () => {
85
85
);
86
86
});
87
87
88
+
it('warns if a package is already present with a different specifier with replace behavior', async () => {
89
+
const tree = new EmptyTree();
90
+
tree.create(
91
+
'/package.json',
92
+
JSON.stringify({
93
+
dependencies: { '@angular/core': '^13.0.0' },
94
+
}),
95
+
);
96
+
97
+
const rule = addDependency('@angular/core', '^14.0.0', { existing: ExistingBehavior.Replace });
98
+
99
+
const { logs } = await testRule(rule, tree);
100
+
expect(logs).toContain(
101
+
jasmine.objectContaining({
102
+
type: 'warn',
103
+
message:
104
+
'Package dependency "@angular/core" already exists with a different specifier. ' +
105
+
'"^13.0.0" will be replaced with "^14.0.0".',
106
+
}),
107
+
);
108
+
});
109
+
110
+
it('replaces the specifier if a package is already present with a different specifier with replace behavior', async () => {
111
+
const tree = new EmptyTree();
112
+
tree.create(
113
+
'/package.json',
114
+
JSON.stringify({
115
+
dependencies: { '@angular/core': '^13.0.0' },
116
+
}),
117
+
);
118
+
119
+
const rule = addDependency('@angular/core', '^14.0.0', { existing: ExistingBehavior.Replace });
120
+
121
+
await testRule(rule, tree);
122
+
123
+
expect(tree.readJson('/package.json')).toEqual({
124
+
dependencies: { '@angular/core': '^14.0.0' },
125
+
});
126
+
});
127
+
128
+
it('does not replace the specifier if a package is already present with a different specifier with skip behavior', async () => {
129
+
const tree = new EmptyTree();
130
+
tree.create(
131
+
'/package.json',
132
+
JSON.stringify({
133
+
dependencies: { '@angular/core': '^13.0.0' },
134
+
}),
135
+
);
136
+
137
+
const rule = addDependency('@angular/core', '^14.0.0', { existing: ExistingBehavior.Skip });
138
+
139
+
await testRule(rule, tree);
140
+
141
+
expect(tree.readJson('/package.json')).toEqual({
142
+
dependencies: { '@angular/core': '^13.0.0' },
143
+
});
144
+
});
145
+
88
146
it('adds a package version with other packages in alphabetical order', async () => {
89
147
const tree = new EmptyTree();
90
148
tree.create(
Original file line number Diff line number Diff line change
@@ -18,4 +18,4 @@ export {
18
18
export { Builders as AngularBuilder } from './workspace-models';
19
19
20
20
// Package dependency related rules and types
21
-
export { DependencyType, InstallBehavior, addDependency } from './dependency';
21
+
export { DependencyType, ExistingBehavior, InstallBehavior, addDependency } from './dependency';
You can’t perform that action at this time.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4