forked from insulineru/ai-commit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
42 lines (32 loc) · 1.12 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
import { execSync } from 'child_process';
import { ChatGPTAPI } from 'chatgpt'
import inquirer from 'inquirer';
const getArgs = () => {
return process.argv.slice(2)
.reduce((args, arg) => {
if (arg.startsWith('--')) {
const [flag, value = true] = arg.split('=');
args[flag.slice(2)] = value;
} else if (arg[0] === '-') {
arg.slice(1).split('').forEach(flag => {
args[flag] = true;
});
}
return args;
}, {});
};
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()
const prompt = 'Generate a short commit title based on diff changes above, using gitmoji and conventional commits. Structure: <emoji> <type>: <subject>'
const { text } = await api.sendMessage(`${diff}\n # ${prompt}`)
console.log(`Proposed Commit:\n------------------------------\n${text}\n------------------------------`)
}
main();