-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
index.ts
291 lines (283 loc) · 8.83 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
import is from '@sindresorhus/is';
import { dequal } from 'dequal';
import { logger } from '../../../../../logger';
import { escapeRegExp, regEx } from '../../../../../util/regex';
import { matchAt, replaceAt } from '../../../../../util/string';
import type { UpdateDependencyConfig, Upgrade } from '../../../types';
import type {
DependenciesMeta,
NpmPackage,
OverrideDependency,
RecursiveOverride,
} from '../../extract/types';
import type { NpmDepType, NpmManagerData } from '../../types';
function renameObjKey(
oldObj: DependenciesMeta,
oldKey: string,
newKey: string,
): DependenciesMeta {
const keys = Object.keys(oldObj);
return keys.reduce((acc, key) => {
if (key === oldKey) {
acc[newKey] = oldObj[oldKey];
} else {
acc[key] = oldObj[key];
}
return acc;
}, {} as DependenciesMeta);
}
function replaceAsString(
parsedContents: NpmPackage,
fileContent: string,
depType: NpmDepType | 'dependenciesMeta' | 'packageManager',
depName: string,
oldValue: string,
newValue: string,
parents?: string[],
): string {
if (depType === 'packageManager') {
parsedContents[depType] = newValue;
} else if (depName === oldValue) {
// The old value is the name of the dependency itself
delete Object.assign(parsedContents[depType]!, {
[newValue]: parsedContents[depType]![oldValue],
})[oldValue];
} else if (depType === 'dependenciesMeta') {
if (oldValue !== newValue) {
parsedContents.dependenciesMeta = renameObjKey(
// TODO #22198
parsedContents.dependenciesMeta!,
oldValue,
newValue,
);
}
} else if (parents && depType === 'overrides') {
// there is an object as a value in overrides block
const { depObjectReference, overrideDepName } = overrideDepPosition(
parsedContents[depType]!,
parents,
depName,
);
if (depObjectReference) {
depObjectReference[overrideDepName] = newValue;
}
} else {
// The old value is the version of the dependency
parsedContents[depType]![depName] = newValue;
}
// Look for the old version number
const searchString = `"${oldValue}"`;
let newString = `"${newValue}"`;
const escapedDepName = escapeRegExp(depName);
const patchRe = regEx(`^(patch:${escapedDepName}@(npm:)?).*#`);
const match = patchRe.exec(oldValue);
if (match && depType === 'resolutions') {
const patch = oldValue.replace(match[0], `${match[1]}${newValue}#`);
parsedContents[depType]![depName] = patch;
newString = `"${patch}"`;
}
// Skip ahead to depType section
let searchIndex = fileContent.indexOf(`"${depType}"`) + depType.length;
logger.trace(`Starting search at index ${searchIndex}`);
// Iterate through the rest of the file
for (; searchIndex < fileContent.length; searchIndex += 1) {
// First check if we have a hit for the old version
if (matchAt(fileContent, searchIndex, searchString)) {
logger.trace(`Found match at index ${searchIndex}`);
// Now test if the result matches
const testContent = replaceAt(
fileContent,
searchIndex,
searchString,
newString,
);
// Compare the parsed JSON structure of old and new
if (dequal(parsedContents, JSON.parse(testContent))) {
return testContent;
}
}
}
// istanbul ignore next
throw new Error();
}
export function updateDependency({
fileContent,
upgrade,
}: UpdateDependencyConfig): string | null {
const { depType, managerData } = upgrade;
const depName: string = managerData?.key || upgrade.depName;
let { newValue } = upgrade;
if (upgrade.currentRawValue) {
if (upgrade.currentDigest) {
logger.debug('Updating package.json git digest');
newValue = upgrade.currentRawValue.replace(
upgrade.currentDigest,
// TODO #22198
upgrade.newDigest!.substring(0, upgrade.currentDigest.length),
);
} else {
logger.debug('Updating package.json git version tag');
newValue = upgrade.currentRawValue.replace(
upgrade.currentValue,
upgrade.newValue,
);
}
}
if (upgrade.npmPackageAlias) {
// TODO: types (#22198)
newValue = `npm:${upgrade.packageName}@${newValue}`;
}
// TODO: types (#22198)
logger.debug(`npm.updateDependency(): ${depType}.${depName} = ${newValue}`);
try {
const parsedContents: NpmPackage = JSON.parse(fileContent);
let overrideDepParents: string[] | undefined = undefined;
// Save the old version
let oldVersion: string | undefined;
if (depType === 'packageManager') {
oldVersion = parsedContents[depType];
// TODO: types (#22198)
newValue = `${depName}@${newValue}`;
} else if (isOverrideObject(upgrade)) {
overrideDepParents = managerData?.parents;
if (overrideDepParents) {
// old version when there is an object as a value in overrides block
const { depObjectReference, overrideDepName } = overrideDepPosition(
parsedContents['overrides']!,
overrideDepParents,
depName,
);
if (depObjectReference) {
oldVersion = depObjectReference[overrideDepName]!;
}
}
} else {
// eslint-disable @typescript-eslint/no-unnecessary-type-assertion
oldVersion = parsedContents[depType as NpmDepType]![depName] as string;
}
if (oldVersion === newValue) {
logger.trace('Version is already updated');
return fileContent;
}
// TODO #22198
let newFileContent = replaceAsString(
parsedContents,
fileContent,
depType as NpmDepType,
depName,
oldVersion!,
newValue!,
overrideDepParents,
);
if (upgrade.newName) {
newFileContent = replaceAsString(
parsedContents,
newFileContent,
depType as NpmDepType,
depName,
depName,
upgrade.newName,
overrideDepParents,
);
}
// istanbul ignore if
if (!newFileContent) {
logger.debug(
{ fileContent, parsedContents, depType, depName, newValue },
'Warning: updateDependency error',
);
return fileContent;
}
if (parsedContents?.resolutions) {
let depKey: string | undefined;
if (parsedContents.resolutions[depName]) {
depKey = depName;
} else if (parsedContents.resolutions[`**/${depName}`]) {
depKey = `**/${depName}`;
}
if (depKey) {
// istanbul ignore if
if (parsedContents.resolutions[depKey] !== oldVersion) {
logger.debug(
{
depName,
depKey,
oldVersion,
resolutionsVersion: parsedContents.resolutions[depKey],
},
'Upgraded dependency exists in yarn resolutions but is different version',
);
}
newFileContent = replaceAsString(
parsedContents,
newFileContent,
'resolutions',
depKey,
// TODO #22198
parsedContents.resolutions[depKey]!,
// TODO #22198
newValue!,
);
if (upgrade.newName) {
if (depKey === `**/${depName}`) {
// handles the case where a replacement is in a resolution
upgrade.newName = `**/${upgrade.newName}`;
}
newFileContent = replaceAsString(
parsedContents,
newFileContent,
'resolutions',
depKey,
depKey,
upgrade.newName,
);
}
}
}
if (parsedContents?.dependenciesMeta) {
for (const [depKey] of Object.entries(parsedContents.dependenciesMeta)) {
if (depKey.startsWith(depName + '@')) {
newFileContent = replaceAsString(
parsedContents,
newFileContent,
'dependenciesMeta',
depName,
depKey,
// TODO: types (#22198)
`${depName}@${newValue}`,
);
}
}
}
return newFileContent;
} catch (err) {
logger.debug({ err }, 'updateDependency error');
return null;
}
}
function overrideDepPosition(
overrideBlock: OverrideDependency,
parents: string[],
depName: string,
): {
depObjectReference: Record<string, string>;
overrideDepName: string;
} {
// get override dep position when its nested in an object
const lastParent = parents[parents.length - 1];
let overrideDep: OverrideDependency = overrideBlock;
for (const parent of parents) {
if (overrideDep) {
overrideDep = overrideDep[parent] as Record<string, RecursiveOverride>;
}
}
const overrideDepName = depName === lastParent ? '.' : depName;
const depObjectReference = overrideDep as Record<string, string>;
return { depObjectReference, overrideDepName };
}
function isOverrideObject(upgrade: Upgrade<NpmManagerData>): boolean {
return (
is.array(upgrade.managerData?.parents, is.nonEmptyStringAndNotWhitespace) &&
upgrade.depType === 'overrides'
);
}