-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
Copy pathindex.ts
140 lines (138 loc) · 4.19 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
import { dequal } from 'dequal';
import type { PackageJson } from 'type-fest';
import { logger } from '../../../../logger';
import { matchAt, replaceAt } from '../../../../util/string';
import type { UpdateDependencyConfig } from '../../../types';
function replaceAsString(
parsedContents: PackageJson,
fileContent: string,
depType: string,
depName: string,
oldVersion: string,
newValue: string
): string | null {
// Update the file = this is what we want
if (depType === 'packageManager') {
parsedContents[depType] = newValue;
} else {
parsedContents[depType][depName] = newValue;
}
// Look for the old version number
const searchString = `"${oldVersion}"`;
const newString = `"${newValue}"`;
// 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,
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) {
newValue = `npm:${upgrade.lookupName}@${newValue}`;
}
logger.debug(`npm.updateDependency(): ${depType}.${depName} = ${newValue}`);
try {
const parsedContents: PackageJson = JSON.parse(fileContent);
// Save the old version
let oldVersion: string;
if (depType === 'packageManager') {
oldVersion = parsedContents[depType];
newValue = `${depName}@${newValue}`;
} else {
oldVersion = parsedContents[depType][depName];
}
if (oldVersion === newValue) {
logger.trace('Version is already updated');
return fileContent;
}
let newFileContent = replaceAsString(
parsedContents,
fileContent,
depType,
depName,
oldVersion,
newValue
);
// istanbul ignore if
if (!newFileContent) {
logger.debug(
{ fileContent, parsedContents, depType, depName, newValue },
'Warning: updateDependency error'
);
return fileContent;
}
if (parsedContents?.resolutions) {
let depKey: string;
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,
parsedContents.resolutions[depKey],
newValue
);
}
}
return newFileContent;
} catch (err) {
logger.debug({ err }, 'updateDependency error');
return null;
}
}