/
index.ts
57 lines (52 loc) · 1.67 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import * as modulesParser from './npm-modules-parser';
import * as lockParser from './npm-lock-parser';
import * as types from '../types';
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';
export async function inspect(
root: string,
targetFile: string,
options: types.Options = {},
): Promise<MultiProjectResult> {
if (!targetFile) {
throw MissingTargetFileError(root);
}
const isLockFileBased =
targetFile.endsWith('package-lock.json') ||
targetFile.endsWith('yarn.lock');
const getLockFileDeps = isLockFileBased && !options.traverseNodeModules;
const depRes: PkgTree | DepGraph = getLockFileDeps
? await lockParser.parse(root, targetFile, options)
: await modulesParser.parse(root, targetFile, options);
let scannedProjects: any[] = [];
if (isResDepGraph(depRes)) {
if (depRes.pkgManager.version) {
analytics.add(
'lockfileVersion',
(depRes as DepGraph)?.pkgManager.version,
);
}
scannedProjects = [{ depGraph: depRes }];
} else {
if (depRes.meta?.lockfileVersion) {
analytics.add(
'lockfileVersion',
(depRes as PkgTree)?.meta?.lockfileVersion,
);
}
scannedProjects = [{ depTree: depRes }];
}
return {
plugin: {
name: 'snyk-nodejs-lockfile-parser',
runtime: process.version,
},
scannedProjects,
};
}
function isResDepGraph(depRes: PkgTree | DepGraph): depRes is DepGraph {
return 'rootPkg' in depRes;
}