-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.js
196 lines (168 loc) · 4.68 KB
/
utils.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
'use strict';
var merge = require('merge-deep');
var unset = require('unset-value');
var set = require('set-value');
var semver = require('semver');
var get = require('get-value');
var unionValue = require('union-value');
var utils = module.exports;
utils.updateEngines = function(defaults, pkg, travis) {
var engines = mergeEngines(defaults, travis);
var allowedFailures = mergeAllowedFailures(defaults, travis);
var pkgEngine = toSemver(pkg.engines.node);
if (engines.indexOf(pkgEngine) === -1) {
engines.push(pkgEngine);
}
// "e_" is engines
var e_partitioned = partition(engines, isNumericVersion);
var e_nonNumeric = e_partitioned[1];
var e_versions = e_partitioned[0];
var e_pversions = partitionVersions(e_versions, pkg);
// "a_" is allowed failures
var a_partitioned = partition(allowedFailures, isNumericVersion);
var a_versions = a_partitioned[0];
var a_pversions = partitionVersions(a_versions, pkg);
var required = e_pversions[0].concat(a_pversions[0]).concat(e_nonNumeric);
var notRequired = e_pversions[1].concat(a_pversions[1]);
var arr = [];
for (var i = 0; i < notRequired.length; i++) {
arr.push({node_js: notRequired[i]});
}
travis = merge({}, defaults, travis);
updateValue(travis, 'node_js', required);
updateValue(travis, 'matrix.allow_failures', arr, 'node_js');
if (!get(travis, 'matrix.allow_failures').length) {
unset(travis, 'matrix.allow_failures');
unset(travis, 'matrix.fast_finish');
}
if (Object.keys(travis.matrix).length === 1) {
delete travis.matrix;
}
return travis;
};
function mergeEngines(defaults, travis) {
var defaultValues = defaults.node_js || [];
var travisValues = travis.node_js || [];
return formatVersions(defaultValues.concat(travisValues));
}
function mergeAllowedFailures(defaults, travis) {
var defaultValues = mapValues(defaults, 'matrix.allow_failures', 'node_js');
var travisValues = mapValues(travis, 'matrix.allow_failures', 'node_js');
return formatVersions(defaultValues.concat(travisValues));
}
function formatVersions(versions) {
var result = [];
for (var i = 0; i < versions.length; i++) {
var version = String(versions[i]).trim();
if (isNumericVersion(version)) {
version = toSemver(version);
}
if (result.indexOf(version) === -1) {
result.push(version);
}
}
return result;
}
function mapValues(obj, prop, key) {
var arr = get(obj, prop);
if (!arr) return [];
var values = [];
for (var i = 0; i < arr.length; i++) {
var val = get(arr[i], key);
if (val) values.push(val);
}
return values;
}
function updateValue(obj, prop, arr, sortBy) {
set(obj, prop, []);
var stash = [];
var res = [];
for (var i = 0; i < arr.length; i++) {
var ele = arr[i];
if (typeof ele === 'string') {
if (isNumericVersion(ele)) {
ele = String(compressVersion(ele));
}
if (ele === 'stable') {
continue;
}
if (res.indexOf(ele) === -1) {
res.push(ele);
}
} else if (sortBy && isNumericVersion(ele[sortBy])) {
ele[sortBy] = String(compressVersion(ele[sortBy]));
if (stash.indexOf(ele[sortBy]) === -1) {
stash.push(ele[sortBy]);
res.push(ele);
}
}
}
unionValue(obj, prop, res);
sortValue(obj, prop, sortBy);
}
function sortValue(obj, prop, sortBy) {
var arr = get(obj, prop);
if (Array.isArray(arr)) {
arr.sort(function(a, b) {
if (typeof sortBy === 'string') {
return a[sortBy].localeCompare(b[sortBy]);
}
return a.localeCompare(b);
});
arr.reverse();
}
}
function partitionVersions(versions, pkg) {
let a = [];
let b = [];
for (let i = 0; i < versions.length; i++) {
let version = toSemver(versions[i]);
if (version === 'stable') {
continue;
}
if (semver.satisfies(version, pkg.engines.node)) {
a.push(version);
} else {
b.push(version);
}
}
return [a, b];
}
function partition(arr, fn) {
let a = [];
let b = [];
for (let i = 0; i < arr.length; i++) {
let ele = arr[i];
if (fn(ele)) {
a.push(ele);
} else {
b.push(ele);
}
}
return [a, b];
}
function isNumericVersion(version) {
return /^\d+(\.\d+)?(\.\d+)?$/.test(version);
}
function toSemver(version) {
version = version.replace(/^\D*|\D*$/g, '');
let increments = String(version).trim().split('.');
while (increments.length < 3) increments.push('0');
return increments.join('.');
}
function compressVersion(str) {
str = String(str);
let segs = str.trim().split('.');
let len = segs.length;
while (len--) {
if (segs[len] >= 1) {
break;
}
segs.pop();
}
return segs.join('.');
}
/**
* Expose `utils` modules
*/
module.exports = utils;