-
Notifications
You must be signed in to change notification settings - Fork 2
/
dependencies.js
82 lines (64 loc) · 2.74 KB
/
dependencies.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
import { graph } from "./deps/lively.lang.min.js"
export {
buildStages,
depGraph,
graphvizDeps
}
function buildStages(packageSpec, packageMap, dependencyFields) {
let {name, version} = packageSpec,
{deps, packages: packageDeps, resolvedVersions} = depGraph(packageSpec, packageMap);
for (let dep in deps)
for (let i = 0; i < deps[dep].length; i++)
if (!deps[deps[dep][i]]) deps[dep][i] = resolvedVersions[deps[dep][i]];
return lively.lang.graph.sortByReference(deps, `${name}@${version}`);
}
function depGraph(packageSpec, packageMap, dependencyFields = ["dependencies"]) {
// console.log(lively.lang.string.indent(pNameAndVersion, " ", depth));
// let packages = getInstalledPackages(centralPackageDir);
let pNameAndVersion = `${packageSpec.name}@${packageSpec.version}`,
queue = [pNameAndVersion],
resolvedVersions = {},
deps = {}, packages = {};
while (queue.length) {
let nameAndVersion = queue.shift();
if (nameAndVersion in resolvedVersions) continue;
let atIndex = nameAndVersion.lastIndexOf("@");
if (atIndex === -1) atIndex = nameAndVersion.length;
let name = nameAndVersion.slice(0, atIndex),
version = nameAndVersion.slice(atIndex+1),
pSpec = packageMap.lookup(name, version);
if (!pSpec) throw new Error(`Cannot resolve package ${nameAndVersion}`);
let resolvedNameAndVersion = `${pSpec.name}@${pSpec.version}`;
resolvedVersions[nameAndVersion] = resolvedNameAndVersion;
if (!packages[pSpec.name]) packages[pSpec.name] = [];
if (!packages[pSpec.name].includes(resolvedNameAndVersion))
packages[pSpec.name].push(resolvedNameAndVersion);
if (!deps[resolvedNameAndVersion]) {
let localDeps = Object.assign({},
dependencyFields.reduce((map, key) =>
Object.assign(map, pSpec[key]), {}));
deps[resolvedNameAndVersion] = Object.keys(localDeps).map(name => {
let fullName = name + "@" + localDeps[name];
queue.push(fullName);
return fullName;
});
}
}
return {deps, packages, resolvedVersions};
}
function graphvizDeps({deps, packages, resolvedVersions}) {
let graph = `digraph {\n`
+ `compound=true;\n`
+ `node [shape=record fontsize=10 fontname="Verdana"];\n`;
Object.keys(packages).forEach(pName => {
graph += `subgraph "cluster_${pName}" {\n`
+ `style=filled;\ncolor=lightgrey;\n`
+ packages[pName].map(nameAndVersion => `"${nameAndVersion}";`).join("\n")
+ `\n}\n`;
});
graph += Object.keys(deps).map(nameAndVersion =>
deps[nameAndVersion].map(depVersion =>
`"${nameAndVersion}" -> "${resolvedVersions[depVersion]}";`).join("\n")).join("\n") + "\n"
graph += "\n}\n";
return graph;
}