Skip to content

Commit

Permalink
Use JSON flow version output (#567)
Browse files Browse the repository at this point in the history
  • Loading branch information
jribbink committed Mar 26, 2024
1 parent a76c049 commit e0e3a2a
Showing 1 changed file with 31 additions and 1 deletion.
32 changes: 31 additions & 1 deletion extension/src/flow-cli/cli-provider.ts
Expand Up @@ -6,7 +6,9 @@ import * as vscode from 'vscode'
import { Settings } from '../settings/settings'
import { isEqual } from 'lodash'

const CHECK_FLOW_CLI_CMD = (flowCommand: string): string => `${flowCommand} version`
const CHECK_FLOW_CLI_CMD = (flowCommand: string): string => `${flowCommand} version --output=json`
const CHECK_FLOW_CLI_CMD_NO_JSON = (flowCommand: string): string => `${flowCommand} version`

const KNOWN_BINS = ['flow', 'flow-c1']

const CADENCE_V1_CLI_REGEX = /-cadence-v1.0.0/g
Expand All @@ -16,6 +18,10 @@ export interface CliBinary {
version: semver.SemVer
}

interface FlowVersionOutput {
version: string
}

interface AvailableBinariesCache {
[key: string]: StateCache<CliBinary | null>
}
Expand Down Expand Up @@ -85,13 +91,37 @@ export class CliProvider {
})
}

// Fetches the binary information for the given binary
async #fetchBinaryInformation (bin: string): Promise<CliBinary | null> {
try {
// Get user's version informaton
const buffer: string = (await execDefault(CHECK_FLOW_CLI_CMD(
bin
))).stdout

// Format version string from output
const versionInfo: FlowVersionOutput = JSON.parse(buffer)

// Ensure user has a compatible version number installed
const version: semver.SemVer | null = semver.parse(versionInfo.version)
if (version === null) return null

return { name: bin, version }
} catch {
// Fallback to old method if JSON is not supported/fails
return await this.#fetchBinaryInformationOld(bin)
}
}

// Old version of fetchBinaryInformation (before JSON was supported)
// Used as fallback for old CLI versions
async #fetchBinaryInformationOld (bin: string): Promise<CliBinary | null> {
try {
// Get user's version informaton
const buffer: string = (await execDefault(CHECK_FLOW_CLI_CMD_NO_JSON(
bin
))).stdout

// Format version string from output
let versionStr: string | null = parseFlowCliVersion(buffer)

Expand Down

0 comments on commit e0e3a2a

Please sign in to comment.