UNPKG

4.83 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.buildDepGraphPnpm = void 0;
4const dep_graph_1 = require("@snyk/dep-graph");
5const util_1 = require("../util");
6const utils_1 = require("./utils");
7const event_loop_spinner_1 = require("event-loop-spinner");
8const error_catalog_nodejs_public_1 = require("@snyk/error-catalog-nodejs-public");
9const out_of_sync_error_1 = require("../../errors/out-of-sync-error");
10const __1 = require("../..");
11const debugModule = require("debug");
12const debug = debugModule('snyk-pnpm-workspaces');
13const buildDepGraphPnpm = async (lockFileParser, pkgJson, options, importer) => {
14 var _a;
15 const { strictOutOfSync, includeOptionalDeps, includeDevDeps, pruneWithinTopLevelDeps, } = options;
16 const depGraphBuilder = new dep_graph_1.DepGraphBuilder({ name: 'pnpm' }, { name: pkgJson.name, version: pkgJson.version });
17 lockFileParser.extractedPackages = lockFileParser.extractPackages();
18 const extractedPnpmPkgs = lockFileParser.extractedPackages;
19 const topLevelDeps = (0, util_1.getTopLevelDeps)(pkgJson, options);
20 const extractedTopLevelDeps = lockFileParser.extractTopLevelDependencies(options, importer) || {};
21 for (const name of Object.keys(topLevelDeps)) {
22 if (!extractedTopLevelDeps[name]) {
23 const errMessage = `Dependency ${name} was not found in ` +
24 `${out_of_sync_error_1.LOCK_FILE_NAME[__1.LockfileType.pnpm]}. Your package.json and ` +
25 `${out_of_sync_error_1.LOCK_FILE_NAME[__1.LockfileType.pnpm]} are probably out of sync. Please run ` +
26 `"${out_of_sync_error_1.INSTALL_COMMAND[__1.LockfileType.pnpm]}" and try again.`;
27 debug(errMessage);
28 throw new error_catalog_nodejs_public_1.OpenSourceEcosystems.PnpmOutOfSyncError(errMessage);
29 }
30 topLevelDeps[name].version = extractedTopLevelDeps[name].version;
31 }
32 const rootNode = {
33 id: 'root-node',
34 name: pkgJson.name,
35 version: pkgJson.version,
36 dependencies: topLevelDeps,
37 isDev: false,
38 };
39 const rootNodeId = `${pkgJson.name}@${pkgJson.version}`;
40 await dfsVisit(depGraphBuilder, rootNodeId, rootNode, extractedPnpmPkgs, strictOutOfSync, includeOptionalDeps, includeDevDeps, ((_a = pkgJson.pnpm) === null || _a === void 0 ? void 0 : _a.overrides) || {}, pruneWithinTopLevelDeps, lockFileParser);
41 return depGraphBuilder.build();
42};
43exports.buildDepGraphPnpm = buildDepGraphPnpm;
44/**
45 * Use DFS to add all nodes and edges to the depGraphBuilder and prune cyclic nodes.
46 * The visitedMap keep track of which nodes have already been discovered during traversal.
47 * - If a node doesn't exist in the map, it means it hasn't been visited.
48 * - If a node is already visited, simply connect the new node with this node.
49 */
50const dfsVisit = async (depGraphBuilder, rootNodeId, node, extractedPnpmPkgs, strictOutOfSync, includeOptionalDeps, includeDevDeps, overrides, pruneWithinTopLevel, lockFileParser, visited) => {
51 for (const [name, depInfo] of Object.entries(node.dependencies || {})) {
52 if (event_loop_spinner_1.eventLoopSpinner.isStarving()) {
53 await event_loop_spinner_1.eventLoopSpinner.spin();
54 }
55 const localVisited = visited || new Set();
56 const childNode = (0, utils_1.getPnpmChildNode)(name, depInfo, extractedPnpmPkgs, strictOutOfSync, includeOptionalDeps, includeDevDeps, lockFileParser);
57 if (localVisited.has(childNode.id) || childNode.id == rootNodeId) {
58 if (pruneWithinTopLevel) {
59 const prunedId = `${childNode.id}:pruned`;
60 depGraphBuilder.addPkgNode({ name: childNode.name, version: childNode.version }, prunedId, {
61 labels: Object.assign({ scope: childNode.isDev ? 'dev' : 'prod', pruned: 'true' }, (node.missingLockFileEntry && {
62 missingLockFileEntry: 'true',
63 })),
64 });
65 depGraphBuilder.connectDep(node.id, prunedId);
66 }
67 else {
68 depGraphBuilder.connectDep(node.id, childNode.id);
69 }
70 continue;
71 }
72 depGraphBuilder.addPkgNode({ name: childNode.name, version: childNode.version }, childNode.id, {
73 labels: Object.assign({ scope: childNode.isDev ? 'dev' : 'prod' }, (node.missingLockFileEntry && {
74 missingLockFileEntry: 'true',
75 })),
76 });
77 depGraphBuilder.connectDep(node.id, childNode.id);
78 localVisited.add(childNode.id);
79 await dfsVisit(depGraphBuilder, rootNodeId, childNode, extractedPnpmPkgs, strictOutOfSync, includeOptionalDeps, includeDevDeps, overrides, pruneWithinTopLevel, lockFileParser, localVisited);
80 }
81};
82//# sourceMappingURL=build-dep-graph-pnpm.js.map
\No newline at end of file