From 8979841010ca3cbf0736901c1056abddc49867f6 Mon Sep 17 00:00:00 2001 From: di-sukharev Date: Fri, 26 May 2023 13:14:33 +0800 Subject: [PATCH] refactor(api.ts): reformat imports to be grouped and sorted alphabetically refactor(api.ts): replace axios with execa to get the latest version of opencommit from npm registry --- src/api.ts | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/src/api.ts b/src/api.ts index 8ca4feb..fcd74df 100644 --- a/src/api.ts +++ b/src/api.ts @@ -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(); @@ -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); } @@ -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;