import { getRelease , Release } from '@hashicorp/js-releases' ; import * as semver from 'semver' ; import * as vscode from 'vscode' ; import { exec } from '../utils' ; export const DEFAULT_LS_VERSION = 'latest' ; export function isValidVersionString ( value ) { return semver . validRange ( value , { includePrerelease : true , loose : true } ) !== null ; } export async function getLsVersion ( binPath ) { try { const jsonCmd = await exec ( binPath , [ 'version' , '-json' ] ) ; const jsonOutput = JSON . parse ( jsonCmd . stdout ) ; return jsonOutput . version ; } catch ( err ) { return undefined ; } } export async function getRequiredVersionRelease ( versionString , extensionVersion , vscodeVersion ) { const userAgent = `Terraform-VSCode/${ extensionVersion } VSCode/${ vscodeVersion }` ; try { const release = await getRelease ( 'terraform-ls' , versionString , userAgent ) ; console . log ( `Found Terraform language server version ${ release . version } which satisfies range '${ versionString }'` ) ; return release ; } catch ( err ) { if ( versionString === DEFAULT_LS_VERSION ) { throw err ; } console . log ( `Error while finding Terraform language server release which satisfies range '${ versionString }' and will reattempt with '${ DEFAULT_LS_VERSION }': ${ err }` ) ; vscode . window . showWarningMessage ( `No version matching ${ versionString } found, searching for ${ DEFAULT_LS_VERSION } instead` ) ; } const release = await getRelease ( 'terraform-ls' , DEFAULT_LS_VERSION , userAgent ) ; console . log ( `Found Default Terraform language server version ${ release . version } which satisfies range '${ DEFAULT_LS_VERSION }'` ) ; return release ; } export async function pathExists ( filePath ) { try { await vscode . workspace . fs . stat ( vscode . Uri . file ( filePath ) ) ; return true ; } catch ( error ) { return false ; } }