import { Release } from '@hashicorp/js-releases' ; import * as semver from 'semver' ; import * as vscode from 'vscode' ; import TelemetryReporter from 'vscode-extension-telemetry' ; import { ServerPath } from '../serverPath' ; import { config } from '../vscodeUtils' ; import { DEFAULT_LS_VERSION , getLsVersion , getRequiredVersionRelease , isValidVersionString , pathExists , } from './detector' ; import { installTerraformLS } from './installer' ; export async function updateOrInstall ( lsVersion , extensionVersion , vscodeVersion , lsPath , reporter ) { const stgingExists = await pathExists ( lsPath . stgBinPath ( ) ) ; if ( stgingExists ) { await vscode . workspace . fs . rename ( vscode . Uri . file ( lsPath . stgBinPath ( ) ) , vscode . Uri . file ( lsPath . binPath ( ) ) , { overwrite : true , } ) ; return ; } const versionString = isValidVersionString ( lsVersion ) ? lsVersion : DEFAULT_LS_VERSION ; const lsPresent = await pathExists ( lsPath . binPath ( ) ) ; const autoUpdate = config ( 'extensions' ) . get < boolean > ( 'autoUpdate' , true ) ; if ( lsPresent === true && autoUpdate === false ) { return ; } let release ; try { release = await getRequiredVersionRelease ( versionString , extensionVersion , vscodeVersion ) ; } catch ( err ) { console . log ( `Error while finding Terraform language server release which satisfies range '${ versionString }': ${ err }` ) ; if ( err instanceof Error ) { reporter . sendTelemetryException ( err ) ; } return ; } if ( lsPresent === false ) { return installTerraformLS ( lsPath . installPath ( ) , release , extensionVersion , vscodeVersion , reporter ) ; } const installedVersion = await getLsVersion ( lsPath . binPath ( ) ) ; if ( installedVersion === undefined ) { console . log ( `Currently installed Terraform language server is version '${ installedVersion }` ) ; return installTerraformLS ( lsPath . installPath ( ) , release , extensionVersion , vscodeVersion , reporter ) ; } console . log ( `Currently installed Terraform language server is version '${ installedVersion }` ) ; reporter . sendTelemetryEvent ( 'foundLsInstalled' , { terraformLsVersion : installedVersion } ) ; if ( semver . eq ( release . version , installedVersion , { includePrerelease : true } ) ) { console . log ( `Language server release is current: ${ release . version }` ) ; return ; } if ( semver . gt ( release . version , installedVersion , { includePrerelease : true } ) ) { console . log ( `A newer language server release is available: ${ release . version }` ) ; } else if ( semver . lt ( release . version , installedVersion , { includePrerelease : true } ) ) { console . log ( `An older language server release is available: ${ release . version }` ) ; } return installTerraformLS ( lsPath . stgInstallPath ( ) , release , extensionVersion , vscodeVersion , reporter ) ; }