Skip to content

Commit 39b4925

Browse files
nader-zouaouizenobit
authored and
zenobit
committedApr 12, 2023
feature request: telling user when new version is available (di-sukharev#35)
* ✨ 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.
1 parent d49fd6d commit 39b4925

File tree

3 files changed

+45
-2
lines changed

3 files changed

+45
-2
lines changed
 

‎src/api.ts

+18-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,10 @@ class OpenAi {
5353
} catch (error: unknown) {
5454
outro(`${chalk.red('✖')} ${error}`);
5555

56-
if (axios.isAxiosError<{ error?: { message: string } }>(error) && error.response?.status === 401) {
56+
if (
57+
axios.isAxiosError<{ error?: { message: string } }>(error) &&
58+
error.response?.status === 401
59+
) {
5760
const openAiError = error.response.data.error;
5861

5962
if (openAiError?.message) outro(openAiError.message);
@@ -67,4 +70,18 @@ class OpenAi {
6770
};
6871
}
6972

73+
export const getOpenCommitLatestVersion = async (): Promise<
74+
string | undefined
75+
> => {
76+
try {
77+
const { data } = await axios.get(
78+
'https://unpkg.com/opencommit/package.json'
79+
);
80+
return data.version;
81+
} catch (_) {
82+
outro('Error while getting the latest version of opencommit');
83+
return undefined;
84+
}
85+
};
86+
7087
export const api = new OpenAi();

‎src/cli.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { configCommand } from './commands/config';
77
import { hookCommand, isHookCalled } from './commands/githook.js';
88
import { prepareCommitMessageHook } from './commands/prepare-commit-msg-hook';
99
import { commit } from './commands/commit';
10+
import { checkIsLatestVersion } from './utils/checkIsLatestVersion';
1011

1112
const extraArgs = process.argv.slice(2);
1213

@@ -19,7 +20,8 @@ cli(
1920
ignoreArgv: (type) => type === 'unknown-flag' || type === 'argument',
2021
help: { description: packageJSON.description }
2122
},
22-
() => {
23+
async () => {
24+
await checkIsLatestVersion();
2325
if (isHookCalled) {
2426
prepareCommitMessageHook();
2527
} else {

‎src/utils/checkIsLatestVersion.ts

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { getOpenCommitLatestVersion } from '../api';
2+
import currentPackage from '../../package.json' assert { type: 'json' };
3+
import chalk from 'chalk';
4+
export const checkIsLatestVersion = async () => {
5+
const latestVersion = await getOpenCommitLatestVersion();
6+
7+
if (latestVersion) {
8+
const currentVersion = currentPackage.version;
9+
10+
if (currentVersion !== latestVersion) {
11+
console.warn(
12+
chalk.yellow(
13+
`
14+
You are not using the latest stable version of OpenCommit!
15+
Consider updating to the latest version to get the latest features and bug fixes.
16+
Current version: ${currentVersion}
17+
Latest version: ${latestVersion}
18+
🎉 To update to the latest version, run: npm update opencommit
19+
`
20+
)
21+
);
22+
}
23+
}
24+
};

0 commit comments

Comments
 (0)
Failed to load comments.