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 + ` + ) + ); + } + } +};