@gilbert_oliveira/commit-wizard 1.2.2 → 2.0.0

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,177 @@
1
+ import { execSync } from 'child_process';
2
+
3
+ export interface GitStatus {
4
+ hasStaged: boolean;
5
+ stagedFiles: string[];
6
+ diff: string;
7
+ }
8
+
9
+ export interface GitCommitResult {
10
+ success: boolean;
11
+ hash?: string;
12
+ message?: string;
13
+ error?: string;
14
+ }
15
+
16
+ /**
17
+ * Verifica se estamos em um repositório Git
18
+ */
19
+ export function isGitRepository(): boolean {
20
+ try {
21
+ execSync('git rev-parse --git-dir', { stdio: 'ignore' });
22
+ return true;
23
+ } catch {
24
+ return false;
25
+ }
26
+ }
27
+
28
+ /**
29
+ * Obtém o status dos arquivos staged e o diff
30
+ */
31
+ export function getGitStatus(): GitStatus {
32
+ try {
33
+ // Verificar arquivos staged
34
+ const stagedOutput = execSync('git diff --cached --name-only', {
35
+ encoding: 'utf-8',
36
+ stdio: 'pipe',
37
+ });
38
+
39
+ const stagedFiles = stagedOutput
40
+ .trim()
41
+ .split('\n')
42
+ .filter((file) => file.length > 0);
43
+
44
+ // Obter diff dos arquivos staged
45
+ const diff =
46
+ stagedFiles.length > 0
47
+ ? execSync('git diff --cached', { encoding: 'utf-8', stdio: 'pipe' })
48
+ : '';
49
+
50
+ return {
51
+ hasStaged: stagedFiles.length > 0,
52
+ stagedFiles,
53
+ diff: diff.trim(),
54
+ };
55
+ } catch (error) {
56
+ throw new Error(
57
+ `Erro ao obter status do Git: ${error instanceof Error ? error.message : 'Erro desconhecido'}`
58
+ );
59
+ }
60
+ }
61
+
62
+ /**
63
+ * Obtém o diff de um arquivo específico
64
+ */
65
+ export function getFileDiff(filename: string): string {
66
+ try {
67
+ return execSync(`git diff --cached -- "${filename}"`, {
68
+ encoding: 'utf-8',
69
+ stdio: 'pipe',
70
+ });
71
+ } catch (error) {
72
+ throw new Error(
73
+ `Erro ao obter diff do arquivo ${filename}: ${error instanceof Error ? error.message : 'Erro desconhecido'}`
74
+ );
75
+ }
76
+ }
77
+
78
+ /**
79
+ * Executa um commit com a mensagem fornecida
80
+ */
81
+ export function executeCommit(message: string): GitCommitResult {
82
+ try {
83
+ // Executar commit
84
+ execSync(`git commit -m "${message.replace(/"/g, '\\"')}"`, {
85
+ stdio: 'pipe',
86
+ });
87
+
88
+ // Obter hash do commit
89
+ const hash = execSync('git rev-parse HEAD', {
90
+ encoding: 'utf-8',
91
+ stdio: 'pipe',
92
+ }).trim();
93
+
94
+ return {
95
+ success: true,
96
+ hash,
97
+ message,
98
+ };
99
+ } catch (error) {
100
+ return {
101
+ success: false,
102
+ error:
103
+ error instanceof Error
104
+ ? error.message
105
+ : 'Erro desconhecido ao executar commit',
106
+ };
107
+ }
108
+ }
109
+
110
+ /**
111
+ * Executa commit de arquivo específico
112
+ */
113
+ export function executeFileCommit(
114
+ filename: string,
115
+ message: string
116
+ ): GitCommitResult {
117
+ try {
118
+ // Commit apenas do arquivo específico
119
+ execSync(`git commit "${filename}" -m "${message.replace(/"/g, '\\"')}"`, {
120
+ stdio: 'pipe',
121
+ });
122
+
123
+ // Obter hash do commit
124
+ const hash = execSync('git rev-parse HEAD', {
125
+ encoding: 'utf-8',
126
+ stdio: 'pipe',
127
+ }).trim();
128
+
129
+ return {
130
+ success: true,
131
+ hash,
132
+ message,
133
+ };
134
+ } catch (error) {
135
+ return {
136
+ success: false,
137
+ error:
138
+ error instanceof Error
139
+ ? error.message
140
+ : 'Erro desconhecido ao executar commit do arquivo',
141
+ };
142
+ }
143
+ }
144
+
145
+ /**
146
+ * Obtém estatísticas do diff (linhas adicionadas/removidas)
147
+ */
148
+ export function getDiffStats(): {
149
+ added: number;
150
+ removed: number;
151
+ files: number;
152
+ } {
153
+ try {
154
+ const output = execSync('git diff --cached --numstat', {
155
+ encoding: 'utf-8',
156
+ stdio: 'pipe',
157
+ });
158
+
159
+ const lines = output
160
+ .trim()
161
+ .split('\n')
162
+ .filter((line) => line.length > 0);
163
+ let added = 0;
164
+ let removed = 0;
165
+
166
+ lines.forEach((line) => {
167
+ const [addedStr, removedStr] = line.split('\t');
168
+ if (addedStr && addedStr !== '-') added += parseInt(addedStr) || 0;
169
+ if (removedStr && removedStr !== '-')
170
+ removed += parseInt(removedStr) || 0;
171
+ });
172
+
173
+ return { added, removed, files: lines.length };
174
+ } catch {
175
+ return { added: 0, removed: 0, files: 0 };
176
+ }
177
+ }
@@ -0,0 +1,204 @@
1
+ import {
2
+ text,
3
+ select,
4
+ confirm,
5
+ log,
6
+ note,
7
+ cancel,
8
+ isCancel,
9
+ } from '@clack/prompts';
10
+ import clipboardy from 'clipboardy';
11
+ import type { CommitSuggestion } from '../core/openai.ts';
12
+
13
+ export interface UIAction {
14
+ action: 'commit' | 'edit' | 'copy' | 'cancel';
15
+ message?: string;
16
+ }
17
+
18
+ /**
19
+ * Exibe a mensagem gerada e permite interação do usuário
20
+ */
21
+ export async function showCommitPreview(
22
+ suggestion: CommitSuggestion
23
+ ): Promise<UIAction> {
24
+ // Exibir preview da mensagem
25
+ note(
26
+ `Tipo: ${suggestion.type}\nMensagem: "${suggestion.message}"`,
27
+ '💭 Sugestão de Commit'
28
+ );
29
+
30
+ // Opções disponíveis
31
+ const action = await select({
32
+ message: 'O que você gostaria de fazer?',
33
+ options: [
34
+ {
35
+ value: 'commit',
36
+ label: '✅ Fazer commit com esta mensagem',
37
+ hint: 'Executar git commit imediatamente',
38
+ },
39
+ {
40
+ value: 'edit',
41
+ label: '✏️ Editar mensagem',
42
+ hint: 'Modificar a mensagem antes de commitar',
43
+ },
44
+ {
45
+ value: 'copy',
46
+ label: '📋 Copiar para clipboard',
47
+ hint: 'Copiar mensagem e sair sem commitar',
48
+ },
49
+ {
50
+ value: 'cancel',
51
+ label: '❌ Cancelar',
52
+ hint: 'Sair sem fazer nada',
53
+ },
54
+ ],
55
+ });
56
+
57
+ if (isCancel(action)) {
58
+ return { action: 'cancel' };
59
+ }
60
+
61
+ return { action: action as UIAction['action'] };
62
+ }
63
+
64
+ /**
65
+ * Permite edição da mensagem de commit
66
+ */
67
+ export async function editCommitMessage(
68
+ originalMessage: string
69
+ ): Promise<UIAction> {
70
+ const editedMessage = await text({
71
+ message: 'Edite a mensagem do commit:',
72
+ initialValue: originalMessage,
73
+ placeholder: 'Digite a mensagem do commit...',
74
+ validate: (value) => {
75
+ if (!value || value.trim().length === 0) {
76
+ return 'A mensagem não pode estar vazia';
77
+ }
78
+ if (value.trim().length > 72) {
79
+ return 'A mensagem está muito longa (máximo 72 caracteres recomendado)';
80
+ }
81
+ },
82
+ });
83
+
84
+ if (isCancel(editedMessage)) {
85
+ return { action: 'cancel' };
86
+ }
87
+
88
+ const confirmEdit = await confirm({
89
+ message: `Confirma a mensagem editada: "${editedMessage}"?`,
90
+ });
91
+
92
+ if (isCancel(confirmEdit) || !confirmEdit) {
93
+ return { action: 'cancel' };
94
+ }
95
+
96
+ return {
97
+ action: 'commit',
98
+ message: editedMessage,
99
+ };
100
+ }
101
+
102
+ /**
103
+ * Copia mensagem para clipboard
104
+ */
105
+ export async function copyToClipboard(message: string): Promise<boolean> {
106
+ try {
107
+ await clipboardy.write(message);
108
+ log.success('✅ Mensagem copiada para a área de transferência!');
109
+ return true;
110
+ } catch (error) {
111
+ log.error(
112
+ `❌ Erro ao copiar: ${error instanceof Error ? error.message : 'Erro desconhecido'}`
113
+ );
114
+ return false;
115
+ }
116
+ }
117
+
118
+ /**
119
+ * Confirma execução do commit
120
+ */
121
+ export async function confirmCommit(message: string): Promise<boolean> {
122
+ note(`"${message}"`, '🚀 Confirmar Commit');
123
+
124
+ const confirmed = await confirm({
125
+ message: 'Executar o commit agora?',
126
+ });
127
+
128
+ if (isCancel(confirmed)) {
129
+ return false;
130
+ }
131
+
132
+ return confirmed;
133
+ }
134
+
135
+ /**
136
+ * Exibe resultado do commit
137
+ */
138
+ export function showCommitResult(
139
+ success: boolean,
140
+ hash?: string,
141
+ error?: string
142
+ ) {
143
+ if (success && hash) {
144
+ log.success(`✅ Commit realizado com sucesso!`);
145
+ log.info(`🔗 Hash: ${hash.substring(0, 8)}`);
146
+ } else {
147
+ log.error(`❌ Erro ao realizar commit: ${error || 'Erro desconhecido'}`);
148
+ }
149
+ }
150
+
151
+ /**
152
+ * Interface para modo split (múltiplos commits)
153
+ */
154
+ export async function selectFilesForCommit(files: string[]): Promise<string[]> {
155
+ log.info('📋 Modo Split: Selecione os arquivos para este commit');
156
+
157
+ const selectedFiles: string[] = [];
158
+
159
+ for (const file of files) {
160
+ const include = await confirm({
161
+ message: `Incluir "${file}" neste commit?`,
162
+ });
163
+
164
+ if (isCancel(include)) {
165
+ break;
166
+ }
167
+
168
+ if (include) {
169
+ selectedFiles.push(file);
170
+ }
171
+ }
172
+
173
+ return selectedFiles;
174
+ }
175
+
176
+ /**
177
+ * Confirma se usuário quer continuar com mais commits
178
+ */
179
+ export async function askContinueCommits(
180
+ remainingFiles: string[]
181
+ ): Promise<boolean> {
182
+ if (remainingFiles.length === 0) {
183
+ return false;
184
+ }
185
+
186
+ log.info(`📄 Arquivos restantes: ${remainingFiles.join(', ')}`);
187
+
188
+ const continueCommits = await confirm({
189
+ message: 'Gerar commit para os arquivos restantes?',
190
+ });
191
+
192
+ if (isCancel(continueCommits)) {
193
+ return false;
194
+ }
195
+
196
+ return continueCommits;
197
+ }
198
+
199
+ /**
200
+ * Exibe mensagem de cancelamento
201
+ */
202
+ export function showCancellation() {
203
+ cancel('Operação cancelada pelo usuário');
204
+ }
@@ -0,0 +1,141 @@
1
+ import { select, confirm, log, note, isCancel } from '@clack/prompts';
2
+ import type { FileGroup } from '../core/smart-split.ts';
3
+
4
+ export interface SmartSplitAction {
5
+ action: 'proceed' | 'manual' | 'cancel';
6
+ groups?: FileGroup[];
7
+ }
8
+
9
+ /**
10
+ * Interface para escolher entre smart split e split manual
11
+ */
12
+ export async function chooseSplitMode(): Promise<SmartSplitAction> {
13
+ const mode = await select({
14
+ message: 'Como você gostaria de organizar os commits?',
15
+ options: [
16
+ {
17
+ value: 'smart',
18
+ label: '🧠 Smart Split (Recomendado)',
19
+ hint: 'IA analisa contexto e agrupa automaticamente',
20
+ },
21
+ {
22
+ value: 'manual',
23
+ label: '✋ Split Manual',
24
+ hint: 'Você escolhe arquivos manualmente',
25
+ },
26
+ {
27
+ value: 'cancel',
28
+ label: '❌ Cancelar',
29
+ hint: 'Voltar ao modo normal',
30
+ },
31
+ ],
32
+ });
33
+
34
+ if (isCancel(mode)) {
35
+ return { action: 'cancel' };
36
+ }
37
+
38
+ if (mode === 'manual') {
39
+ return { action: 'manual' };
40
+ }
41
+
42
+ if (mode === 'smart') {
43
+ return { action: 'proceed' };
44
+ }
45
+
46
+ return { action: 'cancel' };
47
+ }
48
+
49
+ /**
50
+ * Exibe os grupos identificados pela IA
51
+ */
52
+ export async function showSmartSplitGroups(
53
+ groups: FileGroup[]
54
+ ): Promise<SmartSplitAction> {
55
+ note(
56
+ `Identificamos ${groups.length} grupo(s) lógico(s) para seus commits:\n\n` +
57
+ groups
58
+ .map(
59
+ (group, index) =>
60
+ `${index + 1}. **${group.name}**\n` +
61
+ ` 📄 ${group.files.join(', ')}\n` +
62
+ ` 💡 ${group.description}\n` +
63
+ ` 🎯 Confiança: ${Math.round(group.confidence * 100)}%`
64
+ )
65
+ .join('\n\n'),
66
+ '🧠 Análise de Contexto'
67
+ );
68
+
69
+ const action = await select({
70
+ message: 'O que você gostaria de fazer?',
71
+ options: [
72
+ {
73
+ value: 'proceed',
74
+ label: '✅ Prosseguir com esta organização',
75
+ hint: 'Usar os grupos como sugeridos pela IA',
76
+ },
77
+ {
78
+ value: 'manual',
79
+ label: '✋ Fazer split manual',
80
+ hint: 'Escolher arquivos manualmente',
81
+ },
82
+ {
83
+ value: 'cancel',
84
+ label: '❌ Cancelar',
85
+ hint: 'Voltar ao modo normal',
86
+ },
87
+ ],
88
+ });
89
+
90
+ if (isCancel(action)) {
91
+ return { action: 'cancel' };
92
+ }
93
+
94
+ if (action === 'proceed') {
95
+ return { action: 'proceed', groups };
96
+ }
97
+
98
+ return { action: action as 'manual' | 'cancel' };
99
+ }
100
+
101
+ /**
102
+ * Interface para confirmar commit de um grupo
103
+ */
104
+ export async function confirmGroupCommit(
105
+ group: FileGroup,
106
+ message: string
107
+ ): Promise<boolean> {
108
+ note(
109
+ `**Grupo:** ${group.name}\n` +
110
+ `**Arquivos:** ${group.files.join(', ')}\n` +
111
+ `**Mensagem:** "${message}"`,
112
+ '🚀 Confirmar Commit do Grupo'
113
+ );
114
+
115
+ const confirmed = await confirm({
116
+ message: `Fazer commit para "${group.name}"?`,
117
+ });
118
+
119
+ if (isCancel(confirmed)) {
120
+ return false;
121
+ }
122
+
123
+ return confirmed;
124
+ }
125
+
126
+ /**
127
+ * Interface para mostrar progresso do smart split
128
+ */
129
+ export function showSmartSplitProgress(
130
+ current: number,
131
+ total: number,
132
+ groupName: string
133
+ ): void {
134
+ const progress = Math.round((current / total) * 100);
135
+ const bar =
136
+ '█'.repeat(Math.floor(progress / 10)) +
137
+ '░'.repeat(10 - Math.floor(progress / 10));
138
+
139
+ log.info(`🔄 Progresso: [${bar}] ${progress}% (${current}/${total})`);
140
+ log.info(`📋 Processando: ${groupName}`);
141
+ }
@@ -0,0 +1,56 @@
1
+ export interface CLIArgs {
2
+ silent: boolean;
3
+ yes: boolean;
4
+ auto: boolean;
5
+ split: boolean;
6
+ smartSplit: boolean;
7
+ dryRun: boolean;
8
+ help: boolean;
9
+ version: boolean;
10
+ }
11
+
12
+ export function parseArgs(args: string[]): CLIArgs {
13
+ return {
14
+ silent: args.includes('--silent') || args.includes('-s'),
15
+ yes: args.includes('--yes') || args.includes('-y'),
16
+ auto: args.includes('--auto') || args.includes('-a'),
17
+ split: args.includes('--split'),
18
+ smartSplit: args.includes('--smart-split'),
19
+ dryRun: args.includes('--dry-run') || args.includes('-n'),
20
+ help: args.includes('--help') || args.includes('-h'),
21
+ version: args.includes('--version') || args.includes('-v'),
22
+ };
23
+ }
24
+
25
+ export function showHelp(): void {
26
+ console.log(`
27
+ 🧙‍♂️ Commit Wizard - Gerador inteligente de mensagens de commit
28
+
29
+ USAGE:
30
+ commit-wizard [OPTIONS]
31
+
32
+ OPTIONS:
33
+ -s, --silent Modo silencioso (sem logs detalhados)
34
+ -y, --yes Confirmar automaticamente sem prompts
35
+ -a, --auto Modo automático (--yes + --silent)
36
+ --split Modo split manual (commits separados por arquivo)
37
+ --smart-split Modo smart split (IA agrupa por contexto)
38
+ -n, --dry-run Visualizar mensagem sem fazer commit
39
+ -h, --help Mostrar esta ajuda
40
+ -v, --version Mostrar versão
41
+
42
+ EXAMPLES:
43
+ commit-wizard # Modo interativo padrão
44
+ commit-wizard --yes # Commit automático
45
+ commit-wizard --split # Split manual por arquivo
46
+ commit-wizard --smart-split # Smart split com IA
47
+ commit-wizard --dry-run # Apenas visualizar mensagem
48
+ commit-wizard --auto # Modo totalmente automático
49
+
50
+ Para mais informações, visite: https://github.com/user/commit-wizard
51
+ `);
52
+ }
53
+
54
+ export function showVersion(): void {
55
+ console.log('commit-wizard v1.0.0');
56
+ }
@@ -0,0 +1,82 @@
1
+ /**
2
+ * Polyfill para stripVTControlCharacters para compatibilidade com Bun/Node.js
3
+ *
4
+ * Este polyfill resolve problemas de compatibilidade onde o Bun não mapeia
5
+ * corretamente a função stripVTControlCharacters do módulo util do Node.js.
6
+ * A função foi adicionada ao Node.js v16.14.0+ mas pode não estar disponível
7
+ * em todos os ambientes ou ter problemas de mapping no Bun.
8
+ */
9
+
10
+ /**
11
+ * Remove caracteres de controle VT de uma string
12
+ * Implementação baseada na função nativa do Node.js
13
+ *
14
+ * @param str - String da qual remover os caracteres de controle
15
+ * @returns String limpa sem caracteres de controle VT/ANSI
16
+ */
17
+ function stripVTControlCharacters(str: string): string {
18
+ if (typeof str !== 'string') {
19
+ throw new TypeError('The "str" argument must be of type string');
20
+ }
21
+
22
+ // Regex para caracteres de controle ANSI/VT
23
+ // Baseada na implementação oficial do Node.js
24
+ const esc = String.fromCharCode(27); // ESC character (\u001B)
25
+ const csi = String.fromCharCode(155); // CSI character (\u009B)
26
+ const ansiRegex = new RegExp(`[${esc}${csi}][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><]`, 'g');
27
+
28
+ return str.replace(ansiRegex, '');
29
+ }
30
+
31
+ // Interceptar require/import do módulo util antes de qualquer outra coisa
32
+
33
+ const Module = require('module');
34
+ const originalRequire = Module.prototype.require;
35
+
36
+ Module.prototype.require = function(id: string) {
37
+ const result = originalRequire.apply(this, arguments);
38
+
39
+ // Se estiver importando o módulo util e stripVTControlCharacters não existe, adicionar
40
+ if (id === 'util' && result && !result.stripVTControlCharacters) {
41
+ result.stripVTControlCharacters = stripVTControlCharacters;
42
+ // Tornar a propriedade não enumerável para não interferir em iterações
43
+ Object.defineProperty(result, 'stripVTControlCharacters', {
44
+ value: stripVTControlCharacters,
45
+ writable: false,
46
+ enumerable: true,
47
+ configurable: false,
48
+ });
49
+ }
50
+
51
+ return result;
52
+ };
53
+
54
+
55
+ declare global {
56
+ // eslint-disable-next-line @typescript-eslint/no-namespace
57
+ namespace NodeJS {
58
+ interface Global {
59
+ stripVTControlCharacters?: typeof stripVTControlCharacters;
60
+ }
61
+ }
62
+ }
63
+
64
+ // Disponibilizar globalmente também como fallback
65
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
66
+ if (typeof globalThis !== 'undefined' && !(globalThis as any).stripVTControlCharacters) {
67
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
68
+ (globalThis as any).stripVTControlCharacters = stripVTControlCharacters;
69
+ }
70
+
71
+ // Tentar aplicar diretamente ao módulo util se possível
72
+ try {
73
+
74
+ const util = require('util');
75
+ if (!util.stripVTControlCharacters) {
76
+ util.stripVTControlCharacters = stripVTControlCharacters;
77
+ }
78
+ } catch {
79
+ // Ignorar se não conseguir aplicar
80
+ }
81
+
82
+ export { stripVTControlCharacters };
@@ -1,44 +0,0 @@
1
- import { Config } from './config.js';
2
- export interface AIResponse {
3
- content: string;
4
- usage?: {
5
- promptTokens: number;
6
- completionTokens: number;
7
- totalTokens: number;
8
- };
9
- }
10
- /**
11
- * Serviço para interação com APIs de IA
12
- */
13
- export declare class AIService {
14
- private config;
15
- constructor(config: Config);
16
- /**
17
- * Gera prompt do sistema baseado no modo e linguagem
18
- */
19
- private getSystemPrompt;
20
- /**
21
- * Gera prompt para mensagem de commit
22
- */
23
- private getCommitPrompt;
24
- /**
25
- * Realiza chamada para a API da OpenAI
26
- */
27
- callOpenAI(prompt: string, mode?: 'commit' | 'summary'): Promise<AIResponse>;
28
- /**
29
- * Limpa a resposta da API removendo conteúdo inválido
30
- */
31
- private cleanApiResponse;
32
- /**
33
- * Valida se a mensagem de commit é válida
34
- */
35
- private validateCommitMessage;
36
- /**
37
- * Gera resumo de um chunk de diff
38
- */
39
- generateSummary(chunk: string): Promise<string>;
40
- /**
41
- * Gera mensagem de commit baseada no diff ou resumo
42
- */
43
- generateCommitMessage(diffOrSummary: string): Promise<AIResponse>;
44
- }