-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshared.js
80 lines (72 loc) · 2.75 KB
/
shared.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
import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'fs';
import { parse } from 'node-html-parser';
export const cachePath = '.cache';
export const ensureDirExists = async () => {
if (!existsSync(cachePath)) {
mkdirSync(cachePath);
}
};
export const getJsonFromFile = async (filename) => {
try {
const data = readFileSync(filename, 'utf8');
return JSON.parse(data);
} catch (err) {
return [];
}
};
const getSpringDefaultVersions = async (springBootVersion) => {
try {
await ensureDirExists();
if (!existsSync(`${cachePath}/dependencies_${springBootVersion}.json`)) {
await downloadSpringDefaultVersions(springBootVersion);
// } else {
// console.log('Spring Boot default versions file already exists in cache.');
}
} catch (err) {
console.error('Error retrieving spring default versions', err);
}
};
const downloadSpringDefaultVersions = async (springBootVersion) => {
let url = `https://docs.spring.io/spring-boot/docs/${springBootVersion}/reference/html/dependency-versions.html`;
let response = await fetch(url);
// Handle new Spring Boot URL, count redirects as failures, and handle 3.3.+ gradle format
if (response.status === 404 || response.url.includes('redirect.html')) {
const springMinorVersion = springBootVersion.replace('.x', '');
url = `https://docs.spring.io/spring-boot/${springMinorVersion}/appendix/dependency-versions/coordinates.html`;
response = await fetch(url);
}
const versions = [];
if (response.ok) {
const template = await response.text();
const parsedTemplate = parse(template);
const tableBody = parsedTemplate.querySelector('table.tableblock tbody');
tableBody.childNodes.forEach(
(
child, // there's a header row we should skip
) =>
child.childNodes.length === 0
? ''
: versions.push({
group: child.childNodes[1].rawText,
name: child.childNodes[3].rawText,
version: child.childNodes[5].rawText,
}),
);
await writeFileSync(`${cachePath}/dependencies_${springBootVersion}.json`, JSON.stringify(versions, null, 2));
} else {
await writeFileSync(`${cachePath}/dependencies_${springBootVersion}.json`, JSON.stringify(versions, null, 2));
console.log('URL not found - Spring Boot default versions URL no longer exists.');
}
};
export const getDefaultSpringBootVersions = async (filename) => {
await getSpringDefaultVersions(filename);
return getJsonFromFile(`${cachePath}/dependencies_${filename}.json`);
};
export class Package {
constructor(group, name, inputFileVersion, bootVersion) {
this.group = group;
this.name = name;
this.inputFileVersion = inputFileVersion;
this.bootVersion = bootVersion;
}
}