From 2f5daf9e707e5290cc88aa28a491b614259ec97e Mon Sep 17 00:00:00 2001 From: Moment <73689580+xun082@users.noreply.github.com> Date: Mon, 24 Jun 2024 16:00:13 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=F0=9F=A7=A9=20fixed=20the=20error=20cau?= =?UTF-8?q?sed=20by=20attempting=20to=20read=20a=20non-existent=20file.=20?= =?UTF-8?q?(#10)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/core/code-review/help.ts | 23 ----------------------- src/core/code-review/index.ts | 3 +-- src/core/commit/help.ts | 23 ----------------------- src/core/commit/index.ts | 3 ++- src/core/commit/prompt.ts | 1 - src/index.ts | 2 +- src/types/index.ts | 5 +++++ src/utils/index.ts | 26 ++++++++++++++++++++++++++ 8 files changed, 35 insertions(+), 51 deletions(-) delete mode 100644 src/core/code-review/help.ts delete mode 100644 src/core/commit/help.ts diff --git a/src/core/code-review/help.ts b/src/core/code-review/help.ts deleted file mode 100644 index 012cb81..0000000 --- a/src/core/code-review/help.ts +++ /dev/null @@ -1,23 +0,0 @@ -import { execSync } from 'node:child_process'; - -export function getFilesChangedInGitAdd() { - const gitDiff = execSync('git diff --cached --name-only', { encoding: 'utf-8' }); - const files = gitDiff.split('\n'); - - // 过滤掉 lock 文件 - const ignoredPatterns = [/package-lock\.json$/, /yarn\.lock$/, /pnpm-lock\.yaml$/]; - const filteredFiles = files.filter( - (file) => file && !ignoredPatterns.some((pattern) => pattern.test(file)), - ); - - return filteredFiles; -} - -interface Staged { - filename: string; - content: string; -} - -export function allStagedFiles2Message(staged: Staged[]) { - return staged.map((item) => item.content).join('\n'); -} diff --git a/src/core/code-review/index.ts b/src/core/code-review/index.ts index 2d50ff5..9d55121 100644 --- a/src/core/code-review/index.ts +++ b/src/core/code-review/index.ts @@ -2,10 +2,9 @@ import fs from 'node:fs'; import { intro, outro, spinner } from '@clack/prompts'; import path from 'node:path'; -import { allStagedFiles2Message, getFilesChangedInGitAdd } from './help'; import { codeReviewPrompt } from './prompt'; -import { getOpenAiClient } from '@/utils'; +import { getOpenAiClient, getFilesChangedInGitAdd, allStagedFiles2Message } from '@/utils'; import { OPENAI_CHAT_COMPLETIONS_ENDPOINT } from '@/utils/constants'; export default async function commitMessage() { diff --git a/src/core/commit/help.ts b/src/core/commit/help.ts deleted file mode 100644 index 012cb81..0000000 --- a/src/core/commit/help.ts +++ /dev/null @@ -1,23 +0,0 @@ -import { execSync } from 'node:child_process'; - -export function getFilesChangedInGitAdd() { - const gitDiff = execSync('git diff --cached --name-only', { encoding: 'utf-8' }); - const files = gitDiff.split('\n'); - - // 过滤掉 lock 文件 - const ignoredPatterns = [/package-lock\.json$/, /yarn\.lock$/, /pnpm-lock\.yaml$/]; - const filteredFiles = files.filter( - (file) => file && !ignoredPatterns.some((pattern) => pattern.test(file)), - ); - - return filteredFiles; -} - -interface Staged { - filename: string; - content: string; -} - -export function allStagedFiles2Message(staged: Staged[]) { - return staged.map((item) => item.content).join('\n'); -} diff --git a/src/core/commit/index.ts b/src/core/commit/index.ts index 47bbf4c..0a312b3 100644 --- a/src/core/commit/index.ts +++ b/src/core/commit/index.ts @@ -1,9 +1,10 @@ import fs from 'node:fs'; import { intro, outro, spinner } from '@clack/prompts'; -import { allStagedFiles2Message, getFilesChangedInGitAdd } from './help'; import { createChatCompletion } from './openai'; +import { getFilesChangedInGitAdd, allStagedFiles2Message } from '@/utils'; + export default async function commitMessage() { intro('-- 开始读取缓存区文件更改'); diff --git a/src/core/commit/prompt.ts b/src/core/commit/prompt.ts index 18f4c2b..024a3ae 100644 --- a/src/core/commit/prompt.ts +++ b/src/core/commit/prompt.ts @@ -60,7 +60,6 @@ export const createChatRequest = (diff: string, options: { locale: string; maxLe const { locale, maxLength } = options; return { model: 'gpt-4o', - // model: 'gpt-3.5-turbo', messages: [ { role: 'system', content: generatePrompt(locale, maxLength) }, { role: 'user', content: diff }, diff --git a/src/index.ts b/src/index.ts index c47bb22..07dae87 100644 --- a/src/index.ts +++ b/src/index.ts @@ -112,7 +112,7 @@ async function main() { ], [ colorize('review', 'cyan'), - 'Generate a code review\nAI will automatically generate code review information for you.\n\nExample:\n $ ai review', + `Generate a code review\nAI will automatically generate code review information for you.\n\nExample:\n ${colorize('$ ai review', 'blue')}`, ], [ colorize('set ', 'cyan'), diff --git a/src/types/index.ts b/src/types/index.ts index 6bb30f5..1e5a132 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -11,3 +11,8 @@ export interface UserSelection { } export type CustomHooksSelection = Omit; + +export interface Staged { + filename: string; + content: string; +} diff --git a/src/utils/index.ts b/src/utils/index.ts index 95ca0d6..813c686 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -1,6 +1,9 @@ import * as path from 'path'; import { promises as fs } from 'fs'; import * as os from 'os'; +import { execSync } from 'node:child_process'; + +import { Staged } from '@/types'; export * from './openai'; export * from './color'; @@ -52,3 +55,26 @@ export async function validatePath(componentPath: string): Promise { throw new Error('Invalid path. The specified directory does not exist.'); } } + +export function getFilesChangedInGitAdd() { + const gitDiff = execSync('git diff --cached --name-status', { encoding: 'utf-8' }); + const files = gitDiff.split('\n'); + + // 过滤掉 lock 文件和被删除的文件 + const ignoredPatterns = [/package-lock\.json$/, /yarn\.lock$/, /pnpm-lock\.yaml$/]; + const filteredFiles = files + .map((line) => { + const [status, file] = line.trim().split('\t'); + return { status, file }; + }) + .filter(({ status, file }) => { + return file && status !== 'D' && !ignoredPatterns.some((pattern) => pattern.test(file)); + }) + .map(({ file }) => file); + + return filteredFiles; +} + +export function allStagedFiles2Message(staged: Staged[]) { + return staged.map((item) => item.content).join('\n'); +}