/
index.js
80 lines (75 loc) · 2.09 KB
/
index.js
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
module.exports = test;
const detect = require('../detect');
const { runTest } = require('./run-test');
const chalk = require('chalk');
const pm = require('../package-managers');
const iacProjects = require('../iac/iac-projects');
const {
UnsupportedPackageManagerError,
NotSupportedIacFileError,
} = require('../errors');
const { isMultiProjectScan } = require('../is-multi-project-scan');
async function test(root, options, callback) {
if (typeof options === 'function') {
callback = options;
options = {};
}
if (!options) {
options = {};
}
const promise = executeTest(root, options);
if (callback) {
promise
.then((res) => {
callback(null, res);
})
.catch(callback);
}
return promise;
}
function executeTest(root, options) {
try {
if (!options.allProjects) {
options.packageManager = options.iac
? detect.isIacProject(root, options)
: detect.detectPackageManager(root, options);
}
return run(root, options).then((results) => {
for (const res of results) {
if (!res.packageManager) {
res.packageManager = options.packageManager;
}
}
if (results.length === 1) {
// Return only one result if only one found as this is the default usecase
return results[0];
}
// For gradle, yarnWorkspaces, allProjects we may be returning more than one result
return results;
});
} catch (error) {
return Promise.reject(chalk.red.bold(error));
}
}
function run(root, options) {
const projectType = options.packageManager;
validateProjectType(options, projectType);
return runTest(projectType, root, options);
}
function validateProjectType(options, projectType) {
if (options.iac) {
if (!iacProjects.TEST_SUPPORTED_IAC_PROJECTS.includes(projectType)) {
throw new NotSupportedIacFileError(projectType);
}
} else {
if (
!(
options.docker ||
isMultiProjectScan(options) ||
pm.SUPPORTED_PACKAGE_MANAGER_NAME[projectType]
)
) {
throw new UnsupportedPackageManagerError(projectType);
}
}
}