Skip to content

Commit

Permalink
Merge branch 'main' of github.com:xun082/create-ai-toolkit
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaoFeng5210 committed Jun 24, 2024
2 parents d064636 + 2f5daf9 commit 94b7e16
Show file tree
Hide file tree
Showing 10 changed files with 55 additions and 66 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
node_modules
dist
.idea
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
2 changes: 1 addition & 1 deletion src/core/commit/gitCommit.ts
Original file line number Diff line number Diff line change
@@ -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);
Expand Down
36 changes: 0 additions & 36 deletions src/core/commit/help.ts

This file was deleted.

5 changes: 3 additions & 2 deletions src/core/commit/index.ts
Original file line number Diff line number Diff line change
@@ -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('开始读取缓存区文件更改')));
// 获取缓存区的文件列表
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;
}
43 changes: 43 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,43 @@ 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');
}

/**
* 自动执行提交信息
*/
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('正在提交中...');
}

0 comments on commit 94b7e16

Please sign in to comment.