-
Notifications
You must be signed in to change notification settings - Fork 2
/
version-selector.js
39 lines (36 loc) · 1.62 KB
/
version-selector.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
const semver = require("semver");
function extractVersionFragment(version, fragment) {
let semanticVersion = semver.parse(version);
switch (fragment) {
case 'major': return semanticVersion.major;
case 'minor': return semanticVersion.major + '.' + semanticVersion.minor;
case 'patch': return semanticVersion.major + '.' + semanticVersion.minor + '.' + semanticVersion.patch;
default: return version;
}
}
function selectVersions(versionRange, limit, limitBy, orderedVersionRepoTupleList, consumer, logger) {
if (!orderedVersionRepoTupleList || !orderedVersionRepoTupleList.length) {
logger.debug('No version given.');
return;
}
let usedVersionFragments = new Set();
if (typeof versionRange === "string") {
versionRange = new semver.Range(versionRange);
}
let idx = 0;
do {
let candidate = orderedVersionRepoTupleList[idx];
if (versionRange.test(candidate.version)) {
const candidateFragment = extractVersionFragment(candidate.version, limitBy);
if (!usedVersionFragments.has(candidateFragment)) {
consumer(candidate);
usedVersionFragments.add(candidateFragment);
} else {
logger.debug('Skipping version ' + candidate.version + ': version fragment already present');
}
} else {
logger.debug('Skipping version ' + candidate.version + ': does not match range "' + versionRange.format() + '"');
}
} while (++idx < orderedVersionRepoTupleList.length && usedVersionFragments.size < limit)
}
module.exports = selectVersions