smart-git-commit
Version:
AI-powered smart commit message generator for Git diffs.
57 lines (45 loc) • 1.43 kB
JavaScript
import inquirer from "inquirer";
import { execSync } from "child_process";
import getGitDiff from "../getDiff/index.js";
import parseDiff from "../parseDiff/index.js";
import suggestCommit from "../suggestCommit/index.js";
import { getApiKey } from "../utils/getApiKey.js";
async function main() {
const args = process.argv.slice(2);
if (args[0] === "config" && args[1] === "--api-key") {
await getApiKey(true);
console.log("API Key updated successfully.");
process.exit(0);
}
const diff = getGitDiff();
const fileChanges = parseDiff(diff);
if (fileChanges.length === 0) {
console.log("No staged changes found.");
process.exit(0);
}
const suggestion = await suggestCommit(fileChanges);
const { confirm } = await inquirer.prompt([
{
type: "confirm",
name: "confirm",
message: `💡 Suggested commit message:\n\n ${suggestion}\n\nUse this message?`,
default: true,
},
]);
let finalMessage = suggestion;
if (!confirm) {
const { customMessage } = await inquirer.prompt([
{
type: "input",
name: "customMessage",
message: "Enter your custom commit message:",
validate: (input) =>
input.trim() !== "" || "Commit message cannot be empty.",
},
]);
finalMessage = customMessage.trim();
}
execSync(`git commit -m "${finalMessage}"`, { stdio: "inherit" });
}
main();