Skip to content

Commit 791cb75

Browse files
♻️ refactor(prompt): 重构提交消息生成器功能
- 将大型函数拆分为更小的独立函数模块 - 添加了 useEmoji 参数控制是否使用表情符号 - 抽取示例生成相关的逻辑到独立的辅助函数 - 优化了代码结构和可读性 - 修复了模板字符串中的字符编码问题
1 parent e670326 commit 791cb75

File tree

1 file changed

+147
-77
lines changed

1 file changed

+147
-77
lines changed

src/prompt/prompt.ts

+147-77
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,80 @@
1+
function getMergeCommitsSection(
2+
allowMergeCommits: boolean,
3+
splitChangesInSingleFile: boolean,
4+
useEmoji: boolean
5+
) {
6+
const formatExample = useEmoji
7+
? "<emoji> <type>(<scope>): <subject>"
8+
: "<type>(<scope>): <subject>";
9+
10+
if (!allowMergeCommits) {
11+
return `### Separate Commits
12+
13+
- If multiple file diffs are provided, generate separate commit messages for each file.
14+
- If only one file diff is provided, ${
15+
splitChangesInSingleFile
16+
? "split different types of changes in the file into separate commit messages"
17+
: "combine all changes in the file into a single commit message"
18+
}.
19+
\`\`\`
20+
${formatExample}
21+
<body for changes in file>
22+
\`\`\`
23+
`;
24+
}
25+
26+
return `### Merged Commit
27+
28+
If multiple file diffs are provided, merge them into a single commit message:
29+
\`\`\`
30+
${formatExample}
31+
<body of merged changes>
32+
\`\`\`
33+
`;
34+
}
35+
36+
function getVCSExamples(
37+
vcsType: "svn" | "git",
38+
allowMergeCommits: boolean,
39+
splitChangesInSingleFile: boolean,
40+
useEmoji: boolean
41+
) {
42+
const exampleContent =
43+
vcsType === "git"
44+
? getGitExamples(allowMergeCommits, splitChangesInSingleFile, useEmoji)
45+
: getSVNExamples(allowMergeCommits, splitChangesInSingleFile, useEmoji);
46+
47+
return `### Example (${vcsType.toUpperCase()})
48+
49+
${exampleContent}`;
50+
}
51+
52+
function getGitExamples(
53+
allowMergeCommits: boolean,
54+
splitChangesInSingleFile: boolean,
55+
useEmoji: boolean
56+
) {
57+
return allowMergeCommits
58+
? getMergedGitExample(useEmoji)
59+
: getSeparateGitExample(splitChangesInSingleFile, useEmoji);
60+
}
61+
62+
function getSVNExamples(
63+
allowMergeCommits: boolean,
64+
splitChangesInSingleFile: boolean,
65+
useEmoji: boolean
66+
) {
67+
return allowMergeCommits
68+
? getMergedSVNExample(useEmoji)
69+
: getSeparateSVNExample(splitChangesInSingleFile, useEmoji);
70+
}
71+
172
export function generateCommitMessageSystemPrompt(
273
language: string,
374
allowMergeCommits: boolean,
475
splitChangesInSingleFile: boolean,
5-
vcsType: "svn" | "git"
76+
vcsType: "svn" | "git",
77+
useEmoji: boolean = true
678
) {
779
const VCSUpper = vcsType?.toUpperCase();
880
return `# ${VCSUpper} Commit Message Guide
@@ -13,30 +85,7 @@ All output MUST be in ${language} language. You are to act as a pure ${VCSUpper}
1385
1486
## Output Format
1587
16-
${
17-
allowMergeCommits
18-
? `### Merged Commit
19-
20-
If multiple file diffs are provided, merge them into a single commit message:
21-
\`\`\`
22-
<emoji> <type>(<scope>): <subject>
23-
<body of merged changes>
24-
\`\`\`
25-
`
26-
: `### Separate Commits
27-
28-
- If multiple file diffs are provided, generate separate commit messages for each file.
29-
- If only one file diff is provided, ${
30-
splitChangesInSingleFile
31-
? "split different types of changes in the file into separate commit messages"
32-
: "combine all changes in the file into a single commit message"
33-
}.
34-
\`\`\`
35-
<emoji> <type>(<scope>): <subject>
36-
<body for changes in file>
37-
\`\`\`
38-
`
39-
}
88+
${getMergeCommitsSection(allowMergeCommits, splitChangesInSingleFile, useEmoji)}
4089
4190
## File Status Detection
4291
@@ -69,7 +118,9 @@ By analyzing both the file status and the changes within the file (e.g., diff),
69118
70119
## Type Reference
71120
72-
| Type | Emoji | Description | Example Scopes |
121+
${
122+
useEmoji
123+
? `| Type | Emoji | Description | Example Scopes |
73124
| -------- | ----- | -------------------- | ------------------- |
74125
| feat | ✨ | New feature | user, payment |
75126
| fix | 🐛 | Bug fix | auth, data |
@@ -81,7 +132,21 @@ By analyzing both the file status and the changes within the file (e.g., diff),
81132
| build | 📦 | Build system | webpack, npm |
82133
| ci | 👷 | CI config | Travis, Jenkins |
83134
| chore | 🔧 | Other changes | scripts, config |
84-
| i18n | 🌐 | Internationalization | locale, translation |
135+
| i18n | 🌐 | Internationalization | locale, translation |`
136+
: `| Type | Description | Example Scopes |
137+
| -------- | -------------------- | ------------------- |
138+
| feat | New feature | user, payment |
139+
| fix | Bug fix | auth, data |
140+
| docs | Documentation | README, API |
141+
| style | Code style | formatting |
142+
| refactor | Code refactoring | utils, helpers |
143+
| perf | Performance | query, cache |
144+
| test | Testing | unit, e2e |
145+
| build | Build system | webpack, npm |
146+
| ci | CI config | Travis, Jenkins |
147+
| chore | Other changes | scripts, config |
148+
| i18n | Internationalization | locale, translation |`
149+
}
85150
86151
## Writing Rules
87152
@@ -126,13 +191,18 @@ Remember: All output MUST be in ${language} language. You are to act as a pure c
126191
127192
## ${VCSUpper} Examples
128193
129-
${
130-
vcsType === "git"
131-
? `### Example (Git)
194+
${getVCSExamples(
195+
vcsType,
196+
allowMergeCommits,
197+
splitChangesInSingleFile,
198+
useEmoji
199+
)}`;
200+
}
132201

