Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion bin/smartcommit.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#!/usr/bin/env node

import { program } from "commander";
import chalk from "chalk";
import { run } from "../src/index";
import { CancellationError } from "../src/utils/errors";
import pkg from "../package.json";
program
.name("smartcommit")
Expand All @@ -11,7 +13,22 @@ program
.option("--auto", "Auto accept commit without confirmation")
.option("--model <name>", "Specify Ollama model");
program.action(async (options) => {
await run(options);
try {
await run(options);
process.exit(0);
} catch (error: unknown) {
if (error instanceof CancellationError) {
console.log(error.message);
process.exit(0);
}

if (error instanceof Error) {
console.error(chalk.red(error.message));
} else {
console.error(chalk.red("An unknown error occurred."));
}
process.exit(1);
}
});

program.parse();
15 changes: 15 additions & 0 deletions src/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { describe, it, expect } from 'vitest';
import { detectScope } from './analyzer/scopeDetector';
import { ValidationError, CancellationError } from './utils/errors';

describe('detectScope', () => {
it('should detect scope from src directory', () => {
Expand All @@ -17,3 +18,17 @@ describe('detectScope', () => {
expect(detectScope(files)).toBe('ui');
});
});

describe('Errors', () => {
it('should create ValidationError with correct name and message', () => {
const error = new ValidationError('test error');
expect(error.name).toBe('ValidationError');
expect(error.message).toBe('test error');
});

it('should create CancellationError with default message', () => {
const error = new CancellationError();
expect(error.name).toBe('CancellationError');
expect(error.message).toBe('Commit cancelled.');
});
});
11 changes: 4 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { commit } from "./git/commit";
import { enhanceCommit } from "./llm/ollamaEnhancer";
import { loadConfig } from "./config/loadConfig";
import { isOllamaRunning, getBestModel } from "./llm/checkOllama";
import { ValidationError, CancellationError } from "./utils/errors";

interface CliOptions {
ai?: boolean;
Expand All @@ -25,16 +26,13 @@ export async function run(options: CliOptions) {
// Ensure we are inside a Git repo
const repo = await isGitRepo();
if (!repo) {
console.log(chalk.red("Not inside a Git repository."));
process.exit(1);
throw new ValidationError("Not inside a Git repository.");
}

const stagedFiles = await getStagedFiles();

if (stagedFiles.length === 0) {
console.log(chalk.yellow("No staged changes found."));
console.log("Stage changes using: git add <file>");
process.exit(0);
throw new ValidationError("No staged changes found.\nStage changes using: git add <file>");
}

// Enrich file stats
Expand Down Expand Up @@ -98,8 +96,7 @@ export async function run(options: CliOptions) {
const result = await confirmCommit(commitMessage);

if (!result) {
console.log("Commit cancelled.");
process.exit(0);
throw new CancellationError();
}

finalMessage = result;
Expand Down
20 changes: 20 additions & 0 deletions src/utils/errors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
export class GitBunError extends Error {
constructor(message: string) {
super(message);
this.name = "GitBunError";
}
}

export class ValidationError extends GitBunError {
constructor(message: string) {
super(message);
this.name = "ValidationError";
}
}

export class CancellationError extends GitBunError {
constructor(message: string = "Commit cancelled.") {
super(message);
this.name = "CancellationError";
}
}
Loading