-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
/
version.js
159 lines (144 loc) · 4.12 KB
/
version.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
/**
* Utilities relating to generating badges relating to version numbers. Includes
* comparing versions to determine the latest, and determining the color to use
* for the badge based on whether the version is a stable release.
*
* For utilities specific to PHP version ranges, see php-version.js.
*/
'use strict'
const semver = require('semver')
const { addv } = require('./text-formatters')
const { version: versionColor } = require('./color-formatters')
// Given a list of versions (as strings), return the latest version.
// Return undefined if no version could be found.
function latest(versions, { pre = false } = {}) {
let version = ''
let origVersions = versions
// return all results that are likely semver compatible versions
versions = origVersions.filter(version => /\d+\.\d+/.test(version))
// If no semver versions then look for single numbered versions
if (!versions.length) {
versions = origVersions.filter(version => /\d+/.test(version))
}
if (!pre) {
// remove pre-releases from array
versions = versions.filter(version => !/\d+-\w+/.test(version))
}
try {
// coerce to string then lowercase otherwise alpha > RC
version = versions.sort((a, b) =>
semver.rcompare(
`${a}`.toLowerCase(),
`${b}`.toLowerCase(),
/* loose */ true
)
)[0]
} catch (e) {
version = latestDottedVersion(versions)
}
if (version === undefined || version === null) {
origVersions = origVersions.sort()
version = origVersions[origVersions.length - 1]
}
return version
}
function listCompare(a, b) {
const alen = a.length,
blen = b.length
for (let i = 0; i < alen; i++) {
if (a[i] < b[i]) {
return -1
} else if (a[i] > b[i]) {
return 1
}
}
return alen - blen
}
// === Private helper functions ===
// Take a list of string versions.
// Return the latest, or undefined, if there are none.
function latestDottedVersion(versions) {
const len = versions.length
if (len === 0) {
return
}
let version = versions[0]
for (let i = 1; i < len; i++) {
if (compareDottedVersion(version, versions[i]) < 0) {
version = versions[i]
}
}
return version
}
// Take string versions.
// -1 if v1 < v2, 1 if v1 > v2, 0 otherwise.
function compareDottedVersion(v1, v2) {
const parts1 = /([0-9.]+)(.*)$/.exec(v1)
const parts2 = /([0-9.]+)(.*)$/.exec(v2)
if (parts1 != null && parts2 != null) {
const numbers1 = parts1[1]
const numbers2 = parts2[1]
const distinguisher1 = parts1[2]
const distinguisher2 = parts2[2]
const numlist1 = numbers1.split('.').map(e => +e)
const numlist2 = numbers2.split('.').map(e => +e)
const cmp = listCompare(numlist1, numlist2)
if (cmp !== 0) {
return cmp
} else {
return distinguisher1 < distinguisher2
? -1
: distinguisher1 > distinguisher2
? 1
: 0
}
}
return v1 < v2 ? -1 : v1 > v2 ? 1 : 0
}
// Slice the specified number of dotted parts from the given semver version.
// e.g. slice('2.4.7', 'minor') -> '2.4'
function slice(v, releaseType) {
if (!semver.valid(v, /* loose */ true)) {
return null
}
const major = semver.major(v, /* loose */ true)
const minor = semver.minor(v, /* loose */ true)
const patch = semver.patch(v, /* loose */ true)
const prerelease = semver.prerelease(v, /* loose */ true)
const dottedParts = {
major: [major],
minor: [major, minor],
patch: [major, minor, patch],
}[releaseType]
if (dottedParts === undefined) {
throw Error(`Unknown releaseType: ${releaseType}`)
}
const dotted = dottedParts.join('.')
if (prerelease) {
return `${dotted}-${prerelease.join('.')}`
} else {
return dotted
}
}
function minor(v) {
return slice(v, 'minor')
}
function rangeStart(v) {
const range = new semver.Range(v, /* loose */ true)
return range.set[0][0].semver.version
}
function renderVersionBadge({ version, tag, defaultLabel }) {
return {
label: tag ? `${defaultLabel}@${tag}` : undefined,
message: addv(version),
color: versionColor(version),
}
}
module.exports = {
latest,
listCompare,
slice,
minor,
rangeStart,
renderVersionBadge,
}