feat(config.ts): add OCO_MESSAGE_TEMPLATE_PLACEHOLDER configuration item to allow users to customize the message template placeholder (#208)

feat(commit.ts): add check for message templates in extraArgs and replace OCO_MESSAGE_TEMPLATE_PLACEHOLDER with generated commit message if found
docs(README.md): add documentation for OCO_MESSAGE_TEMPLATE_PLACEHOLDER configuration item and how to use it in the command line (#205)
This commit is contained in:
seho
2023-07-05 14:27:43 +08:00
committed by GitHub
parent ccfd24a9e5
commit 3c0a271bf8
3 changed files with 39 additions and 3 deletions
+18 -1
View File
@@ -18,25 +18,42 @@ import {
multiselect,
select
} from '@clack/prompts';
import {
getConfig
} from '../commands/config';
import chalk from 'chalk';
import { trytm } from '../utils/trytm';
const config = getConfig();
const getGitRemotes = async () => {
const { stdout } = await execa('git', ['remote']);
return stdout.split('\n').filter((remote) => Boolean(remote.trim()));
};
// Check for the presence of message templates
const checkMessageTemplate = (extraArgs: string[]): string | false => {
for(const key in extraArgs){
if(extraArgs[key].includes(config?.OCO_MESSAGE_TEMPLATE_PLACEHOLDER)) return extraArgs[key];
}
return false;
};
const generateCommitMessageFromGitDiff = async (
diff: string,
extraArgs: string[]
): Promise<void> => {
const messageTemplate = checkMessageTemplate(extraArgs);
await assertGitRepo();
const commitSpinner = spinner();
commitSpinner.start('Generating the commit message');
try {
const commitMessage = await generateCommitMessageByDiff(diff);
let commitMessage = await generateCommitMessageByDiff(diff);
if(typeof messageTemplate === 'string'){
commitMessage = messageTemplate.replace(config?.OCO_MESSAGE_TEMPLATE_PLACEHOLDER, commitMessage);
}
commitSpinner.stop('📝 Commit message generated');
outro(