forked from snyk/nodejs-lockfile-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
178 lines (164 loc) · 4.7 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import * as fs from 'fs';
import * as path from 'path';
import {
LockfileParser,
Lockfile,
ManifestFile,
PkgTree,
Scope,
parseManifestFile,
LockfileType,
getYarnWorkspaces,
} from './parsers';
import { PackageLockParser } from './parsers/package-lock-parser';
import { YarnLockParser } from './parsers/yarn-lock-parser';
import { Yarn2LockParser } from './parsers/yarn2-lock-parser';
import {
UnsupportedRuntimeError,
InvalidUserInputError,
OutOfSyncError,
} from './errors';
import { buildDepGraphFromCliOutput } from './cli-parsers';
export {
buildDepTree,
buildDepTreeFromFiles,
buildDepGraphFromCliOutput,
getYarnWorkspacesFromFiles,
getYarnWorkspaces,
PkgTree,
Scope,
LockfileType,
UnsupportedRuntimeError,
InvalidUserInputError,
OutOfSyncError,
ManifestFile,
Yarn2LockParser,
YarnLockParser
};
async function buildDepTree(
manifestFileContents: string,
lockFileContents: string,
includeDev = false,
lockfileType?: LockfileType,
strictOutOfSync: boolean = true,
defaultManifestFileName: string = 'package.json',
): Promise<PkgTree> {
if (!lockfileType) {
lockfileType = LockfileType.npm;
} else if (lockfileType === LockfileType.yarn) {
lockfileType = getYarnLockfileType(lockFileContents);
}
let lockfileParser: LockfileParser;
switch (lockfileType) {
case LockfileType.npm:
lockfileParser = new PackageLockParser();
break;
case LockfileType.yarn:
lockfileParser = new YarnLockParser();
break;
case LockfileType.yarn2:
lockfileParser = new Yarn2LockParser();
break;
default:
throw new InvalidUserInputError(
'Unsupported lockfile type ' +
`${lockfileType} provided. Only 'npm' or 'yarn' is currently ` +
'supported.',
);
}
const manifestFile: ManifestFile = parseManifestFile(manifestFileContents);
if (!manifestFile.name) {
manifestFile.name = path.isAbsolute(defaultManifestFileName)
? path.basename(defaultManifestFileName)
: defaultManifestFileName;
}
const lockFile: Lockfile = lockfileParser.parseLockFile(lockFileContents);
return lockfileParser.getDependencyTree(
manifestFile,
lockFile,
includeDev,
strictOutOfSync,
);
}
async function buildDepTreeFromFiles(
root: string,
manifestFilePath: string,
lockFilePath: string,
includeDev = false,
strictOutOfSync = true,
): Promise<PkgTree> {
if (!root || !manifestFilePath || !lockFilePath) {
throw new Error('Missing required parameters for buildDepTreeFromFiles()');
}
const manifestFileFullPath = path.resolve(root, manifestFilePath);
const lockFileFullPath = path.resolve(root, lockFilePath);
if (!fs.existsSync(manifestFileFullPath)) {
throw new InvalidUserInputError(
'Target file package.json not found at ' +
`location: ${manifestFileFullPath}`,
);
}
if (!fs.existsSync(lockFileFullPath)) {
throw new InvalidUserInputError(
'Lockfile not found at location: ' + lockFileFullPath,
);
}
const manifestFileContents = fs.readFileSync(manifestFileFullPath, 'utf-8');
const lockFileContents = fs.readFileSync(lockFileFullPath, 'utf-8');
let lockFileType: LockfileType;
if (lockFilePath.endsWith('package-lock.json')) {
lockFileType = LockfileType.npm;
} else if (lockFilePath.endsWith('yarn.lock')) {
lockFileType = getYarnLockfileType(lockFileContents, root, lockFilePath);
} else {
throw new InvalidUserInputError(
`Unknown lockfile ${lockFilePath}. ` +
'Please provide either package-lock.json or yarn.lock.',
);
}
return await buildDepTree(
manifestFileContents,
lockFileContents,
includeDev,
lockFileType,
strictOutOfSync,
manifestFilePath,
);
}
function getYarnWorkspacesFromFiles(
root,
manifestFilePath: string,
): string[] | false {
if (!root || !manifestFilePath) {
throw new Error(
'Missing required parameters for getYarnWorkspacesFromFiles()',
);
}
const manifestFileFullPath = path.resolve(root, manifestFilePath);
if (!fs.existsSync(manifestFileFullPath)) {
throw new InvalidUserInputError(
'Target file package.json not found at ' +
`location: ${manifestFileFullPath}`,
);
}
const manifestFileContents = fs.readFileSync(manifestFileFullPath, 'utf-8');
return getYarnWorkspaces(manifestFileContents);
}
export function getYarnLockfileType(
lockFileContents: string,
root?: string,
lockFilePath?: string,
): LockfileType {
if (
lockFileContents.includes('__metadata') ||
(root &&
lockFilePath &&
fs.existsSync(
path.resolve(root, lockFilePath.replace('yarn.lock', '.yarnrc.yml')),
))
) {
return LockfileType.yarn2;
} else {
return LockfileType.yarn;
}
}