-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild-graph.js
123 lines (106 loc) · 3.16 KB
/
build-graph.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
const fs = require('fs')
const createGraph = require('ngraph.graph')
const Versions = require('./versions')
const filePaths = fs.readFileSync(0).toString().trim().split('\n')
const readJson = (p) => {
return new Promise((resolve, reject) => {
fs.readFile(p, 'utf-8', (e, contents) => {
if (e) {
return reject(e)
}
try {
resolve(JSON.parse(contents))
} catch (e) {
reject(e)
}
})
})
}
;(async function () {
const graph = createGraph()
const packageLookup = {}
const insert = (path, pkg) => {
let [name, version] = parseFilePath(path)
if (pkg.name !== name) {
console.log(
`package name doesn't match: Published as ${name}@${version}, elm.json says ${pkg.name}`
)
}
if (pkg.version !== version) {
console.log(
`package version doesn't match: Published as ${name}@${version}, elm.json says ${pkg.version}`
)
}
if (!(name in packageLookup)) {
packageLookup[name] = {}
}
packageLookup[name][version] = pkg
console.log('add node!', pkgName({ name, version }))
graph.addNode(pkgName({ name, version }), pkg)
}
for (let i = 0; i < filePaths.length; i++) {
const json = await readJson(filePaths[i])
insert(filePaths[i], json)
}
for (let i = 0; i < filePaths.length; i++) {
let [name, version] = parseFilePath(filePaths[i])
if (!(name in packageLookup) || !(version in packageLookup[name])) {
// no package found
continue
}
// create a node for each
// console.log(`${name}@${version}`)
const deps = packageLookup[name][version].dependencies
for (let dep in deps) {
const isMatchingVersion = Versions.parseRange(deps[dep])
if (!(dep in packageLookup)) {
console.log('Unknown dep:', dep, deps[dep])
console.log('needed for:', name, version)
continue
}
const depVersion = Object.keys(packageLookup[dep]).find(isMatchingVersion)
if (!depVersion) {
console.log(`${name}@${version}`)
console.log('no matching version:', dep, deps[dep])
console.log('available:', Object.keys(packageLookup[dep]).join(', '))
continue
}
console.log(
'add link!',
pkgName({ name, version }),
'->',
pkgName({ name: dep, version: depVersion })
)
graph.addLink(
pkgName({ name, version }),
pkgName({ name: dep, version: depVersion })
)
// console.log(` ${key}@${depVersion}`)
}
}
// https://github.com/phiresky/crawl-arch/blob/master/layout.js
console.log(
'Loaded graph with ' +
graph.getLinksCount() +
' edges; ' +
graph.getNodesCount() +
' nodes'
)
const layout = require('ngraph.offline.layout')(graph)
console.log('Starting layout')
layout.run()
const save = require('ngraph.tobinary')
save(graph, {
outDir: './data',
})
console.log('Done.')
console.log(
'Copy `links.bin`, `labels.bin` and `positions.bin` into vis folder'
)
})()
const pkgName = (pkg) => `${pkg.name}@${pkg.version}`
const parseFilePath = (path) =>
path
.replace(/^\.\/packages\//, '')
.replace(/\.json$/, '')
.split('@')