renovate/lib/modules/manager/tflint-plugin/extract.ts
Jonathan Ballet 2dd26e1b0a
feat: TFLint plugin manager (#17954)
Co-authored-by: HonkingGoose <34918129+HonkingGoose@users.noreply.github.com>
Co-authored-by: Rhys Arkins <rhys@arkins.net>
Co-authored-by: Michael Kriese <michael.kriese@visualon.de>
2022-10-10 07:04:55 +00:00

54 lines
1.6 KiB
TypeScript

import { logger } from '../../../logger';
import { newlineRegex, regEx } from '../../../util/regex';
import type { ExtractConfig, PackageDependency, PackageFile } from '../types';
import { extractTFLintPlugin } from './plugins';
import type { ExtractionResult } from './types';
import { checkFileContainsPlugins } from './util';
const dependencyBlockExtractionRegex = regEx(
/^\s*plugin\s+"(?<pluginName>[^"]+)"\s+{\s*$/
);
export function extractPackageFile(
content: string,
fileName: string,
config: ExtractConfig
): PackageFile | null {
logger.trace({ content }, 'tflint.extractPackageFile()');
if (!checkFileContainsPlugins(content)) {
logger.trace(
{ fileName },
'preflight content check has not found any relevant content'
);
return null;
}
let deps: PackageDependency[] = [];
try {
const lines = content.split(newlineRegex);
for (let lineNumber = 0; lineNumber < lines.length; lineNumber += 1) {
const line = lines[lineNumber];
const tfLintPlugin = dependencyBlockExtractionRegex.exec(line);
if (tfLintPlugin?.groups) {
logger.trace(`Matched TFLint plugin on line ${lineNumber}`);
let result: ExtractionResult | null = null;
result = extractTFLintPlugin(
lineNumber,
lines,
tfLintPlugin.groups.pluginName
);
if (result) {
lineNumber = result.lineNumber;
deps = deps.concat(result.dependencies);
result = null;
}
}
}
} catch (err) /* istanbul ignore next */ {
logger.warn({ err }, 'Error extracting TFLint plugins');
}
return deps.length ? { deps } : null;
}