UNPKG

3.35 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.buildDepGraphYarnLockV1Simple = void 0;
4const dep_graph_1 = require("@snyk/dep-graph");
5const util_1 = require("../util");
6const event_loop_spinner_1 = require("event-loop-spinner");
7const buildDepGraphYarnLockV1Simple = async (extractedYarnLockV1Pkgs, pkgJson, options) => {
8 const { includeDevDeps, includeOptionalDeps, includePeerDeps, strictOutOfSync, pruneWithinTopLevelDeps, } = options;
9 const depGraphBuilder = new dep_graph_1.DepGraphBuilder({ name: 'yarn' }, { name: pkgJson.name, version: pkgJson.version });
10 const topLevelDeps = (0, util_1.getTopLevelDeps)(pkgJson, {
11 includeDevDeps,
12 includePeerDeps,
13 includeOptionalDeps,
14 });
15 const rootNode = {
16 id: 'root-node',
17 name: pkgJson.name,
18 version: pkgJson.version,
19 dependencies: topLevelDeps,
20 isDev: false,
21 };
22 await dfsVisit(depGraphBuilder, rootNode, extractedYarnLockV1Pkgs, strictOutOfSync, includeOptionalDeps, pruneWithinTopLevelDeps);
23 return depGraphBuilder.build();
24};
25exports.buildDepGraphYarnLockV1Simple = buildDepGraphYarnLockV1Simple;
26/**
27 * Use DFS to add all nodes and edges to the depGraphBuilder and prune cyclic nodes.
28 * The visitedMap keep track of which nodes have already been discovered during traversal.
29 * - If a node doesn't exist in the map, it means it hasn't been visited.
30 * - If a node is already visited, simply connect the new node with this node.
31 */
32const dfsVisit = async (depGraphBuilder, node, extractedYarnLockV1Pkgs, strictOutOfSync, includeOptionalDeps, pruneWithinTopLevel, visited) => {
33 for (const [name, depInfo] of Object.entries(node.dependencies || {})) {
34 if (event_loop_spinner_1.eventLoopSpinner.isStarving()) {
35 await event_loop_spinner_1.eventLoopSpinner.spin();
36 }
37 const localVisited = visited || new Set();
38 const childNode = (0, util_1.getChildNode)(name, depInfo, extractedYarnLockV1Pkgs, strictOutOfSync, includeOptionalDeps);
39 if (localVisited.has(childNode.id)) {
40 if (pruneWithinTopLevel) {
41 const prunedId = `${childNode.id}:pruned`;
42 depGraphBuilder.addPkgNode({ name: childNode.name, version: childNode.version }, prunedId, {
43 labels: Object.assign({ scope: node.isDev ? 'dev' : 'prod', pruned: 'true' }, (node.missingLockFileEntry && {
44 missingLockFileEntry: 'true',
45 })),
46 });
47 depGraphBuilder.connectDep(node.id, prunedId);
48 }
49 else {
50 depGraphBuilder.connectDep(node.id, childNode.id);
51 }
52 continue;
53 }
54 depGraphBuilder.addPkgNode({ name: childNode.name, version: childNode.version }, childNode.id, {
55 labels: Object.assign({ scope: node.isDev ? 'dev' : 'prod' }, (node.missingLockFileEntry && {
56 missingLockFileEntry: 'true',
57 })),
58 });
59 depGraphBuilder.connectDep(node.id, childNode.id);
60 localVisited.add(childNode.id);
61 await dfsVisit(depGraphBuilder, childNode, extractedYarnLockV1Pkgs, strictOutOfSync, includeOptionalDeps, pruneWithinTopLevel, localVisited);
62 }
63};
64//# sourceMappingURL=build-depgraph-simple.js.map
\No newline at end of file