param src import crypto from 'crypto' ; function getGeneratedSectionIndexes ( src , tag ) { const contents = src . split ( '\n' ) ; const start = contents . findIndex ( line => line . includes ( `@generated begin ${ tag }` ) ) ; const end = contents . findIndex ( line => line . includes ( `@generated end ${ tag }` ) ) ; return { contents , start , end } ; } export type MergeResults = { contents : string ; didClear : boolean ; didMerge : boolean ; } ; param src param newSrc param identifier param anchor param offset param comment export function mergeContents ( { src , newSrc , tag , anchor , offset , comment , } ) { const header = createGeneratedHeaderComment ( newSrc , tag , comment ) ; if ( ! src . includes ( header ) ) { const sanitizedTarget = removeGeneratedContents ( src , tag ) ; return { contents : addLines ( sanitizedTarget ?? src , anchor , offset , [ header , ... newSrc . split ( '\n' ) , `${ comment } @generated end ${ tag }` , ] ) , didMerge : true , didClear : ! ! sanitizedTarget } ; } return { contents : src , didClear : false , didMerge : false } ; } export function removeContents ( { src , tag } ) { const sanitizedTarget = removeGeneratedContents ( src , tag ) ; return { contents : sanitizedTarget ?? src , didMerge : false , didClear : ! ! sanitizedTarget } ; } function addLines ( content , find , offset , toAdd ) { const lines = content . split ( '\n' ) ; let lineIndex = lines . findIndex ( line => line . match ( find ) ) ; if ( lineIndex < 0 ) { const error = new Error ( `Failed to match "${ find }" in contents:\n${ content }` ) ; error . code = 'ERR_NO_MATCH' ; throw error ; } for ( const newLine of toAdd ) { lines . splice ( lineIndex + offset , 0 , newLine ) ; lineIndex ++ ; } return lines . join ( '\n' ) ; } param src export function removeGeneratedContents ( src , tag ) { const { contents , start , end } = getGeneratedSectionIndexes ( src , tag ) ; if ( start > - 1 && end > - 1 && start < end ) { contents . splice ( start , end - start + 1 ) ; return contents . join ( '\n' ) ; } return null ; } export function createGeneratedHeaderComment ( contents , tag , comment ) { const hashKey = createHash ( contents ) ; return `${ comment } @generated begin ${ tag } - expo prebuild (DO NOT MODIFY) ${ hashKey }` ; } export function createHash ( src ) { const hash = crypto . createHash ( 'sha1' ) . update ( src ) . digest ( 'hex' ) ; return `sync-${ hash }` ; }