diff --git a/.gitignore b/.gitignore index f06235c..82ee25a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ node_modules dist +.idea 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/gitCommit.ts b/src/core/commit/gitCommit.ts index 9cf9ea3..8fc27fa 100644 --- a/src/core/commit/gitCommit.ts +++ b/src/core/commit/gitCommit.ts @@ -1,6 +1,6 @@ import { cancel, confirm, note, outro, text } from '@clack/prompts'; -import { autoCommit, remindCommiting } from '@/core/commit/help'; +import { autoCommit, remindCommiting } from '@/utils'; async function selectAIMsgOrManualMsg(msg: string) { const code = await autoCommit(msg); diff --git a/src/core/commit/help.ts b/src/core/commit/help.ts deleted file mode 100644 index 331a704..0000000 --- a/src/core/commit/help.ts +++ /dev/null @@ -1,36 +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$/]; - return files.filter((file) => file && !ignoredPatterns.some((pattern) => pattern.test(file))); -} - -interface Staged { - filename: string; - content: string; -} - -export function allStagedFiles2Message(staged: Staged[]) { - return staged.map((item) => item.content).join('\n'); -} - -/** - * 自动执行提交信息 - */ -export async function autoCommit(commitMsg: string) { - try { - const result = execSync(`git commit -m "${commitMsg}"`); - return 0; - } catch (e) { - return -1; - } -} - -// TODO: 看看以后有什么更好的方式提醒用户,目前先打log -export function remindCommiting() { - console.log('正在提交中...'); -} diff --git a/src/core/commit/index.ts b/src/core/commit/index.ts index ee6f877..7d7ff9c 100644 --- a/src/core/commit/index.ts +++ b/src/core/commit/index.ts @@ -1,11 +1,12 @@ import fs from 'node:fs'; import chalk from 'chalk'; -import { cancel, intro, outro, spinner, confirm, text, note } from '@clack/prompts'; +import { cancel, intro, spinner } from '@clack/prompts'; import selectCommitMsg from './gitCommit'; -import { allStagedFiles2Message, autoCommit, getFilesChangedInGitAdd } from './help'; import { createChatCompletion } from './openai'; +import { getFilesChangedInGitAdd, allStagedFiles2Message } from '@/utils'; + export default async function commitMessage() { intro(chalk.bgCyan(chalk.black('开始读取缓存区文件更改'))); // 获取缓存区的文件列表 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..4a6e8e0 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,43 @@ 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'); +} + +/** + * 自动执行提交信息 + */ +export async function autoCommit(commitMsg: string) { + try { + const result = execSync(`git commit -m "${commitMsg}"`); + return 0; + } catch (e) { + return -1; + } +} + +// TODO: 看看以后有什么更好的方式提醒用户,目前先打log +export function remindCommiting() { + console.log('正在提交中...'); +}