-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpatchApplying.js
More file actions
364 lines (355 loc) · 11.1 KB
/
Copy pathpatchApplying.js
File metadata and controls
364 lines (355 loc) · 11.1 KB
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
import { echo, startColor, stopColor } from './ansiUtils';
import { hasPatches, getConfig, isVersionSuitable } from './utils';
import { parsePatchName, makePatchName, pathNormalize, readFileContent } from './fileUtils';
import { curDir, patchDir } from './variables';
import fs from 'node:fs';
import path from 'node:path';
import { applyPatch, parsePatch, reversePatch } from 'diff';
/**
* fetch original NPM package, then read the patch file and try to apply or reverse chunks
* @param pkgName {String}
* @param versions {String[]} Array with versions of pkgName for which there is a patch
* @param patchCounter {Number} Sequential number of the current patch when multiple (begins from one)
* @param reversing {Boolean}
*/
function readPatch(pkgName, versions, patchCounter, reversing)
{
const packageName = pkgName.replace(/\+/g, path.sep);
const cfg = getConfig(packageName);
if(cfg)
{
// find the most suitable version of the patch
const descendingVersions = versions.sort().reverse();
let mostSuitable = descendingVersions.find(ver => ver <= cfg.version);
if (!mostSuitable) mostSuitable = versions.pop();
echo('\n ',
patchCounter,
') ',
reversing ? 'Reversing' : 'Applying',
' patch for ',
startColor('magentaBright'),
pkgName,
stopColor(),
' ',
startColor('greenBright'),
mostSuitable,
stopColor(),
' onto ',
startColor('whiteBright'),
cfg.version,
stopColor()
);
if (!isVersionSuitable(mostSuitable, cfg.version))
{
echo(
startColor('yellowBright'),
'WARNING: ',
stopColor(),
'The patch is for v',
startColor('greenBright'),
mostSuitable,
stopColor(),
' but you have installed ',
startColor('redBright'),
cfg.version,
stopColor()
);
}
else
{
if (mostSuitable !== cfg.version)
{
echo(
startColor('yellowBright'),
'WARNING: ',
stopColor(),
'The patch for ',
startColor('greenBright'),
mostSuitable,
stopColor(),
' may not ',
reversing ? 'reverse' : 'apply',
' cleanly to the installed ',
startColor('redBright'),
cfg.version,
stopColor()
);
}
const patchFile = makePatchName(pkgName, mostSuitable);
const patch = fs.readFileSync(path.join(patchDir, patchFile), 'utf8');
const chunks = parsePatch(patch);
chunks.forEach((chunk, subIndex) =>
{
// Ensure that we have a valid file name
const filePath = chunk.newFileName ?? chunk.oldFileName;
if (!filePath)
{
echo(
startColor('redBright'),
'ERROR: ',
stopColor(),
'A chunk has no file names for package ',
startColor('greenBright'),
pkgName,
stopColor()
);
chunk.success = false;
}
else
{
const normalizedPath = pathNormalize(filePath);
const fileName = path.join(curDir, 'node_modules', normalizedPath);
const fileContent = readFileContent(fileName);
if (reversing)
{
echo(
'\n(',
patchCounter,
'.',
(1 + subIndex),
') ',
'Reversing chunk ',
startColor('greenBright'),
filePath,
stopColor()
);
// Reverse the patch
const reversedPatchText = reversePatch(chunk);
const reversePatchedContent = applyPatch(fileContent, reversedPatchText);
if (reversePatchedContent === false)
{
// Failed to reverse the patch
// Attempt to apply the original patch to check if it's already reversed
const patchedContent = applyPatch(fileContent, chunk);
if (patchedContent !== false)
{
// Patch is already reversed
echo(
startColor('yellowBright'),
'WARNING: ',
stopColor(),
'Patch already reversed',
);
chunk.success = true;
}
else
{
// Patch failed for other reasons
echo(
startColor('yellowBright'),
'WARNING: ',
stopColor(),
'Failed to reverse patch for ',
startColor('redBright'),
filePath,
stopColor()
);
chunk.success = false;
}
}
else
{
try
{
fs.writeFileSync(fileName, reversePatchedContent, 'utf8');
chunk.success = true;
}
catch (err)
{
echo(
startColor('redBright'),
'ERROR: ',
stopColor(),
'Could not write the new content for chunk ',
startColor('greenBright'),
fileName,
stopColor(),
' = ',
startColor('redBright'),
err.message || err,
);
chunk.success = false;
}
}
}
else
{
echo(
'\n(',
patchCounter,
'.',
(1 + subIndex),
') ',
'Applying chunk ',
startColor('greenBright'),
filePath,
stopColor()
);
// Apply the patch
const patchedContent = applyPatch(fileContent, chunk);
if (patchedContent === false)
{
// Failed to apply patch normally
// Try applying the reversed patch to check if already applied
const reversedPatchText = reversePatch(chunk);
const reversePatchedContent = applyPatch(fileContent, reversedPatchText);
if (reversePatchedContent !== false)
{
// The patch was already applied
echo(
startColor('yellowBright'),
'WARNING: ',
stopColor(),
'Patch already applied',
);
chunk.success = true;
}
else
{
// Patch failed for other reasons
if (!fs.existsSync(fileName))
{
chunk.success = false;
const folder = path.dirname(fileName);
if (!fs.existsSync(folder))
{
echo(
startColor('yellowBright'),
'WARNING: Folder ',
stopColor(),
startColor('redBright'),
path.dirname(fileName),
stopColor(),
startColor('yellowBright'),
' does not exist - the patch is probably for older version',
stopColor(),
);
}
}
else
{
echo(
startColor('yellowBright'),
'WARNING: ',
stopColor(),
'Chunk failed - ',
startColor('redBright'),
cfg.version !== mostSuitable ? ' either already applied or for different version' : 'probably already applied',
stopColor()
);
chunk.success = false;
}
}
}
else
{
try
{
fs.writeFileSync(fileName, patchedContent, 'utf8');
chunk.success = true;
}
catch (err)
{
echo(
'Could not write the new content for chunk ',
startColor('greenBright'),
fileName,
stopColor(),
' = ',
startColor('redBright'),
err.message || err,
stopColor()
);
chunk.success = false;
}
}
}
}
});
const allChunks = chunks.every(chunk => chunk.success);
const noneChunks = chunks.every(chunk => !chunk.success);
echo(
'\nPatch for ',
startColor('magentaBright'),
pkgName,
stopColor(),
' ',
startColor('greenBright'),
mostSuitable,
stopColor(),
' was ',
startColor(allChunks ? 'cyanBright' : noneChunks ? 'redBright' : 'yellow'),
(allChunks ? 'successfully' : noneChunks ? 'not' : 'partially'),
stopColor(),
reversing ? ' reversed' : ' applied'
);
}
}
}
/**
* Apply all found patches or only those for the specified packages. Reversing requires specifying the package names.
* @param packageNames {Array<String>}
* @param reversing {Boolean}
*/
export function applyPatches(packageNames = [], reversing = false)
{
if (hasPatches())
{
// apply patches
const patchFiles = {};
let numPatches = 0;
fs.readdirSync(patchDir).map(item =>
{
if(!item.endsWith('.patch')) return;
const pkg = parsePatchName(item);
// if specific package names are requested through CLI - apply only them, otherwise apply every patch
if (packageNames.length > 0 ? packageNames.includes(pkg.pkgName) : true)
{
const dest = path.join(curDir, 'node_modules', pkg.pkgName);
if(!fs.existsSync(dest))
{
echo(
startColor('yellowBright'),
'WARNING: ',
stopColor(),
'Package ',
startColor('whiteBright'),
pkg.pkgName,
stopColor(),
' is not installed - skipping this patch'
);
return;
}
// combine the patches for different versions of the same NPM package
patchFiles[pkg.pkgName] = (patchFiles[pkg.pkgName] || []).concat(pkg.version);
numPatches++;
}
});
const patches = Object.entries(patchFiles);
echo(
'Found ',
startColor('cyanBright'),
numPatches,
stopColor(),
' patches'
);
if (packageNames.length > 0)
{
// check if we have patches for all requested packages from CLI - if requested explicitly
packageNames.filter(name => !patches.find(file => file[0] !== name)).forEach(name =>
{
echo(
'No patch was found for ',
startColor('cyanBright'),
name,
stopColor(),
);
});
}
// apply patches intelligently - only for the installed version of the package, or for the most recent previous version
patches.forEach((item, index) =>
{
readPatch(item[0], item[1], index + 1, reversing);
});
}
}