-
Notifications
You must be signed in to change notification settings - Fork 28
/
index.ts
163 lines (145 loc) · 4.02 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
import { PackageLock } from './package-lock-parser';
import { YarnLock } from './yarn-lock-parser';
import { InvalidUserInputError } from '../errors';
import { Yarn2Lock } from './yarn2-lock-parser';
export interface Dep {
name: string;
version: string;
dev?: boolean;
}
interface WorkspacesAlternateConfig {
packages?: string[];
}
type ManifestDependencies = {
[dep: string]: string;
};
export interface ManifestFile {
name: string;
private?: string;
engines?: {
node?: string;
};
workspaces?: string[] | WorkspacesAlternateConfig;
dependencies?: ManifestDependencies;
devDependencies?: ManifestDependencies;
optionalDependencies?: ManifestDependencies;
peerDependencies?: ManifestDependencies;
version?: string;
}
// This is a copy/paste from https://github.com/snyk/dep-graph/blob/master/src/legacy/index.ts
// and should be removed in favour of depgraph library interface
export interface DepTreeDep {
name?: string; // shouldn't, but might happen
version?: string; // shouldn't, but might happen
dependencies?: {
[depName: string]: DepTreeDep;
};
labels?: {
[key: string]: string | undefined;
scope?: 'dev' | 'prod';
pruned?: 'cyclic' | 'true';
missingLockFileEntry?: 'true';
};
}
export interface PkgTree extends DepTreeDep {
type?: string;
packageFormatVersion?: string;
dependencies: {
[depName: string]: DepTreeDep;
};
meta?: {
nodeVersion?: string;
lockfileVersion?: number;
packageManager?: string;
};
hasDevDependencies?: boolean;
cyclic?: boolean;
size?: number;
}
export enum Scope {
prod = 'prod',
dev = 'dev',
}
export enum LockfileType {
npm = 'npm',
npm7 = 'npm7',
yarn = 'yarn',
yarn2 = 'yarn2',
}
export interface LockfileParser {
parseLockFile: (lockFileContents: string) => Lockfile;
getDependencyTree: (
manifestFile: ManifestFile,
lockfile: Lockfile,
includeDev?: boolean,
strict?: boolean,
) => Promise<PkgTree>;
}
export type Lockfile = PackageLock | YarnLock | Yarn2Lock;
export function parseManifestFile(manifestFileContents: string): ManifestFile {
try {
return JSON.parse(manifestFileContents);
} catch (e) {
throw new InvalidUserInputError(
'package.json parsing failed with error ' + e.message,
);
}
}
export function getTopLevelDeps(
targetFile: ManifestFile,
includeDev: boolean,
lockfile: Lockfile,
): Dep[] {
const dependencies: Dep[] = [];
const dependenciesIterator = Object.entries({
...targetFile.dependencies,
...(includeDev ? targetFile.devDependencies : null),
...(targetFile.optionalDependencies || {}),
});
for (const [name, version] of dependenciesIterator) {
dependencies.push({
dev:
includeDev && targetFile.devDependencies
? !!targetFile.devDependencies[name]
: false,
name,
version,
});
}
// Only include peerDependencies if using npm and npm is at least
// version 7 as npm v7 automatically installs peerDependencies
if (lockfile.type === LockfileType.npm7 && targetFile.peerDependencies) {
for (const [name, version] of Object.entries(targetFile.peerDependencies)) {
dependencies.push({
name,
version,
});
}
}
return dependencies;
}
export function createDepTreeDepFromDep(dep: Dep): DepTreeDep {
return {
labels: {
scope: dep.dev ? Scope.dev : Scope.prod,
},
name: dep.name,
version: dep.version,
};
}
export function getYarnWorkspaces(targetFile: string): string[] | false {
try {
const packageJson: ManifestFile = parseManifestFile(targetFile);
if (!!packageJson.workspaces && !!packageJson.private) {
const workspacesPackages = packageJson.workspaces as string[];
const workspacesAlternateConfigPackages = (packageJson.workspaces as WorkspacesAlternateConfig)
.packages;
return [...(workspacesAlternateConfigPackages || workspacesPackages)];
}
return false;
} catch (e) {
throw new InvalidUserInputError(
'package.json parsing failed with ' + `error ${e.message}`,
);
}
}