From 5352ee01abbf57a6ee7cac5e1ef22a6dc6cafaaf Mon Sep 17 00:00:00 2001 From: Jordan Ribbink Date: Sun, 24 Mar 2024 16:27:05 -0700 Subject: [PATCH 1/2] Use JSON `flow version` output --- extension/src/flow-cli/cli-provider.ts | 33 +++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/extension/src/flow-cli/cli-provider.ts b/extension/src/flow-cli/cli-provider.ts index 3cfe03cc..ab7fb702 100644 --- a/extension/src/flow-cli/cli-provider.ts +++ b/extension/src/flow-cli/cli-provider.ts @@ -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 @@ -16,6 +18,10 @@ export interface CliBinary { version: semver.SemVer } +interface FlowVersionOutput { + version: string +} + interface AvailableBinariesCache { [key: string]: StateCache } @@ -85,6 +91,7 @@ export class CliProvider { }) } + // Fetches the binary information for the given binary async #fetchBinaryInformation (bin: string): Promise { try { // Get user's version informaton @@ -92,6 +99,30 @@ export class CliProvider { 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 { + 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) From 1496957d7b88d613618448ab5ef1b1ca27ef1dcc Mon Sep 17 00:00:00 2001 From: Jordan Ribbink Date: Tue, 26 Mar 2024 12:38:45 -0700 Subject: [PATCH 2/2] format --- extension/src/flow-cli/cli-provider.ts | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/extension/src/flow-cli/cli-provider.ts b/extension/src/flow-cli/cli-provider.ts index ab7fb702..38286657 100644 --- a/extension/src/flow-cli/cli-provider.ts +++ b/extension/src/flow-cli/cli-provider.ts @@ -100,7 +100,7 @@ export class CliProvider { ))).stdout // Format version string from output - let versionInfo: FlowVersionOutput = JSON.parse(buffer) + const versionInfo: FlowVersionOutput = JSON.parse(buffer) // Ensure user has a compatible version number installed const version: semver.SemVer | null = semver.parse(versionInfo.version) @@ -109,8 +109,7 @@ export class CliProvider { 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) + return await this.#fetchBinaryInformationOld(bin) } }