Skip to content

Commit

Permalink
refactor(cli): using built-in fetch instead of axios
Browse files Browse the repository at this point in the history
  • Loading branch information
ysfscream authored and Red-Asuka committed Jul 31, 2024
1 parent 105c209 commit b322af2
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 69 deletions.
1 change: 0 additions & 1 deletion cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
},
"dependencies": {
"@inquirer/prompts": "^5.0.3",
"axios": "^0.27.2",
"cbor": "^9.0.1",
"chalk": "~4.1.2",
"cli-table3": "^0.6.3",
Expand Down
45 changes: 29 additions & 16 deletions cli/src/utils/checkUpdate.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,43 @@
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()

interface TagsResponse {
code: number
data: string[]
success: boolean
}

/**
* Checks for version updates of MQTTX CLI.
* @returns {Promise<void>} A promise that resolves when the update check is complete.
*/
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(
latestVersion,
)}\nhttps://github.com/emqx/MQTTX/releases/tag/v${latestVersion}`,
),
)
} else {
spinner.succeed('There are currently no updates available.')
}
// Using `fetch` here with adding `DOM` lib in `tsconfig.json` is not correct. See: https://github.com/DefinitelyTyped/DefinitelyTyped/issues/60924
// Update to Node.js 20 to resolve fetch global type issue.
const response = await fetch(tagsUrl)
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`)
}
const tagsRes = (await response.json()) as TagsResponse
const latestVersion = tagsRes.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(
latestVersion,
)}\nhttps://github.com/emqx/MQTTX/releases/tag/v${latestVersion}`,
),
)
} else {
spinner.succeed('There are currently no updates available.')
}
} catch (error) {
const err = error as Error
Expand Down
2 changes: 1 addition & 1 deletion cli/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"target": "ES5",
"module": "commonjs",
"outDir": "./dist",
"lib": ["ESNext", "ScriptHost"],
"lib": ["ESNext", "ScriptHost", "DOM"],
"strict": true,
"esModuleInterop": true,
"resolveJsonModule": true,
Expand Down
51 changes: 0 additions & 51 deletions cli/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -317,19 +317,6 @@ argparse@^2.0.1:
resolved "https://registry.npmjs.org/argparse/-/argparse-2.0.1.tgz"
integrity sha512-8+9WqebbFzpX9OR+Wa6O29asIogeRMzcGtAINdpMHHyAg10f05aSFVBbcEqGf/PXw1EjAZ+q2/bEBg3DvurK3Q==

asynckit@^0.4.0:
version "0.4.0"
resolved "https://registry.npmjs.org/asynckit/-/asynckit-0.4.0.tgz"
integrity sha512-Oei9OH4tRh0YqU3GxhX79dM/mwVgvbZJaSNaRk+bshkj0S5cfHcgYakreBjrHwatXKbz+IoIdYLxrKim2MjW0Q==

axios@^0.27.2:
version "0.27.2"
resolved "https://registry.npmjs.org/axios/-/axios-0.27.2.tgz"
integrity sha512-t+yRIyySRTp/wua5xEr+z1q60QmLq8ABsS5O9Me1AsE5dfKqgnCFzwiCZZ/cGNd1lq4/7akDWMxdhVlucjmnOQ==
dependencies:
follow-redirects "^1.14.9"
form-data "^4.0.0"

balanced-match@^1.0.0:
version "1.0.2"
resolved "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.2.tgz"
Expand Down Expand Up @@ -459,13 +446,6 @@ color-name@~1.1.4:
resolved "https://registry.npmjs.org/color-name/-/color-name-1.1.4.tgz"
integrity sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA==

combined-stream@^1.0.8:
version "1.0.8"
resolved "https://registry.npmjs.org/combined-stream/-/combined-stream-1.0.8.tgz"
integrity sha512-FQN4MRfuJeHf7cBbBMJFXhKSDq+2kAArBlmRBvcvFE5BB1HZKXtSFASDhdlz9zOYwxh8lDdnvmMOe/+5cdoEdg==
dependencies:
delayed-stream "~1.0.0"

commander@^9.3.0:
version "9.3.0"
resolved "https://registry.npmjs.org/commander/-/commander-9.3.0.tgz"
Expand Down Expand Up @@ -518,11 +498,6 @@ defaults@^1.0.3:
dependencies:
clone "^1.0.2"

delayed-stream@~1.0.0:
version "1.0.0"
resolved "https://registry.npmjs.org/delayed-stream/-/delayed-stream-1.0.0.tgz"
integrity sha512-ZySD7Nf91aLB0RxL4KGrKHBXl7Eds1DAmEdcoVawXnLD7SDhpNgtuII2aAkg7a7QS41jxPSZ17p4VdGnMHk3MQ==

duplexify@^4.1.1:
version "4.1.2"
resolved "https://registry.npmjs.org/duplexify/-/duplexify-4.1.2.tgz"
Expand Down Expand Up @@ -580,20 +555,6 @@ find-up@^2.0.0:
dependencies:
locate-path "^2.0.0"

follow-redirects@^1.14.9:
version "1.15.6"
resolved "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.15.6.tgz"
integrity sha512-wWN62YITEaOpSK584EZXJafH1AGpO8RVgElfkuXbTOrPX4fIfOyEpW/CsiNd8JdYrAoOvafRTOEnvsO++qCqFA==

form-data@^4.0.0:
version "4.0.0"
resolved "https://registry.npmjs.org/form-data/-/form-data-4.0.0.tgz"
integrity sha512-ETEklSGi5t0QMZuiXoA/Q6vcnxcLQP5vdugSpuAyi6SVGi2clPPp+xgEhuMaHC+zGgn31Kd235W35f7Hykkaww==
dependencies:
asynckit "^0.4.0"
combined-stream "^1.0.8"
mime-types "^2.1.12"

fs.realpath@^1.0.0:
version "1.0.0"
resolved "https://registry.npmjs.org/fs.realpath/-/fs.realpath-1.0.0.tgz"
Expand Down Expand Up @@ -756,18 +717,6 @@ lru-cache@^6.0.0:
dependencies:
yallist "^4.0.0"

[email protected]:
version "1.52.0"
resolved "https://registry.npmjs.org/mime-db/-/mime-db-1.52.0.tgz"
integrity sha512-sPU4uV7dYlvtWJxwwxHD0PuihVNiE7TyAbQ5SWxDCB9mUYvOgroQOwYQQOKPJ8CIbE+1ETVlOoK1UC2nU3gYvg==

mime-types@^2.1.12:
version "2.1.35"
resolved "https://registry.npmjs.org/mime-types/-/mime-types-2.1.35.tgz"
integrity sha512-ZDY+bPm5zTTF+YpCrAU9nK0UgICYPT0QtT1NZWFv4s++TNkcgVaT0g6+4R2uI4MjQjzysHB1zxuWL50hzaeXiw==
dependencies:
mime-db "1.52.0"

mimic-fn@^2.1.0:
version "2.1.0"
resolved "https://registry.yarnpkg.com/mimic-fn/-/mimic-fn-2.1.0.tgz#7ed2c2ccccaf84d3ffcb7a69b57711fc2083401b"
Expand Down

0 comments on commit b322af2

Please sign in to comment.