-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.ts
340 lines (279 loc) · 7.36 KB
/
index.ts
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
import semver from 'semver';
import isOnlineCheck from 'is-online';
type Filters = {
// Wether to include Nightly inside the list
includeNightly?: boolean
// Wether to include Beta inside the list
includeBeta?: boolean
// Wether to include Release Candidates inside the list
includeReleaseCandidates?: boolean
// Wether to include intermediate versions (only valid for the "range" method)
includeIntermediates?: boolean
}
// const tempPath = (name: string) => join(__dirname, `temp-${name}.json`)
abstract class RuntimeBase<T> {
abstract name: 'node' | 'nw.js' | 'electron'
abstract url: string
cache: T | null = null
abstract matcher(): Promise<Version[]>
protected async getVersions(): Promise<NonNullable<this['cache']>> {
if (this.cache) {
return this.cache;
}
const isOnline = await isOnlineCheck()
if (isOnline) {
const resRAW = await fetch(this.url);
const res = await resRAW.json()
const json = (await res) as NonNullable<this['cache']>;
// await writeFile(tempPath(this.name), JSON.stringify(json), 'utf-8')
this.cache = json;
return json;
} else {
// const jsonRAW = await readFile(tempPath(this.name), 'utf-8')
// const json = JSON.parse(jsonRAW) as NonNullable<this['cache']>
// this.cache = json;
// return json;
throw new Error("Internet connection unavailable. Please try again later");
}
}
}
export interface ElectronJSONItem {
version: string
date: string
node: string
v8: string
uv: string
zlib: string
openssl: string
modules: string
chrome: string
files: string[]
}
type ElectronJSON = ElectronJSONItem[]
class ElectronRuntime extends RuntimeBase<ElectronJSON> {
name: RuntimeBase<any>['name'] = 'electron'
url = 'https://releases.electronjs.org/releases.json'
async matcher() {
const versions = await this.getVersions()
const result = versions.map(version => {
const v: Version = {
version: version.version,
abi: version.modules ? parseInt(version.modules, 10) : 0
}
return v;
});
return result
}
}
export interface NWJSJSONItem {
latest: string
stable: string
lts: string
versions: {
version: string
date: string
files: string[]
flavors: string[]
components: {
node: string
chromium: string
}
}[]
}
type NWJSJSON = NWJSJSONItem
class NWJSRuntime extends RuntimeBase<NWJSJSON> {
name: RuntimeBase<any>['name'] = 'nw.js'
url = 'https://raw.githubusercontent.com/nwjs/website/master/src/versions.json'
async matcher() {
const versions = await this.getVersions()
const finalVersions = [];
for (const version of versions.versions) {
const nodeVersion = version.components.node;
try {
// eslint-disable-next-line no-await-in-loop
const nodeAbi = await getAbi(nodeVersion, 'node');
if (nodeAbi) {
const val: Version = {
version: version.version.replace('v', ''),
abi: nodeAbi
};
finalVersions.push(val);
}
} catch (e) {
// console.error('aaaa')
}
}
return finalVersions;
}
}
export interface NodeJSONItem {
version: string
date: string
files: string[]
npm?: string
v8: string
uv?: string
zlib?: string
openssl?: string
modules?: string
lts: any
security: boolean
}
type NodeJSON = NodeJSONItem[]
class NodeRuntime extends RuntimeBase<NodeJSON> {
name: RuntimeBase<any>['name'] = 'node'
url = 'https://nodejs.org/dist/index.json'
async matcher() {
const versions = await this.getVersions()
return versions.map(version => {
return ({
version: version.version.replace('v', ''),
abi: parseInt(version.modules ?? '0', 10)
});
});
}
}
type Version = {
version: string
abi: number
}
type Runtime = ElectronRuntime | NodeRuntime | NWJSRuntime
export const runtimes: [ElectronRuntime, NodeRuntime, NWJSRuntime] = [
new ElectronRuntime(),
new NodeRuntime(),
new NWJSRuntime(),
]
const _findRuntime = (runtime: Runtime['name']) => {
return runtimes.find(r => r.name === runtime);
}
const _filterBeta = (elem: Version) => {
return !elem.version.includes('beta');
}
const _filterNightly = (elem: Version) => {
return !elem.version.includes('nightly');
}
const _filterRC = (elem: Version) => {
return !elem.version.includes('rc');
}
export const getAbi = async (version: string, runtime: Runtime['name']): Promise<number> => {
const matchedRuntime = _findRuntime(runtime);
if (!matchedRuntime) {
throw new Error('Runtime not found')
}
const versions = await matchedRuntime.matcher();
const found = versions.find(v => v.version === version);
if (found) {
return found.abi;
}
throw new Error(`Version ${runtime}@${version} not found`)
}
export const getTarget = async (abi: number, runtime: Runtime['name']) => {
const matchedRuntime = _findRuntime(runtime);
if (!matchedRuntime) {
throw new Error('Runtime not found')
}
const versions = await matchedRuntime.matcher();
const found = versions
.filter(v => v.abi === abi)
.sort((a, b) => semver.compare(b.version, a.version));
const firstElement = found[0]
if (firstElement) {
return found[0].version;
}
throw new Error('Target not found')
}
export const getRange = async (
abi: number,
runtime: Runtime['name'],
filters?: Filters
) => {
const {
includeNightly = false,
includeBeta = false,
includeReleaseCandidates = false,
includeIntermediates = false,
} = filters ?? {}
const matchedRuntime = _findRuntime(runtime);
if (!matchedRuntime) {
throw new Error('Runtime not found')
}
const versions = await matchedRuntime.matcher();
let found = versions
.filter(v => v.abi === abi)
.sort((a, b) => semver.compare(a.version, b.version));
if (!includeBeta) {
found = found.filter(_filterBeta);
}
if (!includeNightly) {
found = found.filter(_filterNightly);
}
if (!includeReleaseCandidates) {
found = found.filter(_filterRC);
}
if (includeIntermediates) {
return found.map(x => x.version);
}
if (found.length === 0) {
throw new Error('Range not found')
}
const firstElement = found[0]
const lastElement = found[found.length - 1]
if (firstElement && lastElement) {
return [firstElement.version, lastElement.version];
}
throw new Error('Range not found')
}
type FinalVersions = (Version & { runtime: Runtime['name'] })[]
export const getAll = async (filters?: Filters) => {
const {
includeNightly = false,
includeBeta = false,
includeReleaseCandidates = false
} = filters ?? {}
let versions: FinalVersions = [];
for (let runtime of runtimes) {
const matchedRuntime = _findRuntime(runtime.name);
if (!matchedRuntime) {
throw new Error('Runtime not found')
}
const vs = await matchedRuntime.matcher();
vs.forEach(e => {
versions.push({
abi: e.abi,
runtime: runtime.name,
version: e.version,
})
});
}
if (!includeBeta) {
versions = versions.filter(_filterBeta);
}
if (!includeNightly) {
versions = versions.filter(_filterNightly);
}
if (!includeReleaseCandidates) {
versions = versions.filter(_filterRC);
}
return versions;
}
/**
* Return all the runtimes associated with a version
*/
export async function getRuntime(
target: string,
raw: true
): Promise<FinalVersions>
export async function getRuntime(
target: string,
raw: false
): Promise<Runtime['name'][]>
export async function getRuntime(
target: string,
raw: boolean
): Promise<FinalVersions | Runtime['name'][]> {
const versions = await getAll();
if (raw) {
return versions.filter(v => v.version === target);
}
return versions.filter(v => v.version === target).map(v => v.runtime);
}