import { regEx } from '../../util/regex' ; import type { PackageDependency } from '../types' ; import { TerraformDependencyTypes } from './common' ; import type { ProviderLock } from './lockfile/types' ; import { analyzeTerraformProvider } from './providers' ; import type { ExtractionResult } from './types' ; import { keyValueExtractionRegex } from './util' ; export const providerBlockExtractionRegex = regEx ( /^\s*(?<key>[^\s]+)\s+=\s+{/ ) ; function extractBlock ( lineNum , lines , dep ) { let lineNumber = lineNum ; let line ; do { lineNumber += 1 ; line = lines [ lineNumber ] ; const kvMatch = keyValueExtractionRegex . exec ( line ) ; if ( kvMatch ) { switch ( kvMatch . groups . key ) { case 'source' : dep . managerData . source = kvMatch . groups . value ; break ; case 'version' : dep . currentValue = kvMatch . groups . value ; break ; default : break ; } } } while ( line . trim ( ) !== '}' ) ; return lineNumber ; } export function extractTerraformRequiredProviders ( startingLine , lines ) { let lineNumber = startingLine ; let line ; const deps = [ ] ; do { const dep = { managerData : { terraformDependencyType : TerraformDependencyTypes . required_providers , } , } ; lineNumber += 1 ; line = lines [ lineNumber ] ; const kvMatch = keyValueExtractionRegex . exec ( line ) ; if ( kvMatch ) { dep . currentValue = kvMatch . groups . value ; dep . managerData . moduleName = kvMatch . groups . key ; deps . push ( dep ) ; } else { const nameMatch = providerBlockExtractionRegex . exec ( line ) ; if ( nameMatch ?. groups ) { dep . managerData . moduleName = nameMatch . groups . key ; lineNumber = extractBlock ( lineNumber , lines , dep ) ; deps . push ( dep ) ; } } } while ( line . trim ( ) !== '}' ) ; return { lineNumber , dependencies : deps } ; } export function analyzeTerraformRequiredProvider ( dep , locks ) { analyzeTerraformProvider ( dep , locks ) ; dep . depType = `required_provider` ; }