-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.js
193 lines (163 loc) · 5.1 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
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
'use strict'
const got = require('got')
const semver = require('semver')
const _cache = new Map()
module.exports = async function (alias = 'lts_active', opts = {}) {
const now = opts.now || new Date()
const cache = opts.cache || _cache
const mirror = opts.mirror || 'https://nodejs.org/dist/'
const latestOfMajorOnly = opts.latestOfMajorOnly || false
const a = Array.isArray(alias) ? alias : [alias]
const versions = await getLatestVersionsByCodename(now, cache, mirror)
// Reduce to an object
let m = a.reduce((m, a) => {
const vers = resolveAlias(versions, a)
if (Array.isArray(vers)) {
vers.forEach((v) => {
m[v.version] = v
})
} else if (vers) {
m[vers.version] = vers
}
return m
}, {})
// If only latest major is true, filter out all but latest
if (latestOfMajorOnly) {
const vers = Object.values(m).reduce((latestMajor, v) => {
// If highest in version range, save off
if (!latestMajor[v.major] || semver.gt(v.version, latestMajor[v.major].version)) {
latestMajor[v.major] = v
}
return latestMajor
}, {})
m = Array.from(Object.values(vers))
}
// Sort and pluck version
return Object.values(m).sort((v1, v2) => {
return semver.gt(v1.version, v2.version) ? 1 : -1
})
}
function resolveAlias (versions, alias) {
if (typeof alias === 'number' && versions[`v${alias}`]) {
return versions[`v${alias}`]
}
if (typeof alias === 'string') {
if (versions[alias.toLowerCase()]) {
return versions[alias.toLowerCase()]
}
// Alias might be a semver range
return versions.all.filter((v) => semver.satisfies(v.version, alias))
}
}
function getSchedule (cache) {
return got('https://raw.githubusercontent.com/nodejs/Release/master/schedule.json', {
cache
}).json()
}
function getVersions (cache, mirror) {
return got(mirror.replace(/\/$/, '') + '/index.json', {
cache
}).json()
}
async function getLatestVersionsByCodename (now, cache, mirror) {
const schedule = await getSchedule(cache)
const versions = await getVersions(cache, mirror)
// Composite aliases point to the HEAD for each release line
const supported = {}
const active = {}
const ltsActive = {}
const lts = {}
const aliases = versions.reduce((obj, ver) => {
const { major, minor, patch, tag } = splitVersion(ver.version)
const versionName = major !== '0' ? `v${major}` : `v${major}.${minor}`
const codename = ver.lts ? ver.lts.toLowerCase() : versionName
const version = tag !== '' ? `${major}.${minor}.${patch}-${tag}` : `${major}.${minor}.${patch}`
const s = schedule[versionName]
// Version Object
const v = {
version,
major,
minor,
patch,
tag,
codename,
versionName,
start: s && s.start && new Date(s.start),
lts: s && s.lts && new Date(s.lts),
maintenance: s && s.maintenance && new Date(s.maintenance),
end: s && s.end && new Date(s.end),
releaseDate: new Date(ver.date),
isLts: false,
files: ver.files || [],
dependencies: {
npm: ver.npm,
v8: ver.v8,
uv: ver.uv,
zlib: ver.zlib,
openssl: ver.openssl
}
}
// All versions get added to all
obj.all.push(v)
// Add specific version references
obj[v.version] = obj[`v${v.version}`] = v
// The new version is higher than the last stored version for this release line
if (!obj[versionName] || semver.gt(ver.version, obj[versionName].version)) {
// Version and codename alias
obj[versionName] = obj[codename] = v
if (now > v.start && now < v.end) {
supported[versionName] = v
if (now < v.maintenance) {
active[versionName] = v
}
// Latest lts
if (now > v.lts) {
lts[versionName] = v
v.isLts = true
if (now < v.maintenance) {
ltsActive[versionName] = v
}
if (!obj.lts_latest || semver.gt(ver.version, obj.lts_latest.version)) {
obj.lts_latest = v
}
}
// The newest supported release
if (!obj.current || semver.gt(ver.version, obj.current.version)) {
obj.current = v
}
}
}
return obj
}, {
all: []
})
// Add composite aliases
;[
['supported', supported],
['active', active],
['lts_active', ltsActive],
['lts', lts]
].forEach(([alias, vers]) => {
aliases[alias] = Object.values(vers)
})
// nvm --lts='lts/*'
aliases['lts/*'] = aliases.lts_latest
// nvm 'node'
aliases.node = aliases.current
// Deprecated maintained alias
let deprecationShown = false
Object.defineProperty(aliases, 'maintained', {
get: () => {
if (!deprecationShown) {
deprecationShown = true
console.warn(new Error('maintained is deprecated, use supported'))
}
return Object.values(supported)
}
})
return aliases
}
function splitVersion (ver) {
const [, major, minor, patch, tag] = /^v([0-9]*)\.([0-9]*)\.([0-9]*)(?:-([0-9A-Za-z-_]+))?/.exec(ver).map((n, i) => i < 4 ? parseInt(n, 10) : n || '')
return { major, minor, patch, tag }
}