Skip to content

Commit

Permalink
refactor(api.ts): reformat imports to be grouped and sorted alphabeti…
Browse files Browse the repository at this point in the history
…cally

refactor(api.ts): replace axios with execa to get the latest version of opencommit from npm registry
  • Loading branch information
di-sukharev committed May 26, 2023
1 parent 1e97408 commit 8979841
Showing 1 changed file with 14 additions and 11 deletions.
25 changes: 14 additions & 11 deletions src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,14 @@ import {
OpenAIApi
} from 'openai';

import {CONFIG_MODES, DEFAULT_MODEL_TOKEN_LIMIT, getConfig} from './commands/config';
import {tokenCount} from './utils/tokenCount';
import {GenerateCommitMessageErrorEnum} from './generateCommitMessageFromGitDiff';
import {
CONFIG_MODES,
DEFAULT_MODEL_TOKEN_LIMIT,
getConfig
} from './commands/config';
import { tokenCount } from './utils/tokenCount';
import { GenerateCommitMessageErrorEnum } from './generateCommitMessageFromGitDiff';
import { execa } from 'execa';

const config = getConfig();

Expand Down Expand Up @@ -58,11 +63,11 @@ class OpenAi {
max_tokens: maxTokens || 500
};
try {
const REQUEST_TOKENS = messages.map(
(msg) => tokenCount(msg.content) + 4
).reduce((a, b) => a + b, 0);
const REQUEST_TOKENS = messages
.map((msg) => tokenCount(msg.content) + 4)
.reduce((a, b) => a + b, 0);

if (REQUEST_TOKENS > (DEFAULT_MODEL_TOKEN_LIMIT - maxTokens)) {
if (REQUEST_TOKENS > DEFAULT_MODEL_TOKEN_LIMIT - maxTokens) {
throw new Error(GenerateCommitMessageErrorEnum.tooMuchTokens);
}

Expand Down Expand Up @@ -98,10 +103,8 @@ export const getOpenCommitLatestVersion = async (): Promise<
string | undefined
> => {
try {
const { data } = await axios.get(
'https://unpkg.com/opencommit/package.json'
);
return data.version;
const { stdout } = await execa('npm', ['view', 'opencommit', 'version']);
return stdout;
} catch (_) {
outro('Error while getting the latest version of opencommit');
return undefined;
Expand Down

0 comments on commit 8979841

Please sign in to comment.