Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use JSON flow version output #567

Merged
merged 2 commits into from Mar 26, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
33 changes: 32 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,38 @@ 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
let 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
console.warn(`Failed to fetch binary version information for "${bin}", falling back to old method`)
return 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