Skip to content

Commit

Permalink
fix: 🧩 fixed the error caused by attempting to read a non-existent file.
Browse files Browse the repository at this point in the history
  • Loading branch information
xun082 committed Jun 24, 2024
1 parent e430f21 commit 18e7d35
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 51 deletions.
23 changes: 0 additions & 23 deletions src/core/code-review/help.ts

This file was deleted.

3 changes: 1 addition & 2 deletions src/core/code-review/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
23 changes: 0 additions & 23 deletions src/core/commit/help.ts

This file was deleted.

3 changes: 2 additions & 1 deletion src/core/commit/index.ts
Original file line number Diff line number Diff line change
@@ -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('-- 开始读取缓存区文件更改');

Expand Down
1 change: 0 additions & 1 deletion src/core/commit/prompt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 <key> <value>', 'cyan'),
Expand Down
5 changes: 5 additions & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,8 @@ export interface UserSelection {
}

export type CustomHooksSelection = Omit<UserSelection, 'cssOption'>;

export interface Staged {
filename: string;
content: string;
}
26 changes: 26 additions & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -52,3 +55,26 @@ export async function validatePath(componentPath: string): Promise<void> {
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');
}

0 comments on commit 18e7d35

Please sign in to comment.