import { logger } from '../../logger' ; import { regEx } from '../../util/regex' ; import type { PackageDependency , PackageFile } from '../types' ; import { extractCollections } from './collections' ; import { extractCollectionsMetaDataFile } from './collections-metadata' ; import { extractRoles } from './roles' ; export function getSliceEndNumber ( start , numberOfLines , ... blocks ) { if ( start < 0 || start > numberOfLines - 1 ) { return - 1 ; } let nearestEnd = numberOfLines - 1 ; for ( const blocksKey of blocks ) { if ( start < blocksKey && blocksKey < nearestEnd ) { nearestEnd = blocksKey ; } } return nearestEnd ; } export default function extractPackageFile ( content , fileName ) { logger . trace ( 'ansible-galaxy.extractPackageFile()' ) ; const galaxyFileNameRegEx = regEx ( /galaxy\.ya?ml$/ ) ; const deps = [ ] ; const lines = content . split ( '\n' ) ; try { if ( galaxyFileNameRegEx . exec ( fileName ) ) { const galaxyDeps = extractCollectionsMetaDataFile ( lines ) ; deps . push ( ... galaxyDeps ) ; } else { const positions = { collections : - 1 , roles : - 1 , } ; lines . forEach ( ( line , index ) => { if ( regEx ( /^collections:/ ) . exec ( line ) ) { positions . collections = index ; } if ( regEx ( /^roles:/ ) . exec ( line ) ) { positions . roles = index ; } } ) ; if ( positions . collections >= 0 || positions . roles >= 0 ) { const collectionLines = lines . slice ( positions . collections , getSliceEndNumber ( positions . collections , lines . length , positions . roles ) ) ; const collectionDeps = extractCollections ( collectionLines ) ; deps . push ( ... collectionDeps ) ; const roleLines = lines . slice ( positions . roles , getSliceEndNumber ( positions . roles , lines . length , positions . collections ) ) ; const roleDeps = extractRoles ( roleLines ) ; deps . push ( ... roleDeps ) ; } else { const galaxyDeps = extractRoles ( lines ) ; deps . push ( ... galaxyDeps ) ; } } if ( ! deps . length ) { return null ; } return { deps } ; } catch ( err ) { logger . debug ( { err } , 'Error extracting ansible-galaxy deps' ) ; return null ; } }