-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearcher.js
125 lines (107 loc) · 4.64 KB
/
searcher.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
async function getNodeJSON(pPackageName) {
//this table contains manual mapping for packages that are provided by packages
//that have another name (fore some reason)
let manualLookupTable = {
'initramfs': 'mkinitcpio',
'sh': 'bash',
'awk': 'gawk',
'udev': 'systemd',
'libltdl': 'libtool',
'libjpeg': 'libjpeg-turbo',
'opengl-driver': 'mesa'
}
//remove version declarations and comments because the api cannot handle that
let packageName = pPackageName.replace(/(>|>=|=).*/, '').replace(/:\s.*/, '');
//redefine the package name using manual lookups if necerssary
if (typeof manualLookupTable[packageName] !== 'undefined')
packageName = manualLookupTable[packageName];
//TODO: Once the bug is fixed, this should work without CORS Proxy
const proxy = 'https://archviz-proxy.herokuapp.com/'
//make the fetch request
return fetch(
//set the appropriate settings
proxy + "https://archlinux.org/packages/search/json/?name=" + packageName, {
method: 'GET',
mode: 'cors',
headers: {
'Accept': 'application/json',
}
}
)
//make the response into json first
.then(response => response.json())
.then(json => {
//try to get the dependencies trigger an error if they are absent
if (typeof json['results'][0]['depends'].values() === "undefined") throw Error;
return json['results'][0];
})
.catch(() => {
//log the error
console.error(`cannot resolve package ${packageName}`);
//if this was the root package search, set the error message box
if (packageName === document.getElementById('packageInput').value) {
document.getElementById('ErrorMessageBox').innerText =
`cannot resolve package "${packageName}"`;
}
//return an empty set so that iterating in the recursion will not break
return new Set();
});
}
function getDepsFromJSON(json) {
//set the error message to '' (because obviously something was found)
document.getElementById('ErrorMessageBox').innerText = '';
//check if the json is valid
if (typeof json['depends'] === 'undefined') return new Set();
//return the dependencys
const allDeps = new Set(json['depends'].values());
//include optional dependencies if wanted
if (document.getElementById('includeOptionalDepsCheckbox').checked) {
//iterate over all optional dependencies
for (const optionalDepWithComment of json['optdepends'].values()) {
//add them to all dependencies
allDeps.add(optionalDepWithComment.replace(/:\s.*/, ''));
}
}
//include make dependencies if wanted
if (document.getElementById('includeMakeDepsCheckbox').checked) {
//add the make dependencys
for (const makedep of json['optdepends'].values()) {
allDeps.add(makedep);
}
}
return allDeps;
}
async function addSubtree(rootNode, graph, cache) {
//retrieve node JSON
getNodeJSON(rootNode).then((nodeJSON) => {
if (nodeJSON.hasOwnProperty('depends')) {
const dependencies = getDepsFromJSON(nodeJSON);
//add the node to the graph while replacing the unnecessary additional text
graph.addNode(rootNode.replace(/:\s.*/, ''),
JSON.parse(`{"repository": "${nodeJSON['repo']}"}`));
//iterate over all those dependencies
for (const dependency of dependencies) {
//check if circular dependencies are allowd
if (document.getElementById('allowCircularDependencies').checked) {
//add a link from the rootnode to that dependency
graph.addLink(rootNode, dependency);
} else {
if (!cache.has(dependency)) {
graph.addLink(rootNode, dependency);
}
}
//check if the dependency is cached (if the subtree has already been drawn for it
if (!cache.has(dependency)) {
//initiate recursive call to build subtree for that dependency
addSubtree(
dependency,
graph,
cache
);
//add the dependency to the cache so that in the next loop iteration it will not be handled again
cache.add(dependency);
}
}
}
});
}