From a6ccdb5f77b1574cfa10ade643c6eb5254cc9155 Mon Sep 17 00:00:00 2001 From: Nader Zouaoui Date: Tue, 21 Mar 2023 08:11:48 +0100 Subject: [PATCH] feature request: telling user when new version is available (#35) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * ✨ feat(cli.ts): add checkIsLatestVersion function This commit adds a new function that checks if the current version of OpenCommit is the latest version. The function uses the getOpenCommitLatestVersion function from the api module to get the latest version of OpenCommit. If the current version is not the latest version, a warning message is printed to the console, informing the user to update to the latest version to get the latest features and bug fixes. --- src/api.ts | 19 ++++++++++++++++++- src/cli.ts | 4 +++- src/utils/checkIsLatestVersion.ts | 24 ++++++++++++++++++++++++ 3 files changed, 45 insertions(+), 2 deletions(-) create mode 100644 src/utils/checkIsLatestVersion.ts diff --git a/src/api.ts b/src/api.ts index 72dcece..0d1b36f 100644 --- a/src/api.ts +++ b/src/api.ts @@ -53,7 +53,10 @@ class OpenAi { } catch (error: unknown) { outro(`${chalk.red('✖')} ${error}`); - if (axios.isAxiosError<{ error?: { message: string } }>(error) && error.response?.status === 401) { + if ( + axios.isAxiosError<{ error?: { message: string } }>(error) && + error.response?.status === 401 + ) { const openAiError = error.response.data.error; if (openAiError?.message) outro(openAiError.message); @@ -67,4 +70,18 @@ class OpenAi { }; } +export const getOpenCommitLatestVersion = async (): Promise< + string | undefined +> => { + try { + const { data } = await axios.get( + 'https://unpkg.com/opencommit/package.json' + ); + return data.version; + } catch (_) { + outro('Error while getting the latest version of opencommit'); + return undefined; + } +}; + export const api = new OpenAi(); diff --git a/src/cli.ts b/src/cli.ts index 74c9800..3740998 100755 --- a/src/cli.ts +++ b/src/cli.ts @@ -7,6 +7,7 @@ import { configCommand } from './commands/config'; import { hookCommand, isHookCalled } from './commands/githook.js'; import { prepareCommitMessageHook } from './commands/prepare-commit-msg-hook'; import { commit } from './commands/commit'; +import { checkIsLatestVersion } from './utils/checkIsLatestVersion'; const extraArgs = process.argv.slice(2); @@ -19,7 +20,8 @@ cli( ignoreArgv: (type) => type === 'unknown-flag' || type === 'argument', help: { description: packageJSON.description } }, - () => { + async () => { + await checkIsLatestVersion(); if (isHookCalled) { prepareCommitMessageHook(); } else { diff --git a/src/utils/checkIsLatestVersion.ts b/src/utils/checkIsLatestVersion.ts new file mode 100644 index 0000000..555fc69 --- /dev/null +++ b/src/utils/checkIsLatestVersion.ts @@ -0,0 +1,24 @@ +import { getOpenCommitLatestVersion } from '../api'; +import currentPackage from '../../package.json' assert { type: 'json' }; +import chalk from 'chalk'; +export const checkIsLatestVersion = async () => { + const latestVersion = await getOpenCommitLatestVersion(); + + if (latestVersion) { + const currentVersion = currentPackage.version; + + if (currentVersion !== latestVersion) { + console.warn( + chalk.yellow( + ` +You are not using the latest stable version of OpenCommit! +Consider updating to the latest version to get the latest features and bug fixes. +Current version: ${currentVersion} +Latest version: ${latestVersion} +🎉 To update to the latest version, run: npm update opencommit + ` + ) + ); + } + } +};