@sensio-erp/commit-ai 1.0.1

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.
@@ -0,0 +1,127 @@
1
+ #!/usr/bin/env node
2
+
3
+ require("dotenv").config();
4
+ const { execSync } = require("child_process");
5
+ const fs = require("fs");
6
+ const path = require("path");
7
+ const inquirer = require("inquirer");
8
+ const chalk = require("chalk");
9
+ const { generateCommit } = require("../src/openai");
10
+
11
+ if (!process.env.OPENAI_API_KEY) {
12
+ console.error("❌ OPENAI_API_KEY não definida");
13
+ process.exit(1);
14
+ }
15
+
16
+ async function run() {
17
+ try {
18
+ console.log(chalk.cyan("\n🔍 Lendo alterações staged...\n"));
19
+
20
+ const diff = execSync("git diff --cached", {
21
+ encoding: "utf-8",
22
+ maxBuffer: 1024 * 1024 * 10
23
+ });
24
+
25
+ if (!diff.trim()) {
26
+ console.log(chalk.yellow("Nenhuma alteração staged."));
27
+ process.exit(0);
28
+ }
29
+
30
+ const branch = getBranchName();
31
+ const ticket = extractTicket(branch);
32
+
33
+ const instructionsPath = path.join(
34
+ process.cwd(),
35
+ ".github/instructions/copilot-commit-instructions.md.instructions.md"
36
+ );
37
+
38
+ const instructions = fs.existsSync(instructionsPath)
39
+ ? fs.readFileSync(instructionsPath, "utf-8")
40
+ : "";
41
+
42
+ const prompt = `
43
+ ${instructions}
44
+
45
+ ---
46
+
47
+ ## CONTEXT
48
+
49
+ - Branch: ${branch}
50
+ - Ticket detectado: ${ticket || "nenhum"}
51
+
52
+ ---
53
+
54
+ ## DIFF
55
+
56
+ ${diff}
57
+ `;
58
+
59
+ console.log(chalk.cyan("🧠 Gerando commit...\n"));
60
+
61
+ const commitMessage = await generateCommit(prompt);
62
+
63
+ console.log(chalk.green("\n✨ Commit sugerido:\n"));
64
+ console.log(chalk.white(commitMessage));
65
+ console.log("\n");
66
+
67
+ const answers = await inquirer.prompt([
68
+ {
69
+ type: "editor",
70
+ name: "message",
71
+ message: "✏️ Edite o commit se quiser:",
72
+ default: commitMessage
73
+ },
74
+ {
75
+ type: "confirm",
76
+ name: "confirm",
77
+ message: "🚀 Deseja executar o commit?",
78
+ default: true
79
+ }
80
+ ]);
81
+
82
+ const finalMessage = answers.message.trim();
83
+
84
+ if (!answers.confirm) {
85
+ console.log(chalk.red("❌ Commit cancelado."));
86
+ process.exit(0);
87
+ }
88
+
89
+ commitWithMessage(finalMessage);
90
+
91
+ console.log(chalk.green("\n✅ Commit realizado com sucesso!\n"));
92
+ } catch (err) {
93
+ console.error(chalk.red("Erro:"), err.message);
94
+ }
95
+ }
96
+
97
+ function getBranchName() {
98
+ return execSync("git rev-parse --abbrev-ref HEAD", {
99
+ encoding: "utf-8"
100
+ }).trim();
101
+ }
102
+
103
+ function extractTicket(branch) {
104
+ const match = branch.match(/(RP-\d+|CHRIS-\d+)/i);
105
+ return match ? match[0].toUpperCase() : null;
106
+ }
107
+
108
+ function escapeCommit(message) {
109
+ return message.replace(/"/g, '\\"');
110
+ }
111
+
112
+ function commitWithMessage(message) {
113
+ const lines = message.split("\n").filter(Boolean);
114
+
115
+ const title = lines[0];
116
+ const body = lines.slice(1);
117
+
118
+ let command = `git commit -m "${escapeCommit(title)}"`;
119
+
120
+ body.forEach(line => {
121
+ command += ` -m "${escapeCommit(line)}"`;
122
+ });
123
+
124
+ execSync(command, { stdio: "inherit" });
125
+ }
126
+
127
+ run();
Binary file
package/package.json ADDED
@@ -0,0 +1,13 @@
1
+ {
2
+ "name": "@sensio-erp/commit-ai",
3
+ "version": "1.0.1",
4
+ "bin": {
5
+ "commit-ai": "./bin/commit-ai.js"
6
+ },
7
+ "type": "commonjs",
8
+ "dependencies": {
9
+ "openai": "^4.0.0",
10
+ "inquirer": "^9.0.0",
11
+ "chalk": "^5.0.0"
12
+ }
13
+ }
package/src/openai.js ADDED
@@ -0,0 +1,16 @@
1
+ const OpenAI = require("openai");
2
+
3
+ const client = new OpenAI({
4
+ apiKey: process.env.OPENAI_API_KEY
5
+ });
6
+
7
+ async function generateCommit(prompt) {
8
+ const response = await client.responses.create({
9
+ model: "gpt-4.1-mini",
10
+ input: prompt
11
+ });
12
+
13
+ return response.output_text.trim();
14
+ }
15
+
16
+ module.exports = { generateCommit };