/
extract.ts
55 lines (48 loc) · 1.54 KB
/
extract.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
import is from '@sindresorhus/is';
import { logger } from '../../../logger';
import type { ExtractConfig, PackageFileContent } from '../types';
import { resourceExtractors } from './extractors';
import * as hcl from './hcl';
import {
checkFileContainsDependency,
extractLocksForPackageFile,
} from './util';
export async function extractPackageFile(
content: string,
fileName: string,
config: ExtractConfig
): Promise<PackageFileContent | null> {
logger.trace({ content }, 'terraform.extractPackageFile()');
const passedExtractors = [];
for (const extractor of resourceExtractors) {
if (checkFileContainsDependency(content, extractor.getCheckList())) {
passedExtractors.push(extractor);
}
}
if (!passedExtractors.length) {
logger.trace(
{ fileName },
'preflight content check has not found any relevant content'
);
return null;
}
logger.trace(
{ fileName },
`preflight content check passed for extractors: [${passedExtractors
.map((value) => value.constructor.name)
.toString()}]`
);
const dependencies = [];
const hclMap = await hcl.parseHCL(content, fileName);
if (is.nullOrUndefined(hclMap)) {
logger.trace({ fileName }, 'failed to parse HCL file');
return null;
}
const locks = await extractLocksForPackageFile(fileName);
for (const extractor of passedExtractors) {
const deps = extractor.extract(hclMap, locks, config);
dependencies.push(...deps);
}
dependencies.forEach((value) => delete value.managerData);
return { deps: dependencies };
}