/
index.ts
57 lines (52 loc) · 1.81 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
import { parse } from 'node-html-parser';
import { ExternalHostError } from '../../types/errors/external-host-error';
import * as globalCache from '../../util/cache/global';
import { Http } from '../../util/http';
import { isVersion } from '../../versioning/ruby';
import { GetReleasesConfig, ReleaseResult } from '../common';
export const id = 'ruby-version';
const http = new Http(id);
const rubyVersionsUrl = 'https://www.ruby-lang.org/en/downloads/releases/';
export async function getReleases(
_config?: GetReleasesConfig
): Promise<ReleaseResult> {
// First check the persistent cache
const cacheNamespace = 'datasource-ruby-version';
const cachedResult = await globalCache.get<ReleaseResult>(
cacheNamespace,
'all'
);
// istanbul ignore if
if (cachedResult) {
return cachedResult;
}
try {
const res: ReleaseResult = {
homepage: 'https://www.ruby-lang.org',
sourceUrl: 'https://github.com/ruby/ruby',
releases: [],
};
const response = await http.get(rubyVersionsUrl);
const root: any = parse(response.body);
const rows = root.querySelector('.release-list').querySelectorAll('tr');
for (const row of rows) {
const columns: string[] = Array.from(
row.querySelectorAll('td').map((td) => td.innerHTML)
);
if (columns.length) {
const version = columns[0].replace('Ruby ', '');
if (isVersion(version)) {
const releaseTimestamp = columns[1];
const changelogUrl = columns[2]
.replace('<a href="', 'https://www.ruby-lang.org')
.replace('">more...</a>', '');
res.releases.push({ version, releaseTimestamp, changelogUrl });
}
}
}
await globalCache.set(cacheNamespace, 'all', res, 15);
return res;
} catch (err) {
throw new ExternalHostError(err);
}
}