import { regEx } from '../../util/regex' ; import type { CustomExtractConfig , PackageDependency } from '../types' ; import { createDependency , mergeExtractionTemplate , mergeGroups , regexMatchAll , } from './utils' ; export function handleAny ( content , packageFile , config ) { return config . matchStrings . map ( ( matchString ) => regEx ( matchString , 'g' ) ) . flatMap ( ( regex ) => regexMatchAll ( regex , content ) ) . map ( ( matchResult ) => createDependency ( { groups : matchResult . groups , replaceString : matchResult [ 0 ] } , config ) ) ; } export function handleCombination ( content , packageFile , config ) { const matches = config . matchStrings . map ( ( matchString ) => regEx ( matchString , 'g' ) ) . flatMap ( ( regex ) => regexMatchAll ( regex , content ) ) ; if ( ! matches . length ) { return [ ] ; } const extraction = matches . map ( ( match ) => ( { groups : match . groups , replaceString : match ?. groups ?. currentValue ? match [ 0 ] : undefined , } ) ) . reduce ( ( base , addition ) => mergeExtractionTemplate ( base , addition ) ) ; return [ createDependency ( extraction , config ) ] ; } export function handleRecursive ( content , packageFile , config , index = 0 , combinedGroups = { } ) { const regexes = config . matchStrings . map ( ( matchString ) => regEx ( matchString , 'g' ) ) ; if ( ! regexes [ index ] ) { return [ ] ; } return regexMatchAll ( regexes [ index ] , content ) . flatMap ( ( match ) => { if ( match ?. groups ?. depName && match ?. groups ?. currentValue ) { return createDependency ( { groups : mergeGroups ( combinedGroups , match . groups ) , replaceString : match [ 0 ] , } , config ) ; } return handleRecursive ( match [ 0 ] , packageFile , config , index + 1 , mergeGroups ( combinedGroups , match . groups || { } ) ) ; } ) ; }