-
Notifications
You must be signed in to change notification settings - Fork 67
Expand file tree
/
Copy pathversion.js
More file actions
87 lines (73 loc) · 2.07 KB
/
Copy pathversion.js
File metadata and controls
87 lines (73 loc) · 2.07 KB
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
var semver = require('semver')
var unstablePattern = /[a-z+-]/i
/**
* Determine if a version is a stable version or not.
* @param {String} version
* @return {Boolean}
*/
function isStable (version) {
return !unstablePattern.test(version || '')
}
exports.isStable = isStable
/**
* Determine if a version is a SCM URL or not.
* @param {String} version
* @return {Boolean}
*/
function isScm (version) {
var scmPrefixes = ['git:', 'git+ssh:', 'https:', 'git+https:']
var blacklisted = scmPrefixes.filter(function (prefix) {
return version.indexOf(prefix) === 0
})
return !!blacklisted.length
}
exports.isScm = isScm
/**
* Get the latest stable version from a list of versions in ascending order.
* @param {Array} versions
* @return {String}
*/
function getLatestStable (versions) {
versions = versions.slice()
while (versions.length) {
var version = versions.pop()
if (isStable(version)) {
return version
}
}
return null
}
exports.getLatestStable = getLatestStable
/**
* Get the latest version and latest stable version
* @param {String} current The version you get when you `npm install [package]`
* @param {Array} versions All versions available
* @param {Object} opts Options
* @param {Boolean} [opts.loose] Use loose option when querying semver
*/
function getLatest (current, versions, opts) {
var stable = current
var latest = versions[versions.length - 1]
if (!isStable(stable)) {
stable = getLatestStable(versions)
}
// getLatestStable might not have found a stable version
if (stable) {
// Latest is the most recent version with higher version than stable
for (var i = versions.length - 1; i >= 0; i--) {
// If !opts.loose then this may throw
if (semver.gt(versions[i], stable, opts.loose)) {
latest = versions[i]
break
}
}
}
return { latest, stable }
}
exports.getLatest = getLatest
function getVersionsInRange (range, versions, loose) {
return versions.filter(function (v) {
return semver.satisfies(v, range, loose)
})
}
exports.getVersionsInRange = getVersionsInRange