-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
74 lines (56 loc) · 2.26 KB
/
index.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
const Arborist = require('@npmcli/arborist')
const semver = require('semver')
const data = {} // initialize data we'll return
async function mingine (options) {
const arboristOptions = {}
for (const option in options) {
if (option === 'path' || option === 'registry' || option === 'auth' || option === 'scopes') { // only use the options that arborist actually takes
arboristOptions[option] = options[option]
}
}
const arborist = new Arborist(arboristOptions)
const actualVersions = arborist.loadActual().then(tree => {
tree.children.forEach(packageMapHandler) // iterate over the chidlren Map() with the packageMapHandler method
}).then(() => {
return data // once iteration is completed, return data
})
return actualVersions
}
function packageMapHandler (value, key, map) {
const symbols = Object.getOwnPropertySymbols(value)
const children = map.get(key)
const enginesPropertyInPacakgeJSON = children[symbols[0]].engines // only extract the engines property
const namePropertyInPacakgeJSON = children[symbols[0]].name // only extract the name property
if (enginesPropertyInPacakgeJSON !== undefined) {
const presentEngines = Object.keys(enginesPropertyInPacakgeJSON)
presentEngines.forEach(engine => { // in an unopinionated way, iterate over the values for every single engine and build our API
const version = semverify(enginesPropertyInPacakgeJSON[engine]) // semverify normalizes semver ranges for our use case
if (data[engine] === undefined) {
data[engine] = {}
}
if (data[engine].versions === undefined) {
data[engine].versions = {}
}
if (data[engine].versions[version] === undefined) {
data[engine].versions[version] = []
}
if (data[engine].minimum === undefined) {
data[engine].minimum = version
}
if (semver.valid(semver.coerce(version)) !== null) {
if (semver.lt(data[engine].minimum, version)) {
data[engine].minimum = version
}
}
data[engine].versions[version].push(namePropertyInPacakgeJSON)
})
}
}
function semverify (versionlike) {
let cleaned = semver.valid(semver.coerce(versionlike))
if (cleaned === null) {
cleaned = versionlike
}
return cleaned
}
module.exports = mingine