/
update.ts
192 lines (190 loc) · 6.24 KB
/
update.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
import equal from 'fast-deep-equal';
import { ReleaseType, inc } from 'semver';
import { logger } from '../../logger';
import { matchAt, replaceAt } from '../../util/string';
import { UpdateDependencyConfig } from '../common';
export function bumpPackageVersion(
content: string,
currentValue: string,
bumpVersion: ReleaseType | string
): string {
if (!bumpVersion) {
return content;
}
logger.debug(
{ bumpVersion, currentValue },
'Checking if we should bump package.json version'
);
let newPjVersion: string;
try {
if (bumpVersion.startsWith('mirror:')) {
const mirrorPackage = bumpVersion.replace('mirror:', '');
const parsedContent = JSON.parse(content);
newPjVersion =
(parsedContent.dependencies || {})[mirrorPackage] ||
(parsedContent.devDependencies || {})[mirrorPackage] ||
(parsedContent.optionalDependencies || {})[mirrorPackage] ||
(parsedContent.peerDependencies || {})[mirrorPackage];
if (!newPjVersion) {
logger.warn('bumpVersion mirror package not found: ' + mirrorPackage);
return content;
}
} else {
newPjVersion = inc(currentValue, bumpVersion as ReleaseType);
}
logger.debug({ newPjVersion });
const bumpedContent = content.replace(
/("version":\s*")[^"]*/,
`$1${newPjVersion}`
);
if (bumpedContent === content) {
logger.debug('Version was already bumped');
} else {
logger.debug('Bumped package.json version');
}
return bumpedContent;
} catch (err) {
logger.warn(
{
content,
currentValue,
bumpVersion,
},
'Failed to bumpVersion'
);
return content;
}
}
export function updateDependency({
fileContent,
upgrade,
}: UpdateDependencyConfig): string | null {
const { depType, managerData } = upgrade;
const depName = 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 = JSON.parse(fileContent);
// Save the old version
const oldVersion = parsedContents[depType][depName];
if (oldVersion === newValue) {
logger.trace('Version is already updated');
return bumpPackageVersion(
fileContent,
upgrade.packageJsonVersion,
upgrade.bumpVersion
);
}
// Update the file = this is what we want
parsedContents[depType][depName] = newValue;
// Look for the old version number
const searchString = `"${oldVersion}"`;
const newString = `"${newValue}"`;
let newFileContent = null;
// 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 (equal(parsedContents, JSON.parse(testContent))) {
newFileContent = testContent;
break;
}
}
}
// istanbul ignore if
if (!newFileContent) {
logger.debug(
{ fileContent, parsedContents, depType, depName, newValue },
'Warning: updateDependency error'
);
return fileContent;
}
if (parsedContents && 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'
);
}
// Look for the old version number
const oldResolution = `"${parsedContents.resolutions[depKey]}"`;
const newResolution = `"${newValue}"`;
// Update the file = this is what we want
parsedContents.resolutions[depKey] = newValue;
// Skip ahead to depType section
searchIndex = newFileContent.indexOf(`"resolutions"`);
logger.trace(`Starting search at index ${searchIndex}`);
// Iterate through the rest of the file
for (; searchIndex < newFileContent.length; searchIndex += 1) {
// First check if we have a hit for the old version
if (matchAt(newFileContent, searchIndex, oldResolution)) {
logger.trace(`Found match at index ${searchIndex}`);
// Now test if the result matches
const testContent = replaceAt(
newFileContent,
searchIndex,
oldResolution,
newResolution
);
// Compare the parsed JSON structure of old and new
if (equal(parsedContents, JSON.parse(testContent))) {
newFileContent = testContent;
break;
}
}
}
}
}
return bumpPackageVersion(
newFileContent,
upgrade.packageJsonVersion,
upgrade.bumpVersion
);
} catch (err) {
logger.debug({ err }, 'updateDependency error');
return null;
}
}