import { regEx } from '../../util/regex' ; import { api as npm } from '../npm' ; import type { NewValueConfig , VersioningApi } from '../types' ; export const id = 'hashicorp' ; export const displayName = 'Hashicorp' ; export const urls = [ 'https://www.terraform.io/docs/configuration/terraform.html#specifying-a-required-terraform-version' , ] ; export const supportsRanges = true ; export const supportedRangeStrategies = [ 'bump' , 'extend' , 'pin' , 'replace' ] ; function hashicorp2npm ( input ) { return input . replace ( regEx ( /~>(\s*\d+\.\d+$)/ ) , '^$1' ) . replace ( ',' , '' ) ; } const isLessThanRange = ( version , range ) : boolean => npm . isLessThanRange ( hashicorp2npm ( version ) , hashicorp2npm ( range ) ) ; export const isValid = ( input ) : boolean => ! ! input && npm . isValid ( hashicorp2npm ( input ) ) ; const matches = ( version , range ) : boolean => npm . matches ( hashicorp2npm ( version ) , hashicorp2npm ( range ) ) ; const getSatisfyingVersion = ( versions , range ) : string => npm . getSatisfyingVersion ( versions . map ( hashicorp2npm ) , hashicorp2npm ( range ) ) ; const minSatisfyingVersion = ( versions , range ) : string => npm . minSatisfyingVersion ( versions . map ( hashicorp2npm ) , hashicorp2npm ( range ) ) ; function getNewValue ( { currentValue , rangeStrategy , currentVersion , newVersion , } ) { if ( [ 'replace' , 'update-lockfile' ] . includes ( rangeStrategy ) ) { if ( regEx ( /~>\s*0\.\d+/ ) . test ( currentValue ) && npm . getMajor ( newVersion ) === 0 ) { const testFullVersion = regEx ( /(~>\s*0\.)(\d+)\.\d$/ ) ; let replaceValue = '' ; if ( testFullVersion . test ( currentValue ) ) { replaceValue = `$<prefix>${ npm . getMinor ( newVersion ) }.0` ; } else { replaceValue = `$<prefix>${ npm . getMinor ( newVersion ) }$<suffix>` ; } return currentValue . replace ( regEx ( `(?<prefix>~>\\s*0\\.)\\d+(?<suffix>.*)$` ) , replaceValue ) ; } if ( regEx ( /(~>\s*)\d+\.\d+$/ ) . test ( currentValue ) ) { return currentValue . replace ( regEx ( `(?<prefix>~>\\s*)\\d+\\.\\d+$` ) , `$<prefix>${ npm . getMajor ( newVersion ) }.0` ) ; } } return npm . getNewValue ( { currentValue , rangeStrategy , currentVersion , newVersion , } ) ; } export const api = { ... npm , isLessThanRange , isValid , matches , getSatisfyingVersion , minSatisfyingVersion , getNewValue , } ; export const { isVersion } = api ; export default api ;