/
npm-lock-parser.ts
63 lines (58 loc) · 1.93 KB
/
npm-lock-parser.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
58
59
60
61
62
63
import * as baseDebug from 'debug';
const debug = baseDebug('snyk-test');
import * as path from 'path';
import * as spinner from '../../spinner';
import * as analytics from '../../analytics';
import * as fs from 'fs';
import * as lockFileParser from 'snyk-nodejs-lockfile-parser';
import { PkgTree } from 'snyk-nodejs-lockfile-parser';
import { Options } from '../types';
export async function parse(
root: string,
targetFile: string,
options: Options,
): Promise<PkgTree> {
const lockFileFullPath = path.resolve(root, targetFile);
if (!fs.existsSync(lockFileFullPath)) {
throw new Error(
'Lockfile ' + targetFile + ' not found at location: ' + lockFileFullPath,
);
}
const fullPath = path.parse(lockFileFullPath);
const manifestFileFullPath = path.resolve(fullPath.dir, 'package.json');
const shrinkwrapFullPath = path.resolve(fullPath.dir, 'npm-shrinkwrap.json');
if (!fs.existsSync(manifestFileFullPath)) {
throw new Error(
`Could not find package.json at ${manifestFileFullPath} ` +
`(lockfile found at ${targetFile})`,
);
}
if (fs.existsSync(shrinkwrapFullPath)) {
throw new Error(
'Both `npm-shrinkwrap.json` and `package-lock.json` were found in ' +
fullPath.dir +
'.\n' +
'Please run your command again specifying `--file=package.json` flag.',
);
}
analytics.add('local', true);
analytics.add('generating-node-dependency-tree', {
lockFile: true,
targetFile,
});
const resolveModuleSpinnerLabel = `Analyzing npm dependencies for ${lockFileFullPath}`;
debug(resolveModuleSpinnerLabel);
try {
await spinner(resolveModuleSpinnerLabel);
const strictOutOfSync = options.strictOutOfSync !== false;
return lockFileParser.buildDepTreeFromFiles(
root,
manifestFileFullPath,
lockFileFullPath,
options.dev,
strictOutOfSync,
);
} finally {
await spinner.clear<void>(resolveModuleSpinnerLabel)();
}
}