Skip to content

Commit 9418f67

Browse files
authored
Merge pull request #457 from jcppkkk/feat/commitlint-improve-consistency-handling
refactor(prompts): streamline commit message generation logic
2 parents 60a7650 + fb533f8 commit 9418f67

File tree

1 file changed

+26
-24
lines changed

1 file changed

+26
-24
lines changed

src/prompts.ts

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -183,43 +183,45 @@ export const INIT_DIFF_PROMPT: OpenAI.Chat.Completions.ChatCompletionMessagePara
183183
});`
184184
};
185185

186-
const getContent = (translation: ConsistencyPrompt) => {
187-
const getCommitString = (commitWithScope: string, commitWithoutScope?: string) => {
188-
if (config.OCO_OMIT_SCOPE && commitWithoutScope) {
189-
return config.OCO_EMOJI
190-
? `🐛 ${removeConventionalCommitWord(commitWithoutScope)}`
191-
: commitWithoutScope;
192-
}
193-
return config.OCO_EMOJI
194-
? `🐛 ${removeConventionalCommitWord(commitWithScope)}`
195-
: commitWithScope;
196-
};
186+
const COMMIT_TYPES = {
187+
fix: '🐛',
188+
feat: '✨'
189+
} as const;
197190

198-
const fix = getCommitString(
199-
translation.commitFix,
200-
translation.commitFixOmitScope
201-
);
191+
const generateCommitString = (
192+
type: keyof typeof COMMIT_TYPES,
193+
message: string
194+
): string => {
195+
const cleanMessage = removeConventionalCommitWord(message);
196+
return config.OCO_EMOJI
197+
? `${COMMIT_TYPES[type]} ${cleanMessage}`
198+
: message;
199+
};
200+
201+
const getConsistencyContent = (translation: ConsistencyPrompt) => {
202+
const fixMessage = config.OCO_OMIT_SCOPE && translation.commitFixOmitScope
203+
? translation.commitFixOmitScope
204+
: translation.commitFix;
205+
206+
const featMessage = config.OCO_OMIT_SCOPE && translation.commitFeatOmitScope
207+
? translation.commitFeatOmitScope
208+
: translation.commitFeat;
202209

203-
const feat = config.OCO_OMIT_SCOPE && translation.commitFeatOmitScope
204-
? (config.OCO_EMOJI
205-
? `✨ ${removeConventionalCommitWord(translation.commitFeatOmitScope)}`
206-
: translation.commitFeatOmitScope)
207-
: (config.OCO_EMOJI
208-
? `✨ ${removeConventionalCommitWord(translation.commitFeat)}`
209-
: translation.commitFeat);
210+
const fix = generateCommitString('fix', fixMessage);
211+
const feat = generateCommitString('feat', featMessage);
210212

211213
const description = config.OCO_DESCRIPTION
212214
? translation.commitDescription
213215
: '';
214216

215-
return `${fix}\n${feat}\n${description}`;
217+
return [fix, feat, description].filter(Boolean).join('\n');
216218
};
217219

218220
const INIT_CONSISTENCY_PROMPT = (
219221
translation: ConsistencyPrompt
220222
): OpenAI.Chat.Completions.ChatCompletionMessageParam => ({
221223
role: 'assistant',
222-
content: getContent(translation)
224+
content: getConsistencyContent(translation)
223225
});
224226

225227
export const getMainCommitPrompt = async (

0 commit comments

Comments
 (0)