133-
${
134-
allowMergeCommits
135-
? `#### Merged Commit (allowMergeCommits: true)
202+
// Helper functions for examples generation (implementations omitted for brevity)
203+
function getMergedGitExample(useEmoji: boolean) {
204+
const prefix = useEmoji ? "✨ " : "";
205+
return `#### Merged Commit (allowMergeCommits: true)
136206
137207
- **Input (Multiple Diffs)**:
138208
\`\`\`
@@ -154,16 +224,21 @@ ${
154224
155225
- **Generated Commit Message**:
156226
\`\`\`
157-
feat(app): 添加多个新文件
227+
${prefix}feat(app): 添加多个新文件
158228
- 添加 file1.js 文件
159229
- 添加 file2.js 文件,包含基础日志输出
160-
\`\`\`
161-
`
162-
: `#### Separate Commits (allowMergeCommits: false)
230+
\`\`\``;
231+
}
163232

164-
${
165-
splitChangesInSingleFile
166-
? `- **Input (Single File with Multiple Changes)**:
233+
function getSeparateGitExample(
234+
splitChangesInSingleFile: boolean,
235+
useEmoji: boolean
236+
) {
237+
const featPrefix = useEmoji ? "✨ " : "";
238+
const fixPrefix = useEmoji ? "🐛 " : "";
239+
240+
if (splitChangesInSingleFile) {
241+
return `- **Input (Single File with Multiple Changes)**:
167242
\`\`\`
168243
diff --git a/file.js b/file.js
169244
index e69de29..b6fc4c6 100644
@@ -176,14 +251,15 @@ ${
176251
177252
- **Generated Commit Messages**:
178253
\`\`\`
179-
feat(file): 添加功能 A
254+
${featPrefix}feat(file): 添加功能 A
180255
- 在 file.js 中添加了 "Feature A"
181256
182-
🐛 fix(file): 修复 B 的问题
257+
${fixPrefix}fix(file): 修复 B 的问题
183258
- 在 file.js 中修复了与 B 相关的问题
184-
\`\`\`
185-
`
186-
: `- **Input (Single File with Multiple Changes)**:
259+
\`\`\``;
260+
}
261+
262+
return `- **Input (Single File with Multiple Changes)**:
187263
\`\`\`
188264
diff --git a/file.js b/file.js
189265
index e69de29..b6fc4c6 100644
@@ -196,20 +272,15 @@ ${
196272
197273
- **Generated Commit Message**:
198274
\`\`\`
199-
feat(file): 添加功能 A 和修复问题 B
275+
${featPrefix}feat(file): 添加功能 A 和修复问题 B
200276
- 在 file.js 中添加了 "Feature A"
201277
- 修复了与 B 相关的问题
202-
\`\`\`
203-
`
278+
\`\`\``;
204279
}
205-
`
206-
}
207-
`
208-
: `### Example (SVN)
209280

210-
${
211-
allowMergeCommits
212-
? `#### Merged Commit (allowMergeCommits: true)
281+
function getMergedSVNExample(useEmoji: boolean) {
282+
const prefix = useEmoji ? "✨ " : "";
283+
return `#### Merged Commit (allowMergeCommits: true)
213284
214285
- **Input (Multiple Diffs)**:
215286
\`\`\`
@@ -230,16 +301,21 @@ ${
230301
231302
- **Generated Commit Message**:
232303
\`\`\`
233-
feat(app): 添加多个新文件
304+
${prefix}feat(app): 添加多个新文件
234305
- 添加 file1.js 文件
235306
- 添加 file2.js 文件,包含基础日志输出
236-
\`\`\`
237-
`
238-
: `#### Separate Commits (allowMergeCommits: false)
307+
\`\`\``;
308+
}
239309

240-
${
241-
splitChangesInSingleFile
242-
? `- **Input (Single File with Multiple Changes)**:
310+
function getSeparateSVNExample(
311+
splitChangesInSingleFile: boolean,
312+
useEmoji: boolean
313+
) {
314+
const featPrefix = useEmoji ? "✨ " : "";
315+
const fixPrefix = useEmoji ? "🐛 " : "";
316+
317+
if (splitChangesInSingleFile) {
318+
return `- **Input (Single File with Multiple Changes)**:
243319
\`\`\`
244320
Index: file.js
245321
===================================================================
@@ -252,14 +328,15 @@ ${
252328
253329
- **Generated Commit Messages**:
254330
\`\`\`
255-
feat(file): 添加功能 A
331+
${featPrefix}feat(file): 添加功能 A
256332
- 在 file.js 中添加了 "Feature A"
257333
258-
🐛 fix(file): 修复 B 的问题
334+
${fixPrefix}fix(file): 修复 B 的问题
259335
- 在 file.js 中修复了与 B 相关的问题
260-
\`\`\`
261-
`
262-
: `- **Input (Single File with Multiple Changes)**:
336+
\`\`\``;
337+
}
338+
339+
return `- **Input (Single File with Multiple Changes)**:
263340
\`\`\`
264341
Index: file.js
265342
===================================================================
@@ -272,17 +349,10 @@ ${
272349
273350
- **Generated Commit Message**:
274351
\`\`\`
275-
feat(file): 添加功能 A 和修复问题 B
352+
${featPrefix}feat(file): 添加功能 A 和修复问题 B
276353
- 在 file.js 中添加了 "Feature A"
277-
- 修复了与 B 相关的问题
278-
\`\`\`
279-
`
280-
}
281-
`
282-
}
283-
`
284-
}
285-
`;
354+
- 修复了与 B 相��的问题
355+
\`\`\``;
286356
}
287357

288358
export function generateCommitMessageUserPrompt(language: string) {}

0 commit comments

Comments
 (0)