-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathresourceDiscovery.ts
106 lines (95 loc) · 2.72 KB
/
resourceDiscovery.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
import { IFramework } from './frameworks/iFrameworks.js';
import { cdkFramework } from './frameworks/cdkFramework.js';
import { slsFramework } from './frameworks/slsFramework.js';
import { samFramework } from './frameworks/samFramework.js';
import { terraformFramework } from './frameworks/terraformFramework.js';
import { LldConfig } from './types/lldConfig.js';
import { LambdaResource } from './types/resourcesDiscovery.js';
import { Logger } from './logger.js';
import path from 'node:path';
/**
* List of supported frameworks
*/
const frameworksSupported: IFramework[] = [
cdkFramework,
slsFramework,
samFramework,
terraformFramework,
];
/**
* Get the names of the supported frameworks
*/
function getSupportedFrameworksNames() {
return frameworksSupported.map((f) => f.name);
}
/**
* Get the name of the current framework
*/
async function getCurrentFrameworkName(config: LldConfig) {
const framework = await getCurrentFramework(frameworksSupported, config);
return framework?.name;
}
async function getLambdas(
config: LldConfig,
): Promise<LambdaResource[] | undefined> {
let resources: LambdaResource[] | undefined = undefined;
let frameworks = [...frameworksSupported];
if (config.framework) {
if (config.framework === 'none') {
frameworks = [];
} else {
frameworks = frameworks.filter((f) => f.name === config.framework);
}
}
const framework: IFramework | undefined = await getCurrentFramework(
frameworks,
config,
);
if (framework) {
Logger.verbose(`Getting resources with '${framework.name}' framework`);
resources = await framework.getLambdas(config);
if (config.function && !config.remove) {
// if we are removing the debugger, we don't want to filter by function name
const functionNameFilter = config.function.trim();
resources = resources.filter(
// filter by function name, can use * as wildcard
(l) =>
l.functionName === functionNameFilter ||
new RegExp('^' + functionNameFilter.split('*').join('.*') + '$').test(
l.functionName,
),
);
}
} else {
return undefined;
}
return resources.map((r) => ({
...r,
codePath: path.resolve(r.codePath),
esBuildOptions: {
...r.esBuildOptions,
metafile: true,
},
}));
}
/**
* Get the current framework
*/
async function getCurrentFramework(
frameworks: IFramework[],
config: LldConfig,
): Promise<IFramework | undefined> {
let framework: IFramework | undefined;
for (const f of frameworks) {
if (await f.canHandle(config)) {
framework = f;
break;
}
}
return framework;
}
export const ResourceDiscovery = {
getSupportedFrameworksNames,
getCurrentFrameworkName,
getLambdas,
};