/
index.js
83 lines (78 loc) · 2.13 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
81
82
83
module.exports = test;
const detect = require('../detect');
const runTest = require('./run-test');
const chalk = require('chalk');
const pm = require('../package-managers');
const cloudConfigProjects = require('../cloud-config/cloud-config-projects');
const {
UnsupportedPackageManagerError,
NoSupportedCloudConfigFileError,
} = require('../errors');
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) {
if (options.iac) {
options.packageManager = detect.isCloudConfigProject(root, options);
} else {
options.packageManager = 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) {
if (options.iac) {
const projectType = options.packageManager;
if (
!cloudConfigProjects.TEST_SUPPORTED_CLOUD_CONFIG_PROJECTS.includes(
projectType,
)
) {
throw new NoSupportedCloudConfigFileError(projectType);
}
return runTest(projectType, root, options);
}
const packageManager = options.packageManager;
if (
!(
options.docker ||
options.allProjects ||
pm.SUPPORTED_PACKAGE_MANAGER_NAME[packageManager]
)
) {
throw new UnsupportedPackageManagerError(packageManager);
}
return runTest(packageManager, root, options);
}