2022-03-03 09:35:26 +00:00
|
|
|
import { logger } from '../../../logger';
|
|
|
|
import { newlineRegex, regEx } from '../../../util/regex';
|
2021-03-02 20:44:55 +00:00
|
|
|
import type { PackageDependency, PackageFile } from '../types';
|
2021-05-11 17:08:02 +00:00
|
|
|
import { TerragruntDependencyTypes } from './common';
|
2020-11-12 15:37:15 +00:00
|
|
|
import { analyseTerragruntModule, extractTerragruntModule } from './modules';
|
2022-04-20 04:19:59 +00:00
|
|
|
import type { ExtractionResult, TerraformManagerData } from './types';
|
2020-11-12 15:37:15 +00:00
|
|
|
import {
|
|
|
|
checkFileContainsDependency,
|
|
|
|
getTerragruntDependencyType,
|
|
|
|
} from './util';
|
|
|
|
|
2021-10-20 04:38:49 +00:00
|
|
|
const dependencyBlockExtractionRegex = regEx(/^\s*(?<type>[a-z_]+)\s+{\s*$/);
|
2020-11-12 15:37:15 +00:00
|
|
|
const contentCheckList = ['terraform {'];
|
|
|
|
|
|
|
|
export function extractPackageFile(content: string): PackageFile | null {
|
|
|
|
logger.trace({ content }, 'terragrunt.extractPackageFile()');
|
|
|
|
if (!checkFileContainsDependency(content, contentCheckList)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
let deps: PackageDependency<TerraformManagerData>[] = [];
|
|
|
|
try {
|
2022-02-02 07:31:11 +00:00
|
|
|
const lines = content.split(newlineRegex);
|
2020-11-12 15:37:15 +00:00
|
|
|
for (let lineNumber = 0; lineNumber < lines.length; lineNumber += 1) {
|
|
|
|
const line = lines[lineNumber];
|
|
|
|
const terragruntDependency = dependencyBlockExtractionRegex.exec(line);
|
2022-04-20 04:19:59 +00:00
|
|
|
if (terragruntDependency?.groups) {
|
2020-11-12 15:37:15 +00:00
|
|
|
logger.trace(
|
|
|
|
`Matched ${terragruntDependency.groups.type} on line ${lineNumber}`
|
|
|
|
);
|
|
|
|
const tfDepType = getTerragruntDependencyType(
|
|
|
|
terragruntDependency.groups.type
|
|
|
|
);
|
2022-04-20 04:19:59 +00:00
|
|
|
let result: ExtractionResult | null = null;
|
2020-11-12 15:37:15 +00:00
|
|
|
switch (tfDepType) {
|
|
|
|
case TerragruntDependencyTypes.terragrunt: {
|
|
|
|
result = extractTerragruntModule(lineNumber, lines);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* istanbul ignore next */
|
|
|
|
default:
|
|
|
|
logger.trace(
|
|
|
|
`Could not identify TerragruntDependencyType ${terragruntDependency.groups.type} on line ${lineNumber}.`
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (result) {
|
|
|
|
lineNumber = result.lineNumber;
|
|
|
|
deps = deps.concat(result.dependencies);
|
|
|
|
result = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (err) /* istanbul ignore next */ {
|
|
|
|
logger.warn({ err }, 'Error extracting terragrunt plugins');
|
|
|
|
}
|
|
|
|
deps.forEach((dep) => {
|
2022-06-21 11:00:21 +00:00
|
|
|
// TODO #7154
|
2022-04-20 04:19:59 +00:00
|
|
|
switch (dep.managerData!.terragruntDependencyType) {
|
2020-11-12 15:37:15 +00:00
|
|
|
case TerragruntDependencyTypes.terragrunt:
|
|
|
|
analyseTerragruntModule(dep);
|
|
|
|
break;
|
|
|
|
/* istanbul ignore next */
|
|
|
|
default:
|
|
|
|
}
|
2021-11-08 19:20:03 +00:00
|
|
|
|
2020-11-12 15:37:15 +00:00
|
|
|
delete dep.managerData;
|
|
|
|
});
|
|
|
|
return { deps };
|
|
|
|
}
|