Skip to content

Commit

Permalink
Use Homebrew to check for latest Flow CLI version (#600)
Browse files Browse the repository at this point in the history
  • Loading branch information
jribbink committed Apr 26, 2024
1 parent 3c329a9 commit 2af432a
Showing 1 changed file with 31 additions and 21 deletions.
52 changes: 31 additions & 21 deletions extension/src/dependency-installer/installers/flow-cli-installer.ts
Expand Up @@ -8,6 +8,13 @@ import fetch from 'node-fetch'
import { HomebrewInstaller } from './homebrew-installer'
import { KNOWN_FLOW_COMMANDS } from '../../flow-cli/cli-versions-provider'

// Relevant subset of Homebrew formulae JSON
interface HomebrewVersionInfo {
versions: {
stable: string
}
}

// Command to check flow-cli
const COMPATIBLE_FLOW_CLI_VERSIONS = '>=1.6.0'

Expand All @@ -21,7 +28,8 @@ const BASH_INSTALL_FLOW_CLI = (githubToken?: string): string =>
`${
githubToken != null ? `GITHUB_TOKEN=${githubToken} ` : ''
}sh -ci "$(curl -fsSL https://raw.githubusercontent.com/onflow/flow-cli/master/install.sh)"`
const VERSION_INFO_URL = 'https://raw.githubusercontent.com/onflow/flow-cli/master/version.txt'
const VERSION_INFO_URL = 'https://formulae.brew.sh/api/formula/flow-cli.json'

export class InstallFlowCLI extends Installer {
#githubToken: string | undefined
#context: InstallerContext
Expand Down Expand Up @@ -78,24 +86,26 @@ export class InstallFlowCLI extends Installer {
await execVscodeTerminal('Install Flow CLI', BASH_INSTALL_FLOW_CLI())
}

async findLatestVersion (currentVersion: semver.SemVer): Promise<void> {
const response = await fetch(VERSION_INFO_URL)
const latestStr = semver.clean(await response.text())
const latest: semver.SemVer | null = semver.parse(latestStr)

// Check if latest version > current version
if (latest != null && latestStr != null && semver.compare(latest, currentVersion) === 1) {
promptUserInfoMessage(
'There is a new Flow CLI version available: ' + latestStr,
[{
label: 'Install latest Flow CLI',
callback: async () => {
await this.runInstall()
await this.#context.refreshDependencies()
}
}]
)
}
async maybeNotifyNewerVersion (currentVersion: semver.SemVer): Promise<void> {
try {
const response = await fetch(VERSION_INFO_URL)
const { versions: { stable: latestStr } }: HomebrewVersionInfo = await response.json()
const latest: semver.SemVer | null = semver.parse(latestStr)

// Check if latest version > current version
if (latest != null && latestStr != null && semver.compare(latest, currentVersion) === 1) {
promptUserInfoMessage(
'There is a new Flow CLI version available: ' + latest.format(),
[{
label: 'Install latest Flow CLI',
callback: async () => {
await this.runInstall()
await this.#context.refreshDependencies()
}
}]
)
}
} catch (e) {}
}

async checkVersion (version: semver.SemVer): Promise<boolean> {
Expand All @@ -119,8 +129,8 @@ export class InstallFlowCLI extends Installer {
return false
}

// Check for newer version
await this.findLatestVersion(version)
// Maybe notify user of newer version, non-blocking
void this.maybeNotifyNewerVersion(version)

return true
}
Expand Down

0 comments on commit 2af432a

Please sign in to comment.