import { TerraformProviderDatasource } from '../../datasource/terraform-provider' ; import { regEx } from '../../util/regex' ; import type { PackageDependency } from '../types' ; import { TerraformDependencyTypes } from './common' ; import type { ProviderLock } from './lockfile/types' ; export const keyValueExtractionRegex = regEx ( /^\s*(?<key>[^\s]+)\s+=\s+"(?<value>[^"]+)"\s*$/ ) ; export const resourceTypeExtractionRegex = regEx ( /^\s*resource\s+"(?<type>[^\s]+)"\s+"(?<name>[^"]+)"\s*{/ ) ; export function getTerraformDependencyType ( value ) { switch ( value ) { case 'module' : { return TerraformDependencyTypes . module ; } case 'provider' : { return TerraformDependencyTypes . provider ; } case 'required_providers' : { return TerraformDependencyTypes . required_providers ; } case 'resource' : { return TerraformDependencyTypes . resource ; } case 'terraform' : { return TerraformDependencyTypes . terraform_version ; } default : { return TerraformDependencyTypes . unknown ; } } } export function checkFileContainsDependency ( content , checkList ) { return checkList . some ( ( check ) => content . includes ( check ) ) ; } const pathStringRegex = regEx ( /(.|..)?(\/[^/])+/ ) ; export function checkIfStringIsPath ( path ) { const match = pathStringRegex . exec ( path ) ; return ! ! match ; } export function massageProviderLookupName ( dep ) { if ( ! dep . lookupName ) { dep . lookupName = dep . depName ; } if ( ! dep . lookupName . includes ( '/' ) ) { dep . lookupName = `hashicorp/${ dep . lookupName }` ; } dep . lookupName = dep . lookupName . toLowerCase ( ) ; } export function getLockedVersion ( dep , locks ) { const depRegistryUrl = dep . registryUrls ? dep . registryUrls [ 0 ] : TerraformProviderDatasource . defaultRegistryUrls [ 0 ] ; const foundLock = locks . find ( ( lock ) => lock . lookupName === dep . lookupName && lock . registryUrl === depRegistryUrl ) ; if ( foundLock ) { return foundLock . version ; } return undefined ; }