Skip to content

Commit

Permalink
✨ feat: Update index.js
Browse files Browse the repository at this point in the history
  • Loading branch information
insulineru committed Feb 12, 2023
1 parent 3823b01 commit 264ab69
Showing 1 changed file with 36 additions and 11 deletions.
47 changes: 36 additions & 11 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 14,7 @@ const api = new ChatGPTAPI({
apiKey,
})

async function main() {
const diff = execSync('git diff --staged').toString()

// Handle empty diff
if (!diff) {
console.log('No changes to commit πŸ™…');
console.log('May be you forgot to add the files? Try `git add .` and then run this script again.')
process.exit(1);
}

const generateSingleCommit = async () => {
const prompt = 'I want you to act as a commit message generator. I will provide you with my code changes as a git diff and I would like you to generate an appropriate commit message. Try to understand the meaning of the changes, not just the name of the file. In our project, we use conventional commits and gitmoji to design the messages. The commit structure should be of `<emoji> <type in lowercase>: <subject>`\nHere is a list of changes:\n'

const { text } = await api.sendMessage(prompt diff)
Expand Down Expand Up @@ -51,4 42,38 @@ async function main() {
});
}

main();
const generateListCommits = async (diff) => {
const prompt = 'I want you to act as a commit message generator. I will provide you with my code changes as a git diff and I would like you to generate an appropriate commit message. Generate 5 variants and send them in one line, separated by commas. Try to understand the meaning of the changes, not just the name of the file. In our project, we use conventional commits and gitmoji to design the messages. The commit structure should be of `<emoji> <type in lowercase>: <subject>`\nHere is a list of changes:\n'

const { text } = await api.sendMessage(prompt diff)

const msgs = text.split(',').map((msg) => msg.trim())

inquirer.prompt([
{
type: 'list',
name: 'commit',
message: 'Select a commit message',
choices: msgs,
},
]).then((answers) => {
console.log('Committing Message... πŸš€ ')
execSync(`git commit -F -`, { input: answers.commit });
console.log('Commit Successful! πŸŽ‰')
});
}

async function generateAICommit() {
const diff = execSync('git diff --staged').toString()

// Handle empty diff
if (!diff) {
console.log('No changes to commit πŸ™…');
console.log('May be you forgot to add the files? Try `git add .` and then run this script again.')
process.exit(1);
}

args.list ? await generateListCommits(diff) : await generateSingleCommit(diff)
}

generateAICommit();

0 comments on commit 264ab69

Please sign in to comment.