import { logger } from '../../../../../logger' ; import { regEx } from '../../../../../util/regex' ; export function replaceConstraintVersion ( lockFileContent , depName , constraint , newVersion , newConstraint ? ) { if ( lockFileContent . startsWith ( '__metadata:' ) ) { return lockFileContent ; } const depNameConstraint = `${ depName }@${ constraint }` ; const escaped = depNameConstraint . replace ( /(@|\^|\.|\\)/g , '\\$1' ) ; const matchString = `(${ escaped }(("|",|,)[^\n:]*)?:\n)(.*\n)*?(\\s+dependencies|\n[@a-z])` ; const matchResult = regEx ( matchString ) . exec ( lockFileContent ) ; if ( ! matchResult ) { logger . debug ( { depName , constraint , newVersion } , 'Could not find constraint in lock file' ) ; return lockFileContent ; } let constraintLine = matchResult [ 1 ] ; if ( newConstraint ) { const newDepNameConstraint = `${ depName }@${ newConstraint }` ; constraintLine = constraintLine . replace ( depNameConstraint , newDepNameConstraint ) ; } return lockFileContent . replace ( regEx ( matchString ) , `${ constraintLine }  version: "${ newVersion }"\n$5` ) ; }