UNPKG

3.09 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.createChangedPackagesGraph = void 0;
4const dep_graph_1 = require("./dep-graph");
5const builder_1 = require("./builder");
6const event_loop_spinner_1 = require("event-loop-spinner");
7/**
8 * Creates an induced subgraph of {@param graphB} with only packages
9 * that are not present in {@param graphA} or have a different version.
10 *
11 * @param graphA
12 * @param graphB
13 */
14async function createChangedPackagesGraph(graphA, graphB) {
15 const depGraph = graphB;
16 const graphAPackageIds = new Set(graphA.getDepPkgs().map(dep_graph_1.DepGraphImpl.getPkgId));
17 const addedOrUpdatedPackages = depGraph
18 .getDepPkgs()
19 .filter((pkg) => !graphAPackageIds.has(dep_graph_1.DepGraphImpl.getPkgId(pkg)));
20 const depGraphBuilder = new builder_1.DepGraphBuilder(depGraph.pkgManager, depGraph.rootPkg);
21 const parentQueue = [];
22 for (const changedPackage of addedOrUpdatedPackages) {
23 for (const changedNodeId of depGraph.getPkgNodeIds(changedPackage)) {
24 //we add all nodes with new and changed packages to the new graph.
25 //a newly added node will also have its dependencies added here, since they are "new".
26 depGraphBuilder.addPkgNode(depGraph.getNodePkg(changedNodeId), changedNodeId, getNodeInfo(depGraph, changedNodeId));
27 //Push all direct parents of the changed nodes to a queue to later build up a path to root from that node
28 for (const parentId of depGraph.getNodeParentsNodeIds(changedNodeId)) {
29 parentQueue.push([parentId, changedNodeId]);
30 if (event_loop_spinner_1.eventLoopSpinner.isStarving()) {
31 await event_loop_spinner_1.eventLoopSpinner.spin();
32 }
33 }
34 }
35 }
36 //add direct and transitive parents for the changed nodes
37 const visited = new Set([depGraph.rootNodeId]);
38 while (parentQueue.length > 0) {
39 const [nodeId, dependencyNodeId] = parentQueue.pop();
40 if (visited.has(nodeId)) {
41 //ensure we link parents even if visited through another path
42 depGraphBuilder.connectDep(nodeId, dependencyNodeId);
43 continue;
44 }
45 visited.add(nodeId);
46 depGraphBuilder.addPkgNode(depGraph.getNodePkg(nodeId), nodeId, getNodeInfo(depGraph, nodeId));
47 depGraphBuilder.connectDep(nodeId, dependencyNodeId);
48 for (const parentId of depGraph.getNodeParentsNodeIds(nodeId)) {
49 parentQueue.push([parentId, nodeId]);
50 if (event_loop_spinner_1.eventLoopSpinner.isStarving()) {
51 await event_loop_spinner_1.eventLoopSpinner.spin();
52 }
53 }
54 }
55 return depGraphBuilder.build();
56}
57exports.createChangedPackagesGraph = createChangedPackagesGraph;
58function getNodeInfo(depGraph, nodeId) {
59 const nodeInfo = depGraph.getNode(nodeId);
60 if (!nodeInfo || Object.keys(nodeInfo).length === 0) {
61 return undefined;
62 }
63 return nodeInfo;
64}
65//# sourceMappingURL=create-changed-packages-graph.js.map
\No newline at end of file