diff --git a/package-lock.json b/package-lock.json index e6ec1090..7526506c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "opencommit", - "version": "1.1.11", + "version": "1.1.16", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "opencommit", - "version": "1.1.11", + "version": "1.1.16", "license": "ISC", "dependencies": { "@clack/prompts": "^0.6.1", diff --git a/package.json b/package.json index 7c238aca..5904aad0 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "opencommit", - "version": "1.1.11", + "version": "1.1.16", "description": "GPT CLI to auto-generate impressive commits in 1 second. Killing lame commits with AI 🤯🔫", "keywords": [ "git", diff --git a/src/api.ts b/src/api.ts index 72dcece2..0d1b36f3 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 5bc56e59..c285fd55 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 rawArgv = 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 00000000..555fc690 --- /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 + ` + ) + ); + } + } +};