Skip to content

Commit

Permalink
feat: npm lock v2 and v3 support
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesPatrickGill committed Mar 22, 2023
1 parent 54c95fb commit 3c48d2e
Show file tree
Hide file tree
Showing 9 changed files with 230 additions and 18 deletions.
40 changes: 31 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@
"snyk-gradle-plugin": "3.26.0",
"snyk-module": "3.1.0",
"snyk-mvn-plugin": "2.32.2",
"snyk-nodejs-lockfile-parser": "1.45.1",
"snyk-nodejs-lockfile-parser": "1.47.5",
"snyk-nuget-plugin": "1.23.5",
"snyk-php-plugin": "1.9.2",
"snyk-policy": "^1.25.0",
Expand Down
34 changes: 27 additions & 7 deletions src/lib/plugins/nodejs-plugin/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,13 @@ import * as analytics from '../../analytics';
import { MissingTargetFileError } from '../../errors/missing-targetfile-error';
import { MultiProjectResult } from '@snyk/cli-interface/legacy/plugin';
import { DepGraph } from '@snyk/dep-graph';
import { PkgTree } from 'snyk-nodejs-lockfile-parser';
import {
PkgTree,
getLockfileVersionFromFile,
NodeLockfileVersion,
} from 'snyk-nodejs-lockfile-parser';

import * as path from 'path';

export async function inspect(
root: string,
Expand All @@ -26,17 +32,31 @@ export async function inspect(

let scannedProjects: any[] = [];
if (isResDepGraph(depRes)) {
if (depRes.pkgManager.version) {
analytics.add('lockfileVersion', depRes.pkgManager.version);
}
scannedProjects = [{ depGraph: depRes }];
} else {
if (depRes.meta?.lockfileVersion) {
analytics.add('lockfileVersion', depRes.meta.lockfileVersion);
}
scannedProjects = [{ depTree: depRes }];
}

if (isLockFileBased) {
const lockFileFullPath = path.resolve(root, targetFile);
const lockfileVersion = getLockfileVersionFromFile(lockFileFullPath);
switch (lockfileVersion) {
case NodeLockfileVersion.NpmLockV1:
case NodeLockfileVersion.YarnLockV1:
analytics.add('lockfileVersion', 1);
break;
case NodeLockfileVersion.NpmLockV2:
case NodeLockfileVersion.YarnLockV2:
analytics.add('lockfileVersion', 2);
break;
case NodeLockfileVersion.NpmLockV3:
analytics.add('lockfileVersion', 3);
break;
default:
break;
}
}

return {
plugin: {
name: 'snyk-nodejs-lockfile-parser',
Expand Down
11 changes: 10 additions & 1 deletion src/lib/plugins/nodejs-plugin/npm-lock-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@ export async function parse(
);
if (
lockfileVersion === NodeLockfileVersion.YarnLockV1 ||
lockfileVersion === NodeLockfileVersion.YarnLockV2
lockfileVersion === NodeLockfileVersion.YarnLockV2 ||
lockfileVersion === NodeLockfileVersion.NpmLockV2 ||
lockfileVersion === NodeLockfileVersion.NpmLockV3
) {
return await buildDepGraph(
root,
Expand Down Expand Up @@ -128,6 +130,13 @@ async function buildDepGraph(
lockFileContents,
options,
);
case NodeLockfileVersion.NpmLockV2:
case NodeLockfileVersion.NpmLockV3:
return lockFileParser.parseNpmLockV2Project(
manifestFileContents,
lockFileContents,
options,
);
}
throw new Error('Failed to build dep graph from current project');
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions test/acceptance/workspaces/npm-package-lockfile-v2/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "npm-package-lockfile-v2",
"version": "1.0.0",
"description": "Simple NPM package",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "snyk",
"license": "ISC",
"dependencies": {
"debug": "2.2.0"
},
"devDependencies": {
"object-assign": "4.1.1"
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions test/acceptance/workspaces/npm-package-lockfile-v3/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "npm-package-lockfile-v3",
"version": "1.0.0",
"description": "Simple NPM package",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "snyk",
"license": "ISC",
"dependencies": {
"debug": "2.2.0"
},
"devDependencies": {
"object-assign": "4.1.1"
}
}
24 changes: 24 additions & 0 deletions test/tap/cli-test/cli-test.npm.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,30 @@ export const NpmTests: AcceptanceTests = {
t.match(req.body.targetFile, undefined, 'target is undefined');
},

'`test npm-package with lockfile v2`': (params, utils) => async (t) => {
utils.chdirWorkspaces();
await params.cli.test('npm-package-lockfile-v2');
const req = params.server.popRequest();
const depGraph = req.body.depGraph;
t.same(
depGraph.pkgs.map((p) => p.id).sort(),
['npm-package-lockfile-v2@1.0.0', 'ms@0.7.1', 'debug@2.2.0'].sort(),
'depGraph looks fine',
);
},

'`test npm-package with lockfile v3`': (params, utils) => async (t) => {
utils.chdirWorkspaces();
await params.cli.test('npm-package-lockfile-v3');
const req = params.server.popRequest();
const depGraph = req.body.depGraph;
t.same(
depGraph.pkgs.map((p) => p.id).sort(),
['npm-package-lockfile-v3@1.0.0', 'ms@0.7.1', 'debug@2.2.0'].sort(),
'depGraph looks fine',
);
},

'test npm-package remoteUrl': (params, utils) => async (t) => {
utils.chdirWorkspaces();
process.env.GIT_DIR = 'npm-package/gitdir';
Expand Down

0 comments on commit 3c48d2e

Please sign in to comment.