forked from jupyterlab/jupyterlab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dependency-graph.ts
294 lines (266 loc) · 7.76 KB
/
dependency-graph.ts
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/*-----------------------------------------------------------------------------
| Copyright (c) Jupyter Development Team.
| Distributed under the terms of the Modified BSD License.
|----------------------------------------------------------------------------*/
import * as fs from 'fs-extra';
import * as lockfile from '@yarnpkg/lockfile';
import * as path from 'path';
import * as utils from './utils';
import commander from 'commander';
/**
* Flatten a nested array one level.
*/
function flat(arr: any[]) {
return arr.reduce((acc, val) => acc.concat(val), []);
}
/**
* Parse the yarn file at the given path.
*/
function readYarn(basePath: string = '.') {
let file = fs.readFileSync(path.join(basePath, 'yarn.lock'), 'utf8');
let json = lockfile.parse(file);
if (json.type !== 'success') {
throw new Error('Error reading file');
}
return json.object;
}
/**
* Get a node name corresponding to package@versionspec.
*
* The nodes names are of the form "<package>@<resolved version>".
*
* Returns undefined if the package is not fund
*/
function getNode(yarnData: any, pkgName: string) {
if (!(pkgName in yarnData)) {
console.error(
`Could not find ${pkgName} in yarn.lock file. Ignore if this is a top-level package.`
);
return undefined;
}
let name = pkgName[0] + pkgName.slice(1).split('@')[0];
let version = yarnData[pkgName].version;
let pkgNode = `${name}@${version}`;
return pkgNode;
}
/**
* The type for graphs.
*
* Keys are nodes, values are the list of neighbors for the node.
*/
type Graph = { [key: string]: string[] };
/**
* Build a dependency graph based on the yarn data.
*/
function buildYarnGraph(yarnData: any): Graph | undefined {
// 'a': ['b', 'c'] means 'a' depends on 'b' and 'c'
const dependsOn: Graph = Object.create(null);
Object.keys(yarnData).forEach(pkgName => {
let pkg = yarnData[pkgName];
let pkgNode = getNode(yarnData, pkgName);
// If multiple version specs resolve to the same actual package version, we
// only want to record the dependency once.
if (dependsOn[pkgNode] !== undefined) {
return;
}
dependsOn[pkgNode] = [];
let deps = pkg.dependencies;
if (deps) {
Object.keys(deps).forEach(depName => {
let depNode = getNode(yarnData, `${depName}@${deps[depName]}`);
dependsOn[pkgNode].push(depNode);
});
}
});
return dependsOn;
}
/**
* Construct a subgraph of all nodes reachable from the given nodes.
*/
function subgraph(graph: Graph, nodes: string[]): Graph {
let sub = Object.create(null);
// Seed the graph
let newNodes = nodes;
while (newNodes.length > 0) {
let old = newNodes;
newNodes = [];
old.forEach(i => {
if (!(i in sub)) {
sub[i] = graph[i];
newNodes.push(...sub[i]);
}
});
}
return sub;
}
/**
* Return the package.json data at the given path
*/
function pkgData(packagePath: string) {
packagePath = path.join(packagePath, 'package.json');
let data: any;
try {
data = utils.readJSONFile(packagePath);
} catch (e) {
console.error('Skipping package ' + packagePath);
return {};
}
return data;
}
function convertDot(
g: { [key: string]: string[] },
graphOptions: string,
distinguishRoots = false,
distinguishLeaves = false
) {
let edges: string[][] = flat(
Object.keys(g).map(a => g[a].map(b => [a, b]))
).sort();
let nodes = Object.keys(g).sort();
// let leaves = Object.keys(g).filter(i => g[i].length === 0);
// let roots = Object.keys(g).filter(i => g[i].length === 0);
let dot = `
digraph DEPS {
${graphOptions || ''}
${nodes.map(node => `"${node}";`).join(' ')}
${edges.map(([a, b]) => `"${a}" -> "${b}"`).join('\n ')}
}
`;
return dot;
}
interface IMainOptions {
dependencies: boolean;
devDependencies: boolean;
jupyterlab: boolean;
lerna: boolean;
lernaExclude: string;
lernaInclude: string;
path: string;
phosphor: boolean;
topLevel: boolean;
}
function main({
dependencies,
devDependencies,
jupyterlab,
lerna,
lernaExclude,
lernaInclude,
path,
phosphor,
topLevel
}: IMainOptions) {
let yarnData = readYarn(path);
let graph = buildYarnGraph(yarnData);
let paths: string[] = [path];
if (lerna !== false) {
paths.push(...utils.getLernaPaths(path).sort());
}
// Get all package data
let data: any[] = paths.map(p => pkgData(p));
// Get top-level package names (these won't be listed in yarn)
const topLevelNames: Set<string> = new Set(data.map(d => d.name));
// Filter lerna packages if a regex was supplied
if (lernaInclude) {
let re = new RegExp(lernaInclude);
data = data.filter(d => d.name && d.name.match(re));
}
if (lernaExclude) {
let re = new RegExp(lernaExclude);
data = data.filter(d => d.name && !d.name.match(re));
}
const depKinds: string[] = [];
if (devDependencies) {
depKinds.push('devDependencies');
}
if (dependencies) {
depKinds.push('dependencies');
}
/**
* All dependency roots *except* other packages in this repo.
*/
const dependencyRoots: string[][] = data.map(d => {
let roots: string[] = [];
for (let depKind of depKinds) {
let deps = d[depKind];
if (deps === undefined) {
continue;
}
let nodes = Object.keys(deps)
.map(i => {
// Do not get a package if it is a top-level package (and this is
// not in yarn).
if (!topLevelNames.has(i)) {
return getNode(yarnData, `${i}@${deps[i]}`);
}
})
.filter(i => i !== undefined);
roots.push(...nodes);
}
return roots;
});
// Find the subgraph
let sub = subgraph(graph, flat(dependencyRoots));
// Add in top-level lerna packages if desired
if (topLevel) {
data.forEach((d, i) => {
sub[`${d.name}@${d.version}`] = dependencyRoots[i];
});
}
// Filter out *all* phosphor nodes
if (!phosphor) {
Object.keys(sub).forEach(v => {
sub[v] = sub[v].filter(w => !w.startsWith('@phosphor/'));
});
Object.keys(sub).forEach(v => {
if (v.startsWith('@phosphor/')) {
delete sub[v];
}
});
}
// Filter for any edges going into a jlab package, and then for any
// disconnected jlab packages. This preserves jlab packages in the graph that
// point to other packages, so we can see where third-party packages come
// from.
if (!jupyterlab) {
Object.keys(sub).forEach(v => {
sub[v] = sub[v].filter(w => !w.startsWith('@jupyterlab/'));
});
Object.keys(sub).forEach(v => {
if (v.startsWith('@jupyterlab/') && sub[v].length === 0) {
delete sub[v];
}
});
}
return sub;
}
commander
.description(`Print out the dependency graph in dot graph format.`)
.option('--lerna', 'Include dependencies in all lerna packages')
.option(
'--lerna-include <regex>',
'A regex for package names to include in dependency roots'
)
.option(
'--lerna-exclude <regex>',
'A regex for lerna package names to exclude from dependency roots (can override the include regex)'
)
.option('--path [path]', 'Path to package or monorepo to investigate', '.')
.option(
'--no-jupyterlab',
'Do not include dependency connections TO @jupyterlab org packages nor isolated @jupyterlab org packages'
)
.option('--no-phosphor', 'Do not include @phosphor org packages')
.option('--no-devDependencies', 'Do not include dev dependencies')
.option('--no-dependencies', 'Do not include normal dependencies')
.option('--no-top-level', 'Do not include the top-level packages')
.option(
'--graph-options <options>',
'dot graph options (such as "ratio=0.25; concentrate=true;")'
)
.action(args => {
let graph = main(args);
console.log(convertDot(graph, args.graphOptions));
console.error(`Nodes: ${Object.keys(graph).length}`);
});
commander.parse(process.argv);