-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
159 lines (135 loc) · 3.77 KB
/
index.js
File metadata and controls
159 lines (135 loc) · 3.77 KB
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#!/usr/bin/env node
import { select, Separator } from "@inquirer/prompts";
import "dotenv/config";
import meow from "meow";
import ora from "ora";
import { fetch, Headers, Request, Response } from "undici";
import {
getCommitMessagesWithChatGPT,
getCommitMessagesWithGemini,
} from "./src/ai.js";
import { exitMessages, SYSTEM_PROMPT } from "./src/constants.js";
import {
color,
detectLockFiles,
executeGitCommand,
filterLockFiles,
getAvailableModel,
} from "./src/utils.js";
// Polyfill for fetch, Headers, Request, and Response if not available
if (typeof global.fetch === "undefined") {
global.fetch = fetch;
}
if (typeof global.Headers === "undefined") {
global.Headers = Headers;
}
if (typeof global.Request === "undefined") {
global.Request = Request;
}
if (typeof global.Response === "undefined") {
global.Response = Response;
}
// Polyfill for Array.prototype.findLastIndex if not available
if (!Array.prototype.findLastIndex) {
Array.prototype.findLastIndex = function (predicate, thisArg) {
for (let i = this.length - 1; i >= 0; i--) {
if (predicate.call(thisArg, this[i], i, this)) {
return i;
}
}
return -1;
};
}
const cli = meow(
`
Usage
$ git-bird [options]
Options
-m, --model Specify the AI model to use (ChatGPT or Gemini).
Examples
$ git-bird -m chatgpt
`,
{
importMeta: import.meta,
flags: {
model: {
type: "string",
shortFlag: "m",
default: "chatgpt",
},
},
}
);
async function suggestCommitMessage(model) {
const spinner = ora("Fetching commit suggestions...").start();
spinner.clear(); // Temporary fix for spinner showing multiple times
try {
let diffOutput = executeGitCommand(["diff", "--staged"], false);
if (!diffOutput.trim()) {
spinner.stop();
console.log("> Info: No changes to commit. Please stage some changes.");
return;
}
const hasOnlySourceCode = detectLockFiles();
if (hasOnlySourceCode === -1) {
spinner.stop();
console.log(
"> Info: Only Lock file changes detected. No source code changes to commit."
);
return;
} else if (hasOnlySourceCode === 0) {
diffOutput = filterLockFiles(diffOutput);
}
const prompt = SYSTEM_PROMPT + "\n" + diffOutput;
let commitMessages = [];
if (model === "chatgpt") {
commitMessages = await getCommitMessagesWithChatGPT(prompt);
} else {
commitMessages = await getCommitMessagesWithGemini(prompt);
}
spinner.stop();
const choice = await select({
message: "Select a commit message:",
choices: [
...commitMessages,
new Separator(),
{ name: "Generate Again", value: "again" },
{ name: "Exit", value: "exit" },
new Separator(),
],
});
if (choice === "exit") {
console.log(
`> ${exitMessages[Math.floor(Math.random() * exitMessages.length)]}`
);
return;
}
if (choice === "again") {
return await suggestCommitMessage(model);
}
const commitMessage = choice;
executeGitCommand(["commit", "-m", commitMessage]);
} catch (error) {
console.error("> Error: Something went wrong:", error);
} finally {
spinner.stop();
}
}
(async () => {
let { model } = cli.flags;
model = model.toLowerCase();
try {
executeGitCommand(["rev-parse", "--is-inside-work-tree"], false);
} catch (error) {
console.error("> Error: The current directory is not a Git repository.");
return;
}
const availableModel = getAvailableModel(model);
if (!availableModel) {
console.error("> Info: No API key found. Please set the API keys.");
return;
}
model = availableModel;
console.log(`> Using ${color(model)} model...`);
await suggestCommitMessage(model);
})();