forked from insulineru/ai-commit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
54 lines (44 loc) Β· 1.77 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import { execSync } from 'child_process';
import { ChatGPTAPI } from 'chatgpt'
import inquirer from 'inquirer';
import { getArgs } from './helpers.js';
const args = getArgs();
const apiKey = args.apiKey || process.env.OPENAI_API_KEY || (() => {
error('Please set the OPENAI_API_KEY environment variable.');
process.exit(1);
})();
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 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)
console.log(`Proposed Commit:\n------------------------------\n${text}\n------------------------------`)
inquirer
.prompt([
{
type: 'confirm',
name: 'continue',
message: 'Do you want to continue?',
default: true,
},
])
.then((answers) => {
if (!answers.continue) {
console.log('Commit aborted by user π
ββοΈ');
process.exit(1);
}
// info('Committing Message...');
console.log('Committing Message... π ')
execSync(`git commit -F -`, { input: text });
console.log('Commit Successful! π')
});
}
main();