-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
469 lines (434 loc) · 14.7 KB
/
index.js
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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
#!/usr/bin/env node
const path = require('path');
const fs = require('fs');
const os = require('os');
const diff = require('diff');
const version = require('./package.json').version;
const npmFolder = path.join(npmDir(),'node_modules','npm','node_modules');
let pacote, rimraf;
try
{
pacote = require('pacote');
}
catch(e)
{
pacote = require(path.join(npmFolder,'pacote'));
}
try
{
rimraf = require('rimraf');
}
catch(e)
{
rimraf = require(path.join(npmFolder,'rimraf'));
}
// ANSI styles
const ansiColors = ['black','red','green','yellow','blue','magenta','cyan','white'];
function startColor(colorName, background)
{
let idx = ansiColors.indexOf(colorName);
if(idx !== -1) return ansi(idx + (background ? 40 : 30));
idx = ansiColors.indexOf(colorName.replace('Bright',''));
if(idx !== -1) return ansi(idx + (background ? 100 : 90));
return ansi(background ? 100 : 90); // grey
}
function stopColor(background)
{
return ansi(background ? 49 : 39);
}
function ansi(code)
{
return '\u001B[' + code + 'm';
}
function echo()
{
console.log.apply(null,arguments);
}
const curDir = process.cwd();
const tmpDir = os.tmpdir();
const patchDir = path.join(curDir, 'patches');
echo(startColor('whiteBright') + 'CustomPatch' + stopColor() + ' version ' + startColor('greenBright') + version + stopColor() + '\n');
if(!fs.existsSync(path.join(curDir, '/node_modules')))
{
echo(startColor('redBright') + 'ERROR: ' + stopColor() + 'Missing ' + startColor('whiteBright') + '"node_modules"' + stopColor() + ' folder');
process.exit(1);
}
process.argv.shift(); // remove Node/NPX
process.argv.shift(); // remove this script
const len = process.argv.length;
const patchFiles = [];
const asyncResults = [];
if(len > 0)
{
// create patches
if(!fs.existsSync(patchDir)) fs.mkdirSync(patchDir);
for(let i = 0; i < len; i++)
{
makePatch(process.argv[i]);
}
}
else
{
if(!fs.existsSync(patchDir))
{
echo(startColor('yellowBright') + 'WARNING: ' + stopColor() + 'Missing ' + startColor('whiteBright') + 'patches' + stopColor() + ' folder - nothing to do');
process.exit(2);
}
// apply patches
fs.readdirSync(patchDir).map(item =>
{
if(item.substr(-6) !== '.patch') return;
const pkg = item.replace('.patch','').split('#');
const packageName = pkg[0].replace(/\+/g, path.sep);
const dest = path.join(curDir, 'node_modules', packageName);
if(!fs.existsSync(dest))
{
echo(startColor('yellowBright') + 'WARNING: ' + stopColor() + 'Package ' + startColor('whiteBright') + packageName + stopColor() + ' is not installed - skipping this patch');
return;
}
patchFiles.push({
pkgName: pkg[0],
version: pkg[1],
});
});
echo('Found ' + startColor('cyanBright') + patchFiles.length + stopColor() + ' patches');
patchFiles.forEach(({ pkgName, version}) =>
{
readPatch(pkgName, version);
});
// wait until all chunks of all patches have been processed
setTimeout(waitForResults, 20);
}
// find the NPM folder because PACOTE package is relative to this folder (not a global package)
function npmDir()
{
if(process.platform.indexOf('win') === 0)
{
return process.env.APPDATA ? path.join(process.env.APPDATA, 'npm') : path.dirname(process.execPath);
}
else
{
// /usr/local/bin/node --> prefix=/usr/local
let prefix = path.dirname(path.dirname(process.execPath));
// destdir is respected only on Unix
if (process.env.DESTDIR) prefix = path.join(process.env.DESTDIR, prefix);
return prefix;
}
}
// returns FALSE on error, Package.JSON on success
function getConfig(pkgName)
{
const folder = path.join(curDir, 'node_modules', pkgName);
const cfgName = path.join(folder, 'package.json');
if(!fs.existsSync(folder))
{
echo(startColor('redBright') + 'ERROR: ' + stopColor() + 'Missing folder "' + startColor('whiteBright') + './node_modules/' + stopColor() + startColor('greenBright') + pkgName + stopColor() + '"');
return false;
}
try
{
fs.accessSync(cfgName, fs.constants.R_OK);
}
catch (e)
{
echo(startColor('redBright') + 'ERROR: ' + stopColor() + 'Can not read ' + startColor('whiteBright') + '"package.json"' + stopColor());
return false;
}
const pkgConfig = fs.readFileSync(cfgName,'utf8');
let cfg = {};
try
{
cfg = JSON.parse(pkgConfig);
}
catch(e)
{
echo(startColor('redBright') + 'ERROR: ' + stopColor() + 'Could not parse ' + startColor('whiteBright') + '"package.json"' + stopColor() + ' - ' + startColor('redBright') + e.message + stopColor());
return false;
}
return cfg;
}
// build a tarball URL for the given package version
function npmTarballURL(pkgName, pkgVersion, registryURL)
{
let registry;
if (registryURL)
{
registry = registryURL.endsWith('/') ? registryURL : registryURL + '/';
}
else
{
registry = 'https://registry.npmjs.org/';
}
const scopelessName = getScopelessName(pkgName);
return `${registry}${pkgName}/-/${scopelessName}-${removeBuildMetadataFromVersion(pkgVersion)}.tgz`;
}
function removeBuildMetadataFromVersion (version)
{
const plusPos = version.indexOf('+');
if (plusPos === -1) return version;
return version.substring(0, plusPos);
}
function getScopelessName (name)
{
if (name[0] !== '@') return name;
return name.split('/')[1];
}
// build a patch for the given package
function makePatch(pkgName)
{
echo('Creating patch for: ' + startColor('magentaBright') + pkgName + stopColor());
const cfg = getConfig(pkgName);
if(cfg)
{
fetchPackage(pkgName, npmTarballURL(pkgName, cfg.version), cfg.version, comparePackages);
}
else
{
echo(startColor('redBright') + 'ERROR: ' + stopColor() + 'Could not find the ' + startColor('whiteBright') + 'URL' + stopColor() + ' for ' + startColor('greenBright') + 'tarball' + stopColor());
}
}
// download the tarball
function fetchPackage(pkgName, url, version, callback)
{
echo('Fetching tarball of ' + startColor('whiteBright') + pkgName + stopColor() + ' from ' + startColor('green') + url + stopColor());
const dest = path.join(tmpDir, pkgName);
pacote.extract(url, dest).then(function ()
{
callback(pkgName, version);
rimraf(dest, function (err)
{
if(err) echo(startColor('redBright') + 'ERROR: ' + stopColor() + 'Could not clean up the TEMP folder');
});
}).catch(function (err)
{
echo(startColor('redBright') + err.message + stopColor());
});
}
// replace directory separators in package names like "@vue/cli" or "@babel/register"
function goodFileName(fn)
{
const pattern = new RegExp('/', 'g');
return fn.replace(pattern, '+');
}
function makePatchName(pkgName, version)
{
return goodFileName(pkgName) + '#' + version + '.patch';
}
// compare all files from the locally modified package with their original content
function comparePackages(pkgName, version)
{
const patchFile = makePatchName(pkgName, version);
const stream = fs.createWriteStream(path.join(patchDir, patchFile));
stream.cork();
scanFiles(pkgName, '', stream);
stream.end();
echo('Successfully created ' + startColor('greenBright') + patchFile + stopColor());
}
// recursively enumerate all files in the locally installed package
function scanFiles(pkgName, src, patch)
{
const files = fs.readdirSync(path.join(curDir, 'node_modules', pkgName, src));
files.forEach(item =>
{
if (item === 'node_modules') return;
const pathname = path.join(src, item);
const stat = fs.lstatSync(path.join(curDir, 'node_modules', pkgName, pathname));
if(stat.isDirectory()) scanFiles(pkgName, pathname, patch);
else createPatch(pkgName, pathname, patch);
});
}
// compare a modified file from the local package to its original counterpart - if they are different, create a patch and append it to the patch stream
function createPatch(pkgName, pathname, patch)
{
const newFile = path.join(curDir, 'node_modules', pkgName, pathname);
const oldFile = path.join(tmpDir, pkgName, pathname);
let oldStr = fs.existsSync(oldFile) ? fs.readFileSync(oldFile, 'utf8') : '';
let newStr = fs.readFileSync(newFile, 'utf8');
if(pathname === 'package.json') return; // skip "package.json" - comparison is not reliable because NPM reorders keys and also appends many system keys (whose names begin with underscore)
/*
{
let oldJson = {}, newJson = {};
try
{
oldJson = JSON.parse(oldStr);
newJson = JSON.parse(newStr);
}
catch(e)
{
echo(startColor('redBright') + 'ERROR: ' + stopColor() + 'Could not parse ' + startColor('green') + 'package.json' + stopColor() + ' = ' + startColor('redBright') + e.message + stopColor());
return;
}
// remove all keys which start with underscore
let key;
for(key in oldJson)
if(key[0] === '_') delete oldJson[key];
for(key in newJson)
if(key[0] === '_') delete newJson[key];
// sort the keys
let oldSorted = {}, newSorted = {};
Object.keys(oldJson).sort().forEach(function(key)
{
oldSorted[key] = oldJson[key];
});
Object.keys(newJson).sort().forEach(function(key)
{
newSorted[key] = newJson[key];
});
oldStr = JSON.stringify(oldSorted, null, 2);
newStr = JSON.stringify(newSorted, null, 2);
}
*/
if(oldStr !== newStr) patch.write(diff.createTwoFilesPatch(oldFile.replace(tmpDir,''), newFile.replace(path.join(curDir, 'node_modules'),''), oldStr, newStr));
}
// fetch original NPM package, then read the patch file and try to apply hunks
function readPatch(pkgName, version)
{
const packageName = pkgName.replace(/\+/g, path.sep);
const cfg = getConfig(packageName);
if(cfg)
{
const patchFile = pkgName + '#' + version + '.patch';
const patch = fs.readFileSync(path.join(patchDir, patchFile),'utf8');
const chunks = [];
diff.applyPatches(patch,
{
loadFile: loadFile,
patched: (info, content, callback) =>
{
chunks.push({
packageName: pkgName,
packageVersion: cfg.version,
patchVersion: version,
chunkInfo: info,
newContent: content,
});
callback();
},
complete: (err) =>
{
asyncResults.push(chunks);
},
});
}
else
{
asyncResults.push([]);
}
}
// return FALSE if packageSemVer is lower than patchSemVer
function isVersionSuitable(patchSemVer, packageSemVer)
{
const oldVer = patchSemVer.split('.');
const newVer = packageSemVer.split('.');
if (+oldVer[0] < +newVer[0]) return true;
if (+oldVer[0] > +newVer[0]) return false;
if (+oldVer[1] < +newVer[1]) return true;
if (+oldVer[1] > +newVer[1]) return false;
return +oldVer[2] <= +newVer[2];
}
function loadFile(info, callback)
{
/*
info =
{
index: '\vue2-dragula\dist\vue-dragula.js',
oldFileName: '\vue2-dragula\dist\vue-dragula.js',
oldHeader: '',
newFileName: '\vue2-dragula\dist\vue-dragula.js',
newHeader: '',
hunks: [object1, object2, ...]
}
*/
const oldName = path.join(curDir, 'node_modules', pathNormalize(info.index));
if(fs.existsSync(oldName))
{
// read the original file
fs.readFile(oldName, 'utf8', callback);
}
else
{
callback(null, ''); // old file does not exist - i.e. it is empty
}
}
function pathNormalize(pathName)
{
return path.normalize(path.sep === '/' ? pathName.replace(/\\/g, '/') : pathName.replace(/\//g,'\\\\'));
}
function waitForResults()
{
if (asyncResults.length < patchFiles.length) setTimeout(waitForResults, 20);
else
{
const tree = {};
asyncResults.flat().forEach(item =>
{
if (!item) return;
if (!tree[item.packageName])
{
tree[item.packageName] = {
packageName: item.packageName,
packageVersion: item.packageVersion,
patchVersion: item.patchVersion,
chunks: [],
};
}
tree[item.packageName].chunks.push({
chunkInfo: item.chunkInfo,
newContent: item.newContent,
});
});
Object.values(tree).forEach((pkg, index) =>
{
echo('\n ' + (1 + index) + ') Applying patch for ' + startColor('magentaBright') + pkg.packageName + stopColor() + ' ' + startColor('greenBright') + pkg.patchVersion + stopColor());
if (!isVersionSuitable(pkg.patchVersion, pkg.packageVersion))
{
echo(startColor('yellowBright') + 'WARNING: ' + stopColor() + 'The patch is for v' + startColor('greenBright') + pkg.patchVersion + stopColor()
+ ' but you have installed ' + startColor('redBright') + pkg.packageVersion + stopColor());
return;
}
if(pkg.packageVersion !== pkg.patchVersion)
{
echo(startColor('yellowBright') + 'WARNING: ' + stopColor() + 'The patch for ' + startColor('greenBright') + pkg.patchVersion + stopColor()
+ ' may not apply cleanly to the installed ' + startColor('redBright') + pkg.packageVersion + stopColor());
}
pkg.chunks.forEach((chunk, subIndex) =>
{
echo('\n(' + (1 + index) + '.' + (1 + subIndex) + ') Patching chunk ' + startColor('greenBright') + pathNormalize(chunk.chunkInfo.index) + stopColor());
chunk.success = true;
// replace original file with the patched content
if(chunk.newContent !== false)
{
try
{
fs.writeFileSync(path.join(curDir, 'node_modules', pathNormalize(chunk.chunkInfo.index)), chunk.newContent, 'utf8');
}
catch(err)
{
echo('Could not write the new content for chunk ' + startColor('greenBright') + pathNormalize(chunk.chunkInfo.index) + stopColor() + ' = ' + startColor('redBright') + err + stopColor());
chunk.success = false;
}
}
else
{
chunk.success = false;
const oldName = path.join(curDir, 'node_modules', pathNormalize(chunk.chunkInfo.index));
if(!fs.existsSync(oldName))
{
const folder = path.dirname(oldName);
if (!fs.existsSync(folder))
{
echo(startColor('yellowBright') + 'WARNING: Folder ' + stopColor() + startColor('redBright') + path.dirname(pathNormalize(chunk.chunkInfo.index)) + stopColor() + startColor('yellowBright') + ' does not exist - the patch is probably for older version');
return;
}
}
echo(startColor('yellowBright') + 'WARNING: ' + stopColor() + 'Chunk failed - ' + startColor('redBright') + ' either already applied or for different version' + stopColor());
}
});
const allChunks = pkg.chunks.every(chunk => chunk.success);
const noneChunks = pkg.chunks.every(chunk => !chunk.success);
echo('\nPatch for ' + startColor('magentaBright') + pkg.packageName + stopColor() + ' was '
+ startColor(allChunks ? 'cyanBright' : noneChunks ? 'redBright' : 'yellow')
+ (allChunks ? 'successfully' : noneChunks ? 'not' : 'partially') + stopColor() + ' applied');
});
}
}