-
Notifications
You must be signed in to change notification settings - Fork 534
/
detect.ts
148 lines (134 loc) · 3.88 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
import * as fs from 'then-fs';
import * as pathLib from 'path';
import * as debugLib from 'debug';
import chalk from 'chalk';
import * as _ from 'lodash';
const debug = debugLib('snyk');
const DETECTABLE_FILES = [
'yarn.lock',
'package-lock.json',
'package.json',
'Gemfile',
'Gemfile.lock',
'pom.xml',
'build.gradle',
'build.gradle.kts',
'build.sbt',
'Pipfile',
'requirements.txt',
'Gopkg.lock',
'vendor/vendor.json',
'obj/project.assets.json',
'project.assets.json',
'packages.config',
'paket.dependencies',
'composer.lock',
];
// when file is specified with --file, we look it up here
const DETECTABLE_PACKAGE_MANAGERS = {
'Gemfile': 'rubygems',
'Gemfile.lock': 'rubygems',
'.gemspec': 'rubygems',
'package-lock.json': 'npm',
'pom.xml': 'maven',
'build.gradle': 'gradle',
'build.gradle.kts': 'gradle',
'build.sbt': 'sbt',
'yarn.lock': 'yarn',
'package.json': 'npm',
'Pipfile': 'pip',
'requirements.txt': 'pip',
'Gopkg.lock': 'golangdep',
'vendor.json': 'govendor',
'project.assets.json': 'nuget',
'packages.config': 'nuget',
'project.json': 'nuget',
'paket.dependencies': 'paket',
'composer.lock': 'composer',
};
export function isPathToPackageFile(path) {
for (const fileName of DETECTABLE_FILES) {
if (_.endsWith(path, fileName)) {
return true;
}
}
return false;
}
export function detectPackageManager(root, 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 {
debug('no file specified. Trying to autodetect in base folder ' + root);
file = detectPackageFile(root);
if (file) {
packageManager = detectPackageManagerFromFile(file);
}
}
} else {
debug('specified paramater is not a folder, trying to lookup as repo');
const registry = options.registry || 'npm';
packageManager = detectPackageManagerFromRegistry(registry);
}
if (!packageManager) {
throw new Error('Could not detect supported target files in ' + root +
'.\nPlease see our documentation for supported languages and ' +
'target files: ' +
chalk.underline(
'https://support.snyk.io/hc/en-us/articles/360000911957-Language-support',
) +
' and make sure you are in the right directory.');
}
return packageManager;
}
// 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);
}
function detectPackageManagerFromFile(file) {
let key = pathLib.basename(file);
if (/\.gemspec$/.test(key)) {
key = '.gemspec';
}
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;
}