-
Notifications
You must be signed in to change notification settings - Fork 0
/
package-lock-resolved.js
103 lines (96 loc) · 3.11 KB
/
package-lock-resolved.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
/* eslint-disable @typescript-eslint/no-var-requires */
const fs = require('fs');
const path = require('path');
// true to remove the resolved property from dependencies
// false to convent the awful registry.npm.taobao.org url to registry.npmjs.org
const remove = process.env.NPM_PACKAGE_LOCK_REMOVE_RESOLVED || false;
// since this is the only file reading, using sync io is acceptable
const packageLock =
JSON.parse(fs.readFileSync(path.join(__dirname, 'package-lock.json'), {
encoding: 'utf8'
}));
processDependency(packageLock);
fs.writeFileSync(path.join(__dirname, 'package-lock.json'),
// to make github dependabot happy
JSON.stringify(packageLock, null, 2) + '\n');
/**
* parse and replace registry.npm.taobao.org and registry.npmmirror.com
* to registry.npmjs.org
* @param url mirror url
* @param host host
* @return {string | undefined} string if replaced, or void
*/
function parseNpmMirrorUrl(url, host) {
const resolvedParts = url.split('/');
let status = 0, lastIndex = 0, packageName, fileName;
for (let i = 0; i < resolvedParts.length; i++) {
if (status === 1 && resolvedParts[i][0] === '@') {
continue;
}
switch (resolvedParts[i]) {
case host:
status = 1;
lastIndex = i;
break;
case 'download':
if (status === 1 && lastIndex &&
// note that there is a package called download
(resolvedParts[lastIndex + 1] === resolvedParts[i + 1] ||
resolvedParts[i + 1].startsWith(resolvedParts[lastIndex + 1] + '-'))) {
packageName = resolvedParts.slice(lastIndex + 1, i).join('/');
status = 2;
}
break;
}
}
if (status === 2) {
fileName = resolvedParts[resolvedParts.length - 1];
let idx = fileName.indexOf('?');
if (packageName && idx > 0) {
fileName = fileName.slice(0, idx);
}
if (fileName.endsWith('.tgz')) {
return `https://registry.npmjs.org/${packageName}/-/${fileName}`;
}
}
}
/**
* @param {string} resolved
* @param dependency
*/
function processDependencyUrl(resolved, dependency) {
if (remove) {
if (resolved.includes('registry.npmjs.org')) {
delete dependency.resolved;
}
} else if (resolved.includes('registry.npm.taobao.org')) {
const replaced = parseNpmMirrorUrl(resolved, 'registry.npm.taobao.org');
// console.log(resolved, replaced);
if (replaced) {
dependency.resolved = replaced;
}
} else if (resolved.includes('://registry.npmmirror.com/')) {
const replaced = parseNpmMirrorUrl(resolved, 'registry.npmmirror.com');
// console.log(resolved, replaced);
if (replaced) {
dependency.resolved = replaced;
} else {
dependency.resolved = resolved.replace(
'://registry.npmmirror.com/', '://registry.npmjs.org/');
}
}
}
function processDependency(dependency) {
const {resolved} = dependency;
if (resolved) {
processDependencyUrl(resolved, dependency);
}
if (dependency.dependencies) {
processDependencies(dependency.dependencies);
}
}
function processDependencies(dependencies) {
for (const key of Object.keys(dependencies)){
processDependency(dependencies[key]);
}
}