/
detect.ts
223 lines (202 loc) · 5.5 KB
/
detect.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
import * as fs from 'fs';
import * as pathLib from 'path';
import * as debugLib from 'debug';
import * as _ from '@snyk/lodash';
import { NoSupportedManifestsFoundError } from './errors';
import { SupportedPackageManagers } from './package-managers';
import { validateK8sFile } from './iac/iac-parser';
import {
SupportLocalFileOnlyIacError,
UnsupportedOptionFileIacError,
} from './errors/unsupported-options-iac-error';
const debug = debugLib('snyk-detect');
const DETECTABLE_FILES: string[] = [
'yarn.lock',
'package-lock.json',
'package.json',
'Gemfile',
'Gemfile.lock',
'pom.xml',
'build.gradle',
'build.gradle.kts',
'build.sbt',
'Pipfile',
'requirements.txt',
'Gopkg.lock',
'go.mod',
'vendor/vendor.json',
'obj/project.assets.json',
'project.assets.json',
'packages.config',
'paket.dependencies',
'composer.lock',
'Podfile',
'Podfile.lock',
];
export const AUTO_DETECTABLE_FILES: string[] = [
'package-lock.json',
'yarn.lock',
'package.json',
'Gemfile',
'Gemfile.lock',
'pom.xml',
'packages.config',
'paket.dependencies',
'project.json',
'project.assets.json',
'Podfile',
'Podfile.lock',
'composer.lock',
'Gopkg.lock',
'go.mod',
'vendor.json',
'Pipfile',
'requirements.txt',
'build.sbt',
'build.gradle',
'build.gradle.kts',
];
// when file is specified with --file, we look it up here
const DETECTABLE_PACKAGE_MANAGERS: {
[name: string]: SupportedPackageManagers;
} = {
Gemfile: 'rubygems',
'Gemfile.lock': 'rubygems',
'.gemspec': 'rubygems',
'package-lock.json': 'npm',
'pom.xml': 'maven',
'.jar': 'maven',
'.war': 'maven',
'build.gradle': 'gradle',
'build.gradle.kts': 'gradle',
'build.sbt': 'sbt',
'yarn.lock': 'yarn',
'package.json': 'npm',
Pipfile: 'pip',
'setup.py': 'pip',
'requirements.txt': 'pip',
'Gopkg.lock': 'golangdep',
'go.mod': 'gomodules',
'vendor.json': 'govendor',
'project.assets.json': 'nuget',
'packages.config': 'nuget',
'project.json': 'nuget',
'paket.dependencies': 'paket',
'composer.lock': 'composer',
'Podfile.lock': 'cocoapods',
'CocoaPods.podfile.yaml': 'cocoapods',
'CocoaPods.podfile': 'cocoapods',
Podfile: 'cocoapods',
};
export function isPathToPackageFile(path) {
for (const fileName of DETECTABLE_FILES) {
if (_.endsWith(path, fileName)) {
return true;
}
}
return false;
}
export function detectPackageManager(root: string, options) {
// If user specified a package manager let's use it.
if (options.packageManager) {
return options.packageManager;
}
// The package manager used by a docker container is not known ahead of time
if (options.docker) {
return undefined;
}
let packageManager;
let file;
if (isLocalFolder(root)) {
if (options.file) {
if (localFileSuppliedButNotFound(root, options.file)) {
throw new Error(
'Could not find the specified file: ' +
options.file +
'\nPlease check that it exists and try again.',
);
}
file = options.file;
packageManager = detectPackageManagerFromFile(file);
} else if (options.scanAllUnmanaged) {
packageManager = 'maven';
} else {
debug('no file specified. Trying to autodetect in base folder ' + root);
file = detectPackageFile(root);
if (file) {
packageManager = detectPackageManagerFromFile(file);
}
}
} else {
debug('specified parameter is not a folder, trying to lookup as repo');
const registry = options.registry || 'npm';
packageManager = detectPackageManagerFromRegistry(registry);
}
if (!packageManager) {
throw NoSupportedManifestsFoundError([root]);
}
return packageManager;
}
export function isIacProject(root: string, options): string {
if (options.file) {
debug('Iac - --file specified ' + options.file);
throw UnsupportedOptionFileIacError(options.file);
}
if (isLocalFolder(root)) {
debug('Iac - folder case ' + root);
throw SupportLocalFileOnlyIacError();
}
if (localFileSuppliedButNotFound(root, '.') || !fs.existsSync(root)) {
throw SupportLocalFileOnlyIacError();
}
const filePath = pathLib.resolve(root, '.');
const fileContent = fs.readFileSync(filePath, 'utf-8');
validateK8sFile(fileContent, filePath, root);
return 'k8sconfig';
}
// User supplied a "local" file, but that file doesn't exist
function localFileSuppliedButNotFound(root, file) {
return (
file && fs.existsSync(root) && !fs.existsSync(pathLib.resolve(root, file))
);
}
export function isLocalFolder(root) {
try {
return fs.lstatSync(root).isDirectory();
} catch (e) {
return false;
}
}
export function detectPackageFile(root) {
for (const file of DETECTABLE_FILES) {
if (fs.existsSync(pathLib.resolve(root, file))) {
debug('found package file ' + file + ' in ' + root);
return file;
}
}
debug('no package file found in ' + root);
}
export function detectPackageManagerFromFile(
file: string,
): SupportedPackageManagers {
let key = pathLib.basename(file);
// TODO: fix this to use glob matching instead
// like *.gemspec
if (/\.gemspec$/.test(key)) {
key = '.gemspec';
}
if (/\.jar$/.test(key)) {
key = '.jar';
}
if (/\.war$/.test(key)) {
key = '.war';
}
if (!(key in DETECTABLE_PACKAGE_MANAGERS)) {
// we throw and error here because the file was specified by the user
throw new Error('Could not detect package manager for file: ' + file);
}
return DETECTABLE_PACKAGE_MANAGERS[key];
}
function detectPackageManagerFromRegistry(registry) {
return registry;
}