import { VCSConnector } from '../vcs-connector/connector-models' ; import { Metadata , MetaDataOptions } from '../models' ; import { getAuthorDetails , updateAuthorMetadataString } from './authors' ; import { getFileContributorsMetadata , getFileContributorsString } from './contributors' ; import { isObject } from './utils' ; import { сarriage } from '../utils' ; import { metadataBorder } from '../constants' ; async function getContentWithUpdatedMetadata ( fileContent , options ? , systemVars ? ) { let result ; result = getContentWithUpdatedStaticMetadata ( fileContent , options , systemVars ) ; result = await getContentWithUpdatedDynamicMetadata ( result , options ) ; return result ; } function getContentWithUpdatedStaticMetadata ( fileContent , options ? , systemVars ? ) { if ( ! options || ( ! options ?. addSystemMeta || ! systemVars ) && ! options ?. addSourcePath ) { return fileContent ; } const matches = matchMetadata ( fileContent ) ; const newMetadatas = [ ] ; const { addSystemMeta , addSourcePath , fileData } = options ; if ( addSystemMeta && systemVars && isObject ( systemVars ) ) { newMetadatas . push ( getSystemVarsMetadataString ( systemVars ) ) ; } if ( addSourcePath && fileData . sourcePath ) { const sourcePathMetadataString = `sourcePath: ${ fileData . sourcePath }` ; newMetadatas . push ( sourcePathMetadataString ) ; } if ( matches && matches . length > 0 ) { const [ , fileMetadata , , fileMainContent ] = matches ; return `${ getUpdatedMetadataString ( newMetadatas , fileMetadata ) }${ fileMainContent }` ; } return `${ getUpdatedMetadataString ( newMetadatas ) }${ fileContent }` ; } async function getContentWithUpdatedDynamicMetadata ( fileContent , options ? ) { if ( ! options || ! options ?. isContributorsEnabled ) { return fileContent ; } const matches = matchMetadata ( fileContent ) ; const newMetadatas = [ ] ; const { isContributorsEnabled } = options ; if ( isContributorsEnabled ) { const contributorsMetaData = await getContributorsMetadataString ( options , fileContent ) ; if ( contributorsMetaData ) { newMetadatas . push ( contributorsMetaData ) ; } } if ( matches && matches . length > 0 ) { const [ , fileMetadata , , fileMainContent ] = matches ; let updatedDefaultMetadata = '' ; updatedDefaultMetadata = await updateAuthorMetadataString ( fileMetadata , options . vcsConnector ) ; return `${ getUpdatedMetadataString ( newMetadatas , updatedDefaultMetadata ) }${ fileMainContent }` ; } return `${ getUpdatedMetadataString ( newMetadatas ) }${ fileContent }` ; } function matchMetadata ( fileContent ) { if ( ! fileContent . startsWith ( '---' ) ) { return null ; } const regexpMetadata = '(?<=-{3}\\r?\\n)((.*\\r?\\n)*?)(?=-{3}\\r?\\n)' ; const regexpFileContent = '-{3}((.*[\r?\n]*)*)' ; const regexpParseFileContent = new RegExp ( `${ regexpMetadata }${ regexpFileContent }` , 'gm' ) ; return regexpParseFileContent . exec ( fileContent ) ; } async function getContributorsMetadataString ( options , fileContent ) { const { isContributorsEnabled , vcsConnector , fileData } = options ; if ( isContributorsEnabled && vcsConnector ) { const updatedFileData = { ... fileData , fileContent , } ; return getFileContributorsMetadata ( updatedFileData , vcsConnector ) ; } } function getUpdatedMetadataString ( newMetadatas , defaultMetadata = '' ) { const newMetadata = newMetadatas . join ( сarriage ) + ( newMetadatas . length ? сarriage : '' ) ; const preparedDefaultMetadata = defaultMetadata . trimRight ( ) ; const defaultMetadataСarriage = preparedDefaultMetadata ? сarriage : '' ; const updatedMetadata = `${ preparedDefaultMetadata }${ defaultMetadataСarriage }${ newMetadata }` ; return `${ metadataBorder }${ сarriage }${ updatedMetadata }${ metadataBorder }${ defaultMetadata . length ? '' : сarriage }` ; } async function getUpdatedMetadata ( options , fileContent , meta ? ) { const { vcsConnector } = options ; const newMetadata = { contributors : await getContributorsMetadata ( options , fileContent ) , } ; if ( ! meta ) { return newMetadata ; } const updatedAuthor = await getAuthorMetadata ( meta as Metadata , vcsConnector ) ; return { ... meta , ... newMetadata , author : updatedAuthor , } ; } async function getContributorsMetadata ( options , fileContent ) { const { isContributorsEnabled , vcsConnector , fileData } = options ; if ( isContributorsEnabled && vcsConnector ) { const updatedFileData = { ... fileData , fileContent , } ; return getFileContributorsString ( updatedFileData , vcsConnector ) ; } return JSON . stringify ( [ ] ) ; } async function getAuthorMetadata ( meta , vcsConnector ? ) { if ( meta . author && vcsConnector ) { const updatedAuthor = await getAuthorDetails ( vcsConnector , meta . author ) ; return updatedAuthor ; } return null ; } function getSystemVarsMetadataString ( systemVars ) { return `__system: ${ JSON . stringify ( systemVars ) }` ; } export { getContentWithUpdatedMetadata , getContentWithUpdatedStaticMetadata , getUpdatedMetadata , } ;