Skip to content

Commit

Permalink
Merge pull request #182 from snyk/feat/npm-workspace-support-when-bui…
Browse files Browse the repository at this point in the history
…lding-depgraphs

feat: npm workspaces support when building dep graphs
  • Loading branch information
JamesPatrickGill authored Mar 6, 2023
2 parents 3490b16 + 16aebd1 commit 591bef9
Show file tree
Hide file tree
Showing 18 changed files with 1,798 additions and 3 deletions.
24 changes: 22 additions & 2 deletions lib/dep-graph-builders/npm-lock-v2/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ import {
import { OutOfSyncError } from '../../errors';
import { LockfileType } from '../../parsers';

import * as micromatch from 'micromatch';
import * as pathUtil from 'path';

export { extractPkgsFromNpmLockV2 };

export const parseNpmLockV2Project = (
Expand Down Expand Up @@ -161,7 +164,7 @@ const getChildNode = (
ancestry: { name: string; key: string; inBundle: boolean }[],
pkgKeysByName: Map<string, string[]>,
) => {
const childNodeKey = getChildNodeKey(name, ancestry, pkgs, pkgKeysByName); //
let childNodeKey = getChildNodeKey(name, ancestry, pkgs, pkgKeysByName); //

if (!childNodeKey) {
if (strictOutOfSync) {
Expand All @@ -179,7 +182,24 @@ const getChildNode = (
}
}

const depData = pkgs[childNodeKey];
let depData = pkgs[childNodeKey];

const resolvedToWorkspace = () => {
const workspacesDeclaration = pkgs['']['workspaces'] || [];
const resolvedPath = depData.resolved || '';
const fixedResolvedPath = resolvedPath.replace(/\\/g, '/');
const normalizedWorkspacesDefn = workspacesDeclaration.map((p) => {
return pathUtil.normalize(p).replace(/\\/g, '/');
});
return micromatch.isMatch(fixedResolvedPath, normalizedWorkspacesDefn);
};

// Check for workspaces
if (depData['link'] && resolvedToWorkspace()) {
childNodeKey = depData.resolved as string;
depData = pkgs[depData.resolved as string];
}

const dependencies = getGraphDependencies(
depData.dependencies || {},
depInfo.isDev,
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"lodash.flatmap": "^4.5.0",
"lodash.isempty": "^4.4.0",
"lodash.topairs": "^4.3.0",
"micromatch": "^4.0.5",
"semver": "^7.3.5",
"snyk-config": "^5.0.0",
"tslib": "^1.9.3",
Expand Down
Loading

0 comments on commit 591bef9

Please sign in to comment.