Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature request: telling user when new version is available #35

Merged
merged 7 commits into from
Mar 21, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
✨ 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.
  • Loading branch information
nader-zouaoui committed Mar 17, 2023
commit 83c77eb5009389b4e4c9ed1e8c75effa0e3b2307
19 changes: 18 additions & 1 deletion src/api.ts
Original file line number Diff line number Diff line change
@@ -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();
4 changes: 3 additions & 1 deletion src/cli.ts
Original file line number Diff line number Diff line change
@@ -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 {
23 changes: 23 additions & 0 deletions src/utils/checkIsLatestVersion.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
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(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nader-zouaoui What do you think about already suggesting the command to perform the update? ex:

🎉 Update to latest version by running: npm update opencommit

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense, will do it

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