Skip to content

Commit

Permalink
feat(cli): improve check update with ora
Browse files Browse the repository at this point in the history
  • Loading branch information
ysfscream authored and Red-Asuka committed May 23, 2024
1 parent e8de916 commit 411f597
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions cli/src/utils/checkUpdate.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
import axios from 'axios'
import chalk from 'chalk'
import { compareVersions } from 'compare-versions'

import { version } from '../../package.json'
import ora from 'ora'

const spinner = ora()

const checkUpdate = async () => {
try {
spinner.start('Checking for updates...')
const tagsUrl = 'https://community-sites.emqx.com/api/v1/all_version?product=MQTTX'
const tagsRes = await axios.get(tagsUrl)
if (tagsRes.status === 200) {
const latestVersion = tagsRes.data.data[0].replace('v', '')
if (compareVersions(latestVersion, version) > 0) {
spinner.stop()
console.log(
chalk.yellow(
`A new version of MQTTX CLI is available: ${chalk.cyan(version)} ${chalk.reset('→')} ${chalk.cyan(
Expand All @@ -19,11 +23,14 @@ const checkUpdate = async () => {
),
)
} else {
console.log(chalk.green('There are currently no updates available.'))
spinner.succeed('There are currently no updates available.')
}
}
} catch (error) {
console.error(error)
const err = error as Error
spinner.fail(err.message.toString())
} finally {
spinner.stop()
}
}

Expand Down

0 comments on commit 411f597

Please sign in to comment.