Formatting
This commit is contained in:
22
aicommits.ts
22
aicommits.ts
@@ -13,7 +13,7 @@ export async function main() {
|
|||||||
if (!OPENAI_API_KEY) {
|
if (!OPENAI_API_KEY) {
|
||||||
console.error(
|
console.error(
|
||||||
chalk.white("▲ ") +
|
chalk.white("▲ ") +
|
||||||
"Please save your OpenAI API key as an env variable by doing 'export OPENAI_API_KEY=YOUR_API_KEY'"
|
"Please save your OpenAI API key as an env variable by doing 'export OPENAI_API_KEY=YOUR_API_KEY'",
|
||||||
);
|
);
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
}
|
}
|
||||||
@@ -31,13 +31,13 @@ export async function main() {
|
|||||||
`git diff --cached . ":(exclude)package-lock.json" ":(exclude)yarn.lock" ":(exclude)pnpm-lock.yaml"`,
|
`git diff --cached . ":(exclude)package-lock.json" ":(exclude)yarn.lock" ":(exclude)pnpm-lock.yaml"`,
|
||||||
{
|
{
|
||||||
encoding: "utf8",
|
encoding: "utf8",
|
||||||
}
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
if (!diff) {
|
if (!diff) {
|
||||||
console.log(
|
console.log(
|
||||||
chalk.white("▲ ") +
|
chalk.white("▲ ") +
|
||||||
"No staged changes found. Make sure there are changes and run `git add .`"
|
"No staged changes found. Make sure there are changes and run `git add .`",
|
||||||
);
|
);
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
}
|
}
|
||||||
@@ -45,20 +45,22 @@ export async function main() {
|
|||||||
// Accounting for GPT-3's input req of 4k tokens (approx 8k chars)
|
// Accounting for GPT-3's input req of 4k tokens (approx 8k chars)
|
||||||
if (diff.length > 8000) {
|
if (diff.length > 8000) {
|
||||||
console.log(
|
console.log(
|
||||||
chalk.white("▲ ") + "The diff is too large to write a commit message."
|
chalk.white("▲ ") + "The diff is too large to write a commit message.",
|
||||||
);
|
);
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
let prompt = `I want you to act like a git commit message writer. I will input a git diff and your job is to convert it into a useful commit message. Do not preface the commit with anything, use the present tense, return a complete sentence, and do not repeat yourself: ${diff}`;
|
let prompt =
|
||||||
|
`I want you to act like a git commit message writer. I will input a git diff and your job is to convert it into a useful commit message. Do not preface the commit with anything, use the present tense, return a complete sentence, and do not repeat yourself: ${diff}`;
|
||||||
|
|
||||||
console.log(
|
console.log(
|
||||||
chalk.white("▲ ") + chalk.gray("Generating your AI commit message...\n")
|
chalk.white("▲ ") + chalk.gray("Generating your AI commit message...\n"),
|
||||||
);
|
);
|
||||||
try {
|
try {
|
||||||
const aiCommitMessage = await generateCommitMessage(prompt);
|
const aiCommitMessage = await generateCommitMessage(prompt);
|
||||||
console.log(
|
console.log(
|
||||||
chalk.white("▲ ") + chalk.bold("Commit message: ") + aiCommitMessage + "\n"
|
chalk.white("▲ ") + chalk.bold("Commit message: ") + aiCommitMessage +
|
||||||
|
"\n",
|
||||||
);
|
);
|
||||||
|
|
||||||
const confirmationMessage = await inquirer.prompt([
|
const confirmationMessage = await inquirer.prompt([
|
||||||
@@ -79,7 +81,6 @@ export async function main() {
|
|||||||
stdio: "inherit",
|
stdio: "inherit",
|
||||||
encoding: "utf8",
|
encoding: "utf8",
|
||||||
});
|
});
|
||||||
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error(chalk.white("▲ ") + chalk.red(e.message));
|
console.error(chalk.white("▲ ") + chalk.red(e.message));
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
@@ -108,7 +109,10 @@ async function generateCommitMessage(prompt: string) {
|
|||||||
});
|
});
|
||||||
if (response.status !== 200) {
|
if (response.status !== 200) {
|
||||||
const errorJson: any = await response.json();
|
const errorJson: any = await response.json();
|
||||||
throw new Error(`OpenAI API failed while processing the request '${errorJson?.error?.message}'`);
|
throw new Error(
|
||||||
|
`OpenAI API failed while processing the request '${errorJson?.error
|
||||||
|
?.message}'`,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
const json: any = await response.json();
|
const json: any = await response.json();
|
||||||
|
|||||||
Reference in New Issue
Block a user