-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
138 lines (117 loc) · 2.75 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
const genList = require('./lib/gen-list');
function main(pkg, pkgLock, name, version){
var pkgDeps = pkg.dependencies;
if(!pkgDeps){
return;
}
var pkgLockDeps = pkgLock.dependencies;
if(!pkgLockDeps){
return;
}
var pkgDevDeps = pkg.devDependencies || Object.create(null);
var optDeps = pkg.optionalDependencies || Object.create(null);
Object.assign(pkgDeps, pkgDevDeps);
Object.assign(pkgDeps, optDeps);
let formatedVersion;
if(version){
formatedVersion = formatVersion(version);
}
const captured = [];
function _isCatch(v){
if(v.name === name){
if(formatedVersion){
return isVersionLessThan(v.version, formatedVersion);
}
return true;
}
}
const tree = {
IS_ROOT: true,
child: {}
};
function genTree(_requires, _tree, depArr){
Object.keys(_requires).forEach(k => {
let item = _tree.child[k] = {
parent: _tree,
name: k,
child: Object.create(null)
}
let v = _findDep(k, depArr);
let depsArr2 = [v.dependencies].concat(depArr);
item.version = v.version;
if(v._treeItem){
// Object.assign(item, v._treeItem);
// item.child = v._treeItem.child;
if(!v._treeItem.parents){
v._treeItem.parents = [];
}
if(v._treeItem !== tree && v._treeItem.parents.indexOf(_tree) === -1){
v._treeItem.parents.push(_tree);
}
} else {
v._treeItem = item;
if(v.requires){
genTree(v.requires, item, depsArr2);
}
}
if(_isCatch(item)){
captured.push(item);
}
})
}
function _findDep(key, arr){
for(let i =0, len = arr.length; i < len; i++){
if(arr[i] && arr[i][key]){
return arr[i][key];
}
}
}
genTree(pkgDeps, tree, [pkgLockDeps]);
return genList(captured);
}
function formatVersion(_version){
let version
if(_version.indexOf('-') !== -1){
version = _version.split('-')[0];
version = version || '';
} else {
version = _version;
}
let arr = version.split('.');
if(!arr.length || arr.length > 3){
return 0;
}
let n, i , len;
const result = [];
for(i = 0, len = arr.length; i < len; i++){
n = Number(arr[i]);
if(isNaN(n)){
return 0;
}
result.push(n);
}
if(len < 3){
i = 0;
len = 3 - len;
for(; i < len; i++){
result.push(0);
}
}
return result;
}
function isVersionLessThan(version, fversion){
let f2 = formatVersion(version);
for(let i = 0, len = f2.length; i < len; i++){
if(f2[i] < fversion[i]){
return true;
} else if(f2[i] > fversion[i]){
return false;
}
}
return false;
}
module.exports = {
main,
formatVersion,
isVersionLessThan
};