UNPKG

3.09 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.buildDepGraphYarnLockV1Workspace = void 0;
4const dep_graph_1 = require("@snyk/dep-graph");
5const util_1 = require("../util");
6const util_2 = require("./util");
7const event_loop_spinner_1 = require("event-loop-spinner");
8const buildDepGraphYarnLockV1Workspace = async (extractedYarnLockV1Pkgs, pkgJson, workspacePkgNameToVersion, options) => {
9 const { includeDevDeps, strictOutOfSync, includeOptionalDeps } = options;
10 const depGraphBuilder = new dep_graph_1.DepGraphBuilder({ name: 'yarn' }, { name: pkgJson.name, version: pkgJson.version });
11 const visitedMap = new Set();
12 const topLevelDeps = (0, util_1.getTopLevelDeps)(pkgJson, { includeDevDeps });
13 const rootNode = {
14 id: 'root-node',
15 name: pkgJson.name,
16 version: pkgJson.version,
17 dependencies: topLevelDeps,
18 isDev: false,
19 };
20 await dfsVisit(depGraphBuilder, rootNode, visitedMap, extractedYarnLockV1Pkgs, workspacePkgNameToVersion, strictOutOfSync, includeOptionalDeps);
21 return depGraphBuilder.build();
22};
23exports.buildDepGraphYarnLockV1Workspace = buildDepGraphYarnLockV1Workspace;
24/**
25 * Use DFS to add all nodes and edges to the depGraphBuilder and prune cyclic nodes.
26 * The colorMap keep track of the state of node during traversal.
27 * - If a node doesn't exist in the map, it means it hasn't been visited.
28 * - If a node is GRAY, it means it has already been discovered but its subtree hasn't been fully traversed.
29 * - If a node is BLACK, it means its subtree has already been fully traversed.
30 * - When first exploring an edge, if it points to a GRAY node, a cycle is found and the GRAY node is pruned.
31 * - A pruned node has id `${originalId}|1`
32 * When coming across another workspace package as child node, simply add the node and edge to the graph and mark it as BLACK.
33 */
34const dfsVisit = async (depGraphBuilder, node, visitedMap, extractedYarnLockV1Pkgs, workspacePkgNameToVersion, strictOutOfSync, includeOptionalDeps) => {
35 visitedMap.add(node.id);
36 for (const [name, depInfo] of Object.entries(node.dependencies || {})) {
37 if (event_loop_spinner_1.eventLoopSpinner.isStarving()) {
38 await event_loop_spinner_1.eventLoopSpinner.spin();
39 }
40 const isWorkspacePkg = !!workspacePkgNameToVersion[name];
41 const childNode = (0, util_2.getChildNodeYarnLockV1Workspace)(name, depInfo, workspacePkgNameToVersion, extractedYarnLockV1Pkgs, strictOutOfSync, includeOptionalDeps);
42 if (!visitedMap.has(childNode.id)) {
43 (0, util_1.addPkgNodeToGraph)(depGraphBuilder, childNode, {
44 isCyclic: false,
45 isWorkspacePkg,
46 });
47 if (!isWorkspacePkg) {
48 await dfsVisit(depGraphBuilder, childNode, visitedMap, extractedYarnLockV1Pkgs, workspacePkgNameToVersion, strictOutOfSync, includeOptionalDeps);
49 }
50 }
51 depGraphBuilder.connectDep(node.id, childNode.id);
52 }
53};
54//# sourceMappingURL=build-depgraph-workspace-package.js.map
\No newline at end of file