import { regEx } from '../../util/regex' ; import type { PackageDependency } from '../types' ; import { TerragruntDependencyTypes } from './common' ; import type { ExtractionResult } from './types' ; import { keyValueExtractionRegex } from './util' ; export const sourceExtractionRegex = regEx ( /^(?:(?<hostname>(?:[a-zA-Z0-9]+\.+)+[a-zA-Z0-9]+)\/)?(?:(?<namespace>[^/]+)\/)?(?<type>[^/]+)/ ) ; function extractBracesContent ( content ) { const stack = [ ] ; let i = 0 ; for ( i ; i < content . length ; i += 1 ) { if ( content [ i ] === '{' ) { stack . push ( content [ i ] ) ; } else if ( content [ i ] === '}' ) { stack . pop ( ) ; if ( stack . length === 0 ) { break ; } } } return i ; } export function extractTerragruntProvider ( startingLine , lines , moduleName ) { const lineNumber = startingLine ; let line ; const deps = [ ] ; const managerData = { moduleName , terragruntDependencyType : TerragruntDependencyTypes . terragrunt , } ; const dep = { managerData } ; const teraformContent = lines . slice ( lineNumber ) . join ( '\n' ) . substring ( 0 , extractBracesContent ( lines . slice ( lineNumber ) . join ( '\n' ) ) ) . split ( '\n' ) ; for ( let lineNo = 0 ; lineNo < teraformContent . length ; lineNo += 1 ) { line = teraformContent [ lineNo ] ; const kvGroups = keyValueExtractionRegex . exec ( line ) ?. groups ; if ( kvGroups ) { managerData . source = kvGroups . value ; managerData . sourceLine = lineNumber + lineNo ; } } deps . push ( dep ) ; return { lineNumber , dependencies : deps } ; }