renovate/lib/modules/manager/terragrunt/extract.ts

70 lines
2.4 KiB
TypeScript
Raw Normal View History

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';
import type { ExtractionResult, TerraformManagerData } from './types';
2020-11-12 15:37:15 +00:00
import {
checkFileContainsDependency,
getTerragruntDependencyType,
} from './util';
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 {
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);
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
);
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) => {
// TODO #7154
switch (dep.managerData!.terragruntDependencyType) {
2020-11-12 15:37:15 +00:00
case TerragruntDependencyTypes.terragrunt:
analyseTerragruntModule(dep);
break;
/* istanbul ignore next */
default:
}
2020-11-12 15:37:15 +00:00
delete dep.managerData;
});
return { deps };
}