1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.parsePnpmWorkspace = void 0;
|
4 | const debugModule = require("debug");
|
5 | const path = require("path");
|
6 | const util_1 = require("../util");
|
7 | const build_dep_graph_pnpm_1 = require("./build-dep-graph-pnpm");
|
8 | const index_1 = require("./lockfile-parser/index");
|
9 | const utils_1 = require("../../utils");
|
10 | const utils_2 = require("./utils");
|
11 | const debug = debugModule('snyk-pnpm-workspaces');
|
12 |
|
13 |
|
14 |
|
15 |
|
16 | function computeProjectVersionMaps(root, targets) {
|
17 | const projectsVersionMap = {};
|
18 | for (const target of targets) {
|
19 | const directory = path.join(root, target);
|
20 | const packageJsonFileName = path.join(directory, 'package.json');
|
21 | const packageJson = (0, utils_2.getFileContents)(root, packageJsonFileName);
|
22 | try {
|
23 | const parsedPkgJson = (0, util_1.parsePkgJson)(packageJson.content);
|
24 | projectsVersionMap[target] = {
|
25 | version: parsedPkgJson.version,
|
26 | name: parsedPkgJson.name,
|
27 | };
|
28 | }
|
29 | catch (err) {
|
30 | debug(`Error getting version for project: ${packageJsonFileName}. ERROR: ${err}`);
|
31 | continue;
|
32 | }
|
33 | }
|
34 | return projectsVersionMap;
|
35 | }
|
36 | const parsePnpmWorkspace = async (root, workspaceDir, options) => {
|
37 | const scannedProjects = [];
|
38 | const { includeDevDeps, includePeerDeps, includeOptionalDeps, strictOutOfSync, pruneWithinTopLevelDeps, } = options;
|
39 | const pnpmLockfileContents = (0, utils_2.getFileContents)(root, path.join(workspaceDir, 'pnpm-lock.yaml')).content;
|
40 | const lockfileVersion = (0, utils_1.getPnpmLockfileVersion)(pnpmLockfileContents);
|
41 | const lockFileParser = (0, index_1.getPnpmLockfileParser)(pnpmLockfileContents, lockfileVersion);
|
42 | const projectVersionsMaps = computeProjectVersionMaps(workspaceDir, Object.keys(lockFileParser.importers));
|
43 | for (const importer of Object.keys(lockFileParser.importers)) {
|
44 | const resolvedImporterPath = path.join(workspaceDir, importer);
|
45 | const packagePath = path.join(resolvedImporterPath, 'package.json');
|
46 | debug(`Processing project ${packagePath} as part of a pnpm workspace`);
|
47 | const pkgJsonFile = (0, utils_2.getFileContents)(root, packagePath);
|
48 | const pkgJson = (0, util_1.parsePkgJson)(pkgJsonFile.content);
|
49 | lockFileParser.workspaceArgs = {
|
50 | isWorkspace: true,
|
51 | projectsVersionMap: projectVersionsMaps,
|
52 | };
|
53 | try {
|
54 | const depGraph = await (0, build_dep_graph_pnpm_1.buildDepGraphPnpm)(lockFileParser, pkgJson, {
|
55 | includeDevDeps,
|
56 | includePeerDeps,
|
57 | strictOutOfSync,
|
58 | includeOptionalDeps,
|
59 | pruneWithinTopLevelDeps,
|
60 | }, importer);
|
61 | const project = {
|
62 | packageManager: 'pnpm',
|
63 | targetFile: path.relative(root, pkgJsonFile.fileName),
|
64 | depGraph,
|
65 | plugin: {
|
66 | name: 'snyk-nodejs-lockfile-parser',
|
67 | runtime: process.version,
|
68 | },
|
69 | };
|
70 | scannedProjects.push(project);
|
71 | }
|
72 | catch (e) {
|
73 | debug(`Error process workspace: ${pkgJsonFile.fileName}. ERROR: ${e}`);
|
74 | }
|
75 | }
|
76 | return scannedProjects;
|
77 | };
|
78 | exports.parsePnpmWorkspace = parsePnpmWorkspace;
|
79 |
|
\ | No newline at end of file |