forked from RomanHotsiy/commitgpt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
122 lines (102 loc) · 3.05 KB
/
index.ts
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#!/usr/bin/env node
import { execSync } from 'child_process';
import enquirer from 'enquirer';
import ora from 'ora';
import parseArgs from 'yargs-parser';
import { ChatGPTClient } from './client.js';
const CUSTOM_MESSAGE_OPTION = '[write own message]...';
const MORE_OPTION = '[ask for more ideas]...';
const spinner = ora();
const argv = parseArgs(process.argv.slice(2));
const conventionalCommit = argv.conventional || argv.c;
const CONVENTIONAL_REQUEST = conventionalCommit ? `following conventional commit (<type>: <subject>)` : '';
let diff = '';
try {
diff = execSync('git diff --cached').toString();
if (!diff) {
console.log('No changes to commit.');
process.exit(0);
}
} catch (e) {
console.log('Failed to run git diff --cached');
process.exit(1);
}
run(diff)
.then(() => {
process.exit(0);
})
.catch(e => {
console.log('Error: ' + e.message);
if ((e as any).details) {
console.log((e as any).details);
}
process.exit(1);
});
async function run(diff: string) {
const api = new ChatGPTClient();
const firstRequest =
`Suggest me a few good commit messages for my commit ${CONVENTIONAL_REQUEST}.\n` +
'```\n' +
diff +
'\n' +
'```\n\n' +
`Output results as a list, not more than 6 items.`;
let firstRequestSent = false;
while (true) {
const choices = await getMessages(
api,
firstRequestSent
? `Suggest a few more commit messages for my changes (without explanations) ${CONVENTIONAL_REQUEST}`
: firstRequest
);
try {
const answer = await enquirer.prompt<{ message: string }>({
type: 'select',
name: 'message',
message: 'Pick a message',
choices,
});
firstRequestSent = true;
if (answer.message === CUSTOM_MESSAGE_OPTION) {
execSync('git commit', { stdio: 'inherit' });
return;
} else if (answer.message === MORE_OPTION) {
continue;
} else {
execSync(`git commit -m '${escapeCommitMessage(answer.message)}'`, { stdio: 'inherit' });
return;
}
} catch (e) {
console.log('Aborted.');
console.log(e);
process.exit(1);
}
}
}
async function getMessages(api: ChatGPTClient, request: string) {
spinner.start('Asking ChatGPT 🤖 for commit messages...');
// send a message and wait for the response
try {
const response = await api.getAnswer(request);
const messages = response
.split('\n')
.filter(line => line.match(/^(\d+\.|-|\*)\s+/))
.map(normalizeMessage);
messages.push(CUSTOM_MESSAGE_OPTION, MORE_OPTION);
return messages;
} catch (e) {
throw e;
}
}
function normalizeMessage(line: string) {
return line
.replace(/^(\d+\.|-|\*)\s+/, '')
.replace(/^[`"']/, '')
.replace(/[`"']$/, '')
.replace(/[`"']:/, ':') // sometimes it formats messages like this: `feat`: message
.replace(/:[`"']/, ':') // sometimes it formats messages like this: `feat:` message
.replace(/\\n/g, '');
}
function escapeCommitMessage(message: string) {
return message.replace(/'/, `''`);
}