-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathgo-versions.js
More file actions
145 lines (137 loc) · 3.54 KB
/
go-versions.js
File metadata and controls
145 lines (137 loc) · 3.54 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
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
import fs from 'fs'
import fetch from 'node-fetch'
import semverCoerce from 'semver/functions/coerce.js'
import semverGte from 'semver/functions/gte.js'
import semverLte from 'semver/functions/lte.js'
const gomod = path => {
return fs.readFileSync(path, 'utf8')
}
const getGoModVersion = content => {
const r = /\ngo ([^\n]*)\n/s
const matches = r.exec(content)
if (matches === null) {
throw new Error('No go version in go.mod')
}
return matches[1]
}
const modulename = content => {
const r = /module ([^\n]*)\n/s
const matches = r.exec(content)
if (matches === null) {
throw new Error('No module path in go.mod')
}
return matches[1]
}
const getVersions = async withUnsupported => {
let url = 'https://go.dev/dl/?mode=json'
if (withUnsupported) {
url += '&include=all'
}
const response = await fetch(url)
if (!response.ok) {
throw new Error(
`Could not fetch Go versions from https://go.dev/dl/: ${response.status}`
)
}
const result = await response.json()
return result
}
const matrix = (
min,
withUnstable,
withPatchLevel,
withLatestPatches,
withStrictSemver,
tags
) => {
const minClean = semverCoerce(min)
if (minClean === null) {
throw new Error(`Minimal version isn't quite right: ${min}`)
}
if (withUnstable && withLatestPatches) {
throw new Error(
'The options "unstable" and "latest-patches-only" cannot be used together'
)
}
if (!withUnstable) {
tags = tags.filter(tag => tag.stable === true)
}
const r = /^go(.*)$/
let versions = tags.map(tag => {
const matches = r.exec(tag.version)
return matches ? matches[1] : tag.version
})
versions = versions.filter(v => {
const v2 = semverCoerce(v)
return v2 !== null && semverGte(v2, minClean)
})
if (withStrictSemver) {
versions = versions.map(version => {
// Alpha and beta versions on https://go.dev/dl are named like
// `1.18beta2`. We rewrite into strict semver syntax:
// `1.18.0-beta.2`.
version = version.replace(/([^0-9\\.]+)/, '-$1.')
return semverCoerce(version, {
includePrerelease: true
}).version
})
}
if (!withPatchLevel) {
versions = versions.map(version => {
const parts = version.split('.')
return `${parts[0]}.${parts[1]}`
})
} else if (withLatestPatches) {
// Group by major.minor and keep only the latest patch version
const grouped = {}
versions.forEach(version => {
const parts = version.split('.')
const majorMinor = `${parts[0]}.${parts[1]}`
if (!grouped[majorMinor]) {
grouped[majorMinor] = []
}
grouped[majorMinor].push(version)
})
versions = Object.values(grouped).map(group => {
return group.reduce((acc, val) => {
const a = semverCoerce(acc)
const v = semverCoerce(val)
if (v !== null && a !== null && semverGte(v, a)) {
return val
}
return acc
})
})
}
versions = [...new Set(versions)]
return versions.reverse()
}
const latest = versions => {
return versions.reduce((acc, val) => {
const a = semverCoerce(acc)
const v = semverCoerce(val)
if (v !== null && a !== null && semverGte(v, a)) {
return val
}
return acc
})
}
const minimal = versions => {
return versions.reduce((acc, val) => {
const a = semverCoerce(acc)
const v = semverCoerce(val)
if (v !== null && a !== null && semverLte(v, a)) {
return val
}
return acc
})
}
export {
getGoModVersion,
getVersions,
gomod,
latest,
matrix,
minimal,
modulename
}