import { readFileSync } from 'fs' ; import { dirname , join } from 'path' ; import { replaceDoubleToSingleQuotes } from '../utils' ; import { REGEXP_INCLUDE_CONTENTS , REGEXP_INCLUDE_FILE_PATH } from '../constants' ; import { Contributor , Contributors , FileData } from '../models' ; import { FileContributors , VCSConnector } from '../vcs-connector/connector-models' ; async function getFileContributorsMetadata ( fileData , vcsConnector ) { const contributors = await getFileContributorsString ( fileData , vcsConnector ) ; return `contributors: ${ contributors }` ; } async function getFileContributorsString ( fileData , vcsConnector ) { const { tmpInputFilePath , inputFolderPathLength } = fileData ; const relativeFilePath = tmpInputFilePath . substring ( inputFolderPathLength ) ; const fileContributors = await vcsConnector . getContributorsByPath ( relativeFilePath ) ; let nestedContributors = { } ; if ( ! fileContributors . hasIncludes ) { nestedContributors = await getContributorsForNestedFiles ( fileData , vcsConnector ) ; vcsConnector . addNestedContributorsForPath ( relativeFilePath , nestedContributors ) ; } const fileContributorsWithContributorsIncludedFiles = { ... fileContributors . contributors , ... nestedContributors , } ; const contributorsArray = Object . entries ( fileContributorsWithContributorsIncludedFiles ) . map ( ( [ , contributor ] ) => contributor ) ; return replaceDoubleToSingleQuotes ( JSON . stringify ( contributorsArray ) ) ; } async function getContributorsForNestedFiles ( fileData , vcsConnector ) { const { fileContent , inputFolderPathLength } = fileData ; const includeContents = fileContent . match ( REGEXP_INCLUDE_CONTENTS ) ; if ( ! includeContents || includeContents . length === 0 ) { return { } ; } const includesContributors = [ ] ; const relativeIncludeFilePaths = getRelativeIncludeFilePaths ( fileData , includeContents ) ; for ( const relativeIncludeFilePath of relativeIncludeFilePaths . values ( ) ) { const relativeFilePath = relativeIncludeFilePath . substring ( inputFolderPathLength ) ; const includeContributors = await vcsConnector . getContributorsByPath ( relativeFilePath ) ; let nestedContributors = { } ; if ( ! includeContributors . hasIncludes ) { const contentIncludeFile = readFileSync ( relativeIncludeFilePath , 'utf8' ) ; const newFileData = { ... fileData , fileContent : contentIncludeFile , tmpInputFilePath : relativeIncludeFilePath , } ; nestedContributors = await getContributorsForNestedFiles ( newFileData , vcsConnector ) ; vcsConnector . addNestedContributorsForPath ( relativeFilePath , nestedContributors ) ; } includesContributors . push ( includeContributors . contributors ) ; includesContributors . push ( nestedContributors ) ; } return Object . assign ( { } , ... includesContributors ) ; } function getRelativeIncludeFilePaths ( fileData , includeContents ) { const { tmpInputFilePath } = fileData ; const relativeIncludeFilePaths = new Set ( ) ; includeContents . forEach ( ( includeContent ) => { const relativeIncludeFilePath = includeContent . match ( REGEXP_INCLUDE_FILE_PATH ) ; if ( relativeIncludeFilePath && relativeIncludeFilePath . length !== 0 ) { const relativeIncludeFilePathWithoutHash = relativeIncludeFilePath [ 0 ] . split ( '#' ) ; const includeFilePath = join ( dirname ( tmpInputFilePath ) , relativeIncludeFilePathWithoutHash [ 0 ] ) ; relativeIncludeFilePaths . add ( includeFilePath ) ; } } ) ; return relativeIncludeFilePaths ; } export { getFileContributorsMetadata , getFileContributorsString , } ;