-
Notifications
You must be signed in to change notification settings - Fork 0
/
depstree-latest-version-checker.js
executable file
·127 lines (109 loc) · 3.64 KB
/
depstree-latest-version-checker.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
#!/usr/bin/env node
const https = require('https');
const readline = require('readline');
const fs = require('fs');
const args = process.argv;
const ignorePackagesIndex = args.findIndex(arg => arg === '--ignore-packages') + 1;
const ignorePackages = ignorePackagesIndex > 0 && args.length > ignorePackagesIndex
? args[ignorePackagesIndex].split(",")
: undefined;
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
terminal: false
});
let deps = {};
let depsArray = [];
function getLatestVersion(groupId, artifactId) {
return new Promise((resolve, reject) => {
const url = `https://search.maven.org/solrsearch/select?q=g:${groupId}+AND+a:${artifactId}&rows=1&wt=json`;
const options = {
hostname: 'search.maven.org',
path: `https://search.maven.org/solrsearch/select?q=g:${groupId}+AND+a:${artifactId}&rows=1&wt=json`,
method: 'GET',
accept: 'application/json',
headers: {
'User-Agent': 'depstree-latest-version-checker/0.0.1'
}
};
https.get(url, options, (res) => {
let data = '';
res.on('data', (chunk) => {
data += chunk;
});
res.on('end', () => {
const jsonData = JSON.parse(data);
const latestVersion = jsonData.response?.docs[0]?.latestVersion;
if(!latestVersion) {
console.error("Failed to parse latest version from response: ", jsonData);
}
resolve(latestVersion);
});
}).on('error', (error) => {
reject(error);
});
}
);
}
const CONCURRENCY_LIMIT = 1;
let inProgress = 0;
const results = {};
async function processDependency({groupId, artifactId}) {
const latestVersion = await getLatestVersion(groupId, artifactId);
results[`${groupId}:${artifactId}`] = {'latestVersion': latestVersion};
}
async function worker() {
while (depsArray.length > 0) {
const dependency = depsArray.shift();
inProgress++;
await processDependency(dependency)
.catch(err => console.error(err))
.finally(() => {
inProgress--;
checkCompletion();
});
}
}
function checkCompletion() {
if (depsArray.length === 0 && inProgress === 0) {
console.log("All workers have finished.");
console.log("Results:", JSON.stringify(results));
const fileData = {
'date': new Date(),
'packages': results
};
let fileName = 'latestVersions.json';
fs.writeFileSync(fileName, JSON.stringify(fileData, null, 2), 'utf8');
console.log("Results written to " + fileName)
}
}
rl.on('line', function (line) {
const match = line.match(/INFO]([\s\\+\\\-|]*)([\w\.\-]+):([\w\.\-]+):([\w\.\-]+):([\w\.\-]+):?(\w+)?$/);
if (match) {
const level = match[1].length;
const groupId = match[2];
const artifactId = match[3];
const packaging = match[4];
const version = match[5];
const scope = match[6];
const newNode = {groupId, artifactId, version};
const artifactName = `${newNode.groupId}:${newNode.artifactId}:${newNode.version}`;
if (ignorePackages && ignorePackages.find(ignoredPackage => artifactName.startsWith(ignoredPackage))) {
return;
}
deps[artifactName] = newNode;
}
});
rl.on('close', function () {
depsArray = Object.values(deps);
depsArray.sort((a, b) => {
const fullNameA = `${a.groupId}:${a.artifactId}:${a.version}`;
const fullNameB = `${b.groupId}:${b.artifactId}:${b.version}`;
return fullNameA.localeCompare(fullNameB);
})
console.error(`${depsArray.length} dependencies read successfully, now starting to query them...`);
// Start workers
for (let i = 0; i < CONCURRENCY_LIMIT; i++) {
worker();
}
});