gitwit
Version:
AI-powered commit message generator from staged git changes
84 lines (67 loc) ⢠2.94 kB
JavaScript
import { generateCommitMessage, commitChanges, checkGitRepo, checkStagedChanges } from "../src/generate.js";
import chalk from "chalk";
import ora from "ora";
const args = process.argv.slice(2);
const autoCommit = args.includes('--commit') || args.includes('-c');
const help = args.includes('--help') || args.includes('-h');
if (help) {
console.log(`
${chalk.bold('gitwit')} - AI-powered commit message generator
${chalk.bold('Usage:')}
npx gitwit [options]
${chalk.bold('Options:')}
-c, --commit Auto-commit with the generated message
-h, --help Show this help message
${chalk.bold('Examples:')}
npx gitwit # Generate commit message
npx gitwit --commit # Generate and auto-commit
git add . && npx gitwit -c # Stage changes and auto-commit
${chalk.bold('Requirements:')}
- Git repository with staged changes
- OPENAI_API_KEY environment variable
${chalk.bold('Environment:')}
Set your OpenAI API key:
export OPENAI_API_KEY=your_api_key_here
`);
process.exit(0);
}
(async () => {
try {
// Check if we're in a git repository
const spinner = ora('Checking git repository...').start();
await checkGitRepo();
spinner.succeed('Git repository found');
// Check if there are staged changes
spinner.start('Checking staged changes...');
const hasStagedChanges = await checkStagedChanges();
if (!hasStagedChanges) {
spinner.fail('No staged changes found');
console.log(chalk.yellow('\nš” Tip: Stage your changes first with ') + chalk.bold('git add .'));
process.exit(1);
}
spinner.succeed('Staged changes found');
// Generate commit message
spinner.start('Generating commit message...');
const message = await generateCommitMessage();
spinner.succeed('Commit message generated');
console.log(chalk.bold('\nš Generated commit message:'));
console.log(chalk.green(`"${message}"`));
if (autoCommit) {
console.log(chalk.yellow('\nā” Auto-committing...'));
await commitChanges(message);
console.log(chalk.green('ā
Changes committed successfully!'));
} else {
console.log(chalk.cyan('\nš” To commit with this message, run:'));
console.log(chalk.bold(`git commit -m "${message}"`));
console.log(chalk.dim('\nOr use --commit flag to auto-commit next time'));
}
} catch (error) {
console.error(chalk.red('\nā Error:'), error.message);
if (error.message.includes('API key')) {
console.log(chalk.yellow('\nš” Set your OpenAI API key:'));
console.log(chalk.bold('export OPENAI_API_KEY=your_api_key_here'));
}
process.exit(1);
}
})();