commit-name-generator 1.0.1 → 1.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/dist/index.js +57 -16
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -5,6 +5,7 @@ import { Command } from "commander";
5
5
  import chalk from "chalk";
6
6
  import { simpleGit } from "simple-git";
7
7
  var program = new Command();
8
+ var apiKey = process.env.GEMINI_API_KEY;
8
9
  program.name("commit-name-generator").description("Generate Conventional Commit messages from Git changes").version("1.0.0").option("-a, --all", "Analyze staged and unstaged changes").option("-e, --edit", "Open the generated message in the Git editor").action(async (options) => {
9
10
  const git = simpleGit();
10
11
  try {
@@ -23,7 +24,7 @@ program.name("commit-name-generator").description("Generate Conventional Commit
23
24
  );
24
25
  return;
25
26
  }
26
- const suggestedMessage = generateCommitMessage(diff);
27
+ const suggestedMessage = await generateCommitMessage(diff);
27
28
  console.log(chalk.cyan("Changes detected."));
28
29
  console.log(chalk.green("Suggested commit:"));
29
30
  console.log(chalk.bold(suggestedMessage));
@@ -46,23 +47,63 @@ program.name("commit-name-generator").description("Generate Conventional Commit
46
47
  process.exitCode = 1;
47
48
  }
48
49
  });
49
- function generateCommitMessage(diff) {
50
- const normalizedDiff = diff.toLowerCase();
51
- if (normalizedDiff.includes(".test.") || normalizedDiff.includes(".spec.") || normalizedDiff.includes("__tests__")) {
52
- return "test: update test coverage";
50
+ async function generateCommitMessage(diff) {
51
+ const apiKey2 = process.env.GEMINI_API_KEY;
52
+ if (!apiKey2) {
53
+ throw new Error(
54
+ "GEMINI_API_KEY is missing. Create a key at https://aistudio.google.com/apikey"
55
+ );
53
56
  }
54
- if (normalizedDiff.includes("readme") || normalizedDiff.includes(".md")) {
55
- return "docs: update documentation";
56
- }
57
- if (normalizedDiff.includes("package.json") || normalizedDiff.includes("package-lock.json")) {
58
- return "chore: update project dependencies";
59
- }
60
- if (normalizedDiff.includes("fix") || normalizedDiff.includes("error") || normalizedDiff.includes("bug")) {
61
- return "fix: resolve application issue";
57
+ const limitedDiff = diff.slice(0, 3e4);
58
+ const prompt = `
59
+ Analyze the following staged Git diff.
60
+
61
+ Generate one concise Conventional Commit message.
62
+
63
+ Requirements:
64
+ - Use feat, fix, docs, test, refactor, style, perf, build, ci, or chore.
65
+ - Add a scope when it is clear.
66
+ - Describe the real change, not only the filenames.
67
+ - Keep the first line under 72 characters.
68
+ - Return only the commit message.
69
+ - Do not use Markdown or quotation marks.
70
+
71
+ Git diff:
72
+ ${limitedDiff}
73
+ `.trim();
74
+ const response = await fetch(
75
+ "https://generativelanguage.googleapis.com/v1beta/models/gemini-3.1-flash-lite:generateContent",
76
+ {
77
+ method: "POST",
78
+ headers: {
79
+ "Content-Type": "application/json",
80
+ "x-goog-api-key": apiKey2
81
+ },
82
+ body: JSON.stringify({
83
+ contents: [
84
+ {
85
+ role: "user",
86
+ parts: [{ text: prompt }]
87
+ }
88
+ ],
89
+ generationConfig: {
90
+ temperature: 0.2,
91
+ maxOutputTokens: 100
92
+ }
93
+ })
94
+ }
95
+ );
96
+ if (!response.ok) {
97
+ const details = await response.text();
98
+ throw new Error(
99
+ `Gemini request failed with status ${response.status}: ${details}`
100
+ );
62
101
  }
63
- if (normalizedDiff.includes("new file mode") || normalizedDiff.includes("export function") || normalizedDiff.includes("export const")) {
64
- return "feat: add new functionality";
102
+ const result = await response.json();
103
+ const message = result.candidates?.[0]?.content?.parts?.[0]?.text?.trim();
104
+ if (!message) {
105
+ throw new Error("Gemini did not generate a commit message.");
65
106
  }
66
- return "chore: update project files";
107
+ return message.replace(/^["'`]+|["'`]+$/g, "");
67
108
  }
68
109
  await program.parseAsync(process.argv);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "commit-name-generator",
3
- "version": "1.0.1",
3
+ "version": "1.0.2",
4
4
  "description": "Generate Conventional Commit messages from Git changes",
5
5
  "author": "Taha Kourani",
6
6
  "license": "MIT",