All files / src configManager.ts

17.28% Statements 61/353
63.63% Branches 7/11
83.33% Functions 5/6
17.28% Lines 61/353

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 3551x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 8x 8x 2x 2x 6x 6x 6x 6x 6x 8x 1x 1x 1x 1x 1x 4x 4x 4x 4x                                                         4x 1x 1x 1x 1x 1x 2x 2x 2x 2x 2x 2x                                                                                                             2x 1x 1x 1x 1x 1x 2x 2x 2x 2x                       2x 1x 1x 1x 1x 1x                                                                                                                                                                                                                                                                                                                                                                                                               1x    
import * as vscode from 'vscode';
import * as fs from 'fs';
import * as path from 'path';
import type { CommitConfig } from './configPanel';
 
export class ConfigManager {
  private static readonly CONFIG_FILE_NAME = 'settings.json';
  private static readonly CONFIG_KEY = '[git-commit]';
 
  /**
   * Obtém o caminho do arquivo .vscode/settings.json
   */
  private static getConfigFilePath(): string | null {
    const workspaceFolders = vscode.workspace.workspaceFolders;
    if (!workspaceFolders || workspaceFolders.length === 0) {
      return null;
    }
    return path.join(
      workspaceFolders[0].uri.fsPath,
      '.vscode',
      this.CONFIG_FILE_NAME
    );
  }
 
  /**
   * Carrega as configurações do arquivo .vscode/settings.json
   */
  static loadConfig(): CommitConfig | null {
    const filePath = this.getConfigFilePath();
    if (!filePath) {
      return null;
    }

    try {
      if (!fs.existsSync(filePath)) {
        return null;
      }

      const fileContent = fs.readFileSync(filePath, 'utf-8');
      const settings = JSON.parse(fileContent);
      const gitCommitConfig = settings[this.CONFIG_KEY];

      if (gitCommitConfig) {
        // Mapeia os campos de [git-commit] para CommitConfig
        const config: CommitConfig = {
          language: gitCommitConfig['editor.language'] || 'pt',
          conventionalCommit: gitCommitConfig['editor.conventionalCommit'] !== false,
          conventionalPattern: gitCommitConfig['editor.conventionalPattern'] || 'with-scope',
          maxLength: Array.isArray(gitCommitConfig['editor.rulers']) 
            ? gitCommitConfig['editor.rulers'][0] 
            : 72,
          includeBranch: gitCommitConfig['editor.includeBranch'] || false
        };
        return config;
      }

      return null;
    } catch (error) {
      return null;
    }
  }
 
  /**
   * Salva as configurações no arquivo .vscode/settings.json
   */
  static async saveConfig(config: CommitConfig): Promise<boolean> {
    const filePath = this.getConfigFilePath();
    if (!filePath) {
      const msg = 'Nenhuma pasta de workspace encontrada para salvar configurações.';
      vscode.window.showErrorMessage(msg);
      return false;
    }

    try {
      // Garante que a pasta .vscode existe
      const vscodeDir = path.dirname(filePath);
      if (!fs.existsSync(vscodeDir)) {
        fs.mkdirSync(vscodeDir, { recursive: true });
      }

      // Lê o arquivo existente ou cria um novo
      let settings: Record<string, unknown> = {};
      if (fs.existsSync(filePath)) {
        const fileContent = fs.readFileSync(filePath, 'utf-8');
        settings = JSON.parse(fileContent);
      }

      // Atualiza apenas [git-commit]
      if (!settings['[git-commit]']) {
        settings['[git-commit]'] = {};
      }
      const gitCommitConfig = settings['[git-commit]'] as Record<string, unknown>;
      gitCommitConfig['editor.language'] = config.language;
      gitCommitConfig['editor.rulers'] = [config.maxLength];
      gitCommitConfig['editor.conventionalCommit'] = config.conventionalCommit;
      gitCommitConfig['editor.conventionalPattern'] = config.conventionalPattern;
      gitCommitConfig['editor.includeBranch'] = config.includeBranch;

      // Garante que a configuração do Copilot está presente
      settings['github.copilot.chat.useEditorContext'] = true;

      // Escreve de volta ao arquivo com formatação legível
      const jsonContent = JSON.stringify(settings, null, 2);
      fs.writeFileSync(filePath, jsonContent, 'utf-8');

      // Gera o arquivo copilot-instructions.md
      await this.generateCopilotInstructions(config);
      
      // Oferece a opção de recarregar a janela para o Copilot reconhecer as novas instruções
      vscode.window.showInformationMessage(
        'Configurações salvas! Recarregue a janela para que o Copilot reconheça as novas instruções.',
        'Recarregar Agora'
      ).then((selection) => {
        if (selection === 'Recarregar Agora') {
          vscode.commands.executeCommand('workbench.action.reloadWindow');
        }
      });

      return true;
    } catch (error) {
      const errorMsg = error instanceof Error ? error.message : String(error);
      vscode.window.showErrorMessage(
        `Erro ao salvar configurações: ${errorMsg}`
      );
      return false;
    }
  }
 
  /**
   * Remove as configurações do arquivo .vscode/settings.json
   */
  static async clearConfig(): Promise<boolean> {
    const filePath = this.getConfigFilePath();
    if (!filePath || !fs.existsSync(filePath)) {
      return false;
    }

    try {
      const fileContent = fs.readFileSync(filePath, 'utf-8');
      const settings = JSON.parse(fileContent);
      delete settings[this.CONFIG_KEY];

      fs.writeFileSync(filePath, JSON.stringify(settings, null, 2), 'utf-8');
      return true;
    } catch (error) {
      return false;
    }
  }
 
  /**
   * Gera o arquivo .github/copilot-instructions.md com instruções de commit
   */
  private static async generateCopilotInstructions(config: CommitConfig): Promise<void> {
    const workspaceFolders = vscode.workspace.workspaceFolders;
    if (!workspaceFolders || workspaceFolders.length === 0) {
      return;
    }

    const githubDir = path.join(workspaceFolders[0].uri.fsPath, '.github');
    const instructionsFile = path.join(githubDir, 'copilot-instructions.md');

    try {
      // Garante que a pasta .github existe
      if (!fs.existsSync(githubDir)) {
        fs.mkdirSync(githubDir, { recursive: true });
      }

      const isPortuguese = config.language === 'pt';
      const withScope = config.conventionalPattern === 'with-scope';

      // Template das instruções
      const instructionsContent = isPortuguese ? `# Instruções para Copilot - Geração de Mensagens de Commit

## Padrão Obrigatório: Conventional Commits

Todas as mensagens de commit devem seguir o padrão **Conventional Commits**${withScope ? ' com escopo' : ' sem escopo'}.

### Formato
\`\`\`
${withScope ? 'tipo(escopo): descrição' : 'tipo: descrição'}
\`\`\`

### Tipos Permitidos
- **feat**: Nova funcionalidade
- **fix**: Correção de bug
- **docs**: Alterações na documentação
- **style**: Formatação, semicolons, pontos e vírgulas (sem alteração de código)
- **refactor**: Refatoração de código sem alteração de funcionalidade
- **perf**: Melhoria de performance
- **test**: Adição ou atualização de testes
- **chore**: Atualizações de dependências, configurações, scripts de build
- **ci**: Alterações em CI/CD
${withScope ? `
### Escopo (Obrigatório)
Indique o contexto/módulo afetado:
- \`auth\`, \`config\`, \`git\`, \`validator\`, \`ui\`, \`wizard\`, etc.
` : ''}
### Descrição
- Sempre em **português brasileiro**
- Comece com verbo no infinitivo ou imperativo
- Máximo **${config.maxLength} caracteres** na primeira linha
- Sem período final
- Conciso e descritivo

### Exemplos Corretos
${withScope
  ? `- \`feat(validator): adicionar validação de escopo de commits\`
- \`fix(git): corrigir erro ao gerar mensagem de commit\`
- \`refactor(wizard): simplificar lógica do assistente interativo\`
- \`docs(readme): atualizar instruções de instalação\`
- \`chore(deps): atualizar dependências do projeto\`
- \`test(validator): adicionar testes unitários para validação\``
  : `- \`feat: adicionar validação de mensagens de commit\`
- \`fix: corrigir erro ao gerar mensagem de commit\`
- \`refactor: simplificar lógica do assistente interativo\`
- \`docs: atualizar instruções de instalação\`
- \`chore: atualizar dependências do projeto\`
- \`test: adicionar testes unitários para validação\``}

### Exemplos Incorretos ❌
${withScope
  ? `- \`update stuff\` (vago, não segue padrão)
- \`feat: adicionado novo recurso\` (falta escopo)
- \`fix(git): Corrigir erro ao gerar mensagens de commit com mais de ${config.maxLength} caracteres.\` (muito longo, tem ponto)`
  : `- \`update stuff\` (vago, não segue padrão)
- \`feat(auth): adicionado novo recurso\` (não deve ter escopo)
- \`fix: Corrigir erro ao gerar mensagens de commit com mais de ${config.maxLength} caracteres.\` (muito longo, tem ponto)`}

## Contexto do Projeto

Este é um **extension do VS Code** que padroniza mensagens de commit usando Conventional Commits com um wizard interativo no Source Control.

### Módulos Principais
- **extension**: Arquivo principal de entrada
- **commitWizard**: Assistente interativo para criação de commits
- **configManager**: Gerenciamento de configurações
- **configPanel**: Painel de configuração
- **validator**: Validação de mensagens de commit
- **git**: Integração com Git

## Regras Especiais

${withScope
  ? `1. Ao descrever mudanças de **configuração**, use o escopo \`config\`
2. Ao descrever mudanças de **validação**, use o escopo \`validator\`
3. Ao descrever mudanças da **UI/UX**, use o escopo \`ui\` ou \`wizard\`
4. Ao descrever alterações de **integração Git**, use o escopo \`git\``
  : `1. Não inclua escopo nas mensagens de commit
2. Descreva a mudança de forma clara e objetiva no tipo e descrição`}

## Instruções para IA

- Gere mensagens **claras e objetivas**
- Respeite o limite de **${config.maxLength} caracteres** na primeira linha
- Use **sempre português brasileiro**
- Escolha o **tipo mais apropriado** para o contexto das mudanças${withScope ? '\n- Escolha o **escopo mais apropriado** para o contexto das mudanças' : '\n- **Não inclua escopo** nas mensagens de commit'}
- Se houver múltiplas mudanças em escopos diferentes, gere commits **separados**
` : `# Copilot Instructions - Commit Message Generation

## Mandatory Pattern: Conventional Commits

All commit messages must follow the **Conventional Commits** pattern${withScope ? ' with scope' : ' without scope'}.

### Format
\`\`\`
${withScope ? 'type(scope): description' : 'type: description'}
\`\`\`

### Allowed Types
- **feat**: New functionality
- **fix**: Bug fix
- **docs**: Documentation changes
- **style**: Formatting, semicolons, commas (no code changes)
- **refactor**: Code refactoring without functionality changes
- **perf**: Performance improvement
- **test**: Test addition or update
- **chore**: Dependency updates, configurations, build scripts
- **ci**: CI/CD changes
${withScope ? `
### Scope (Required)
Indicate the affected context/module:
- \`auth\`, \`config\`, \`git\`, \`validator\`, \`ui\`, \`wizard\`, etc.
` : ''}
### Description
- Always in **English**
- Start with verb in infinitive or imperative
- Maximum **${config.maxLength} characters** on the first line
- No final period
- Concise and descriptive

### Correct Examples
${withScope
  ? `- \`feat(validator): add commit scope validation\`
- \`fix(git): fix error when generating commit message\`
- \`refactor(wizard): simplify assistant logic\`
- \`docs(readme): update installation instructions\`
- \`chore(deps): update project dependencies\`
- \`test(validator): add unit tests for validation\``
  : `- \`feat: add commit message validation\`
- \`fix: fix error when generating commit message\`
- \`refactor: simplify assistant logic\`
- \`docs: update installation instructions\`
- \`chore: update project dependencies\`
- \`test: add unit tests for validation\``}

### Incorrect Examples ❌
${withScope
  ? `- \`update stuff\` (vague, does not follow pattern)
- \`feat: added new feature\` (missing scope)
- \`fix(git): Fix error when generating commit messages with more than ${config.maxLength} characters.\` (too long, has period)`
  : `- \`update stuff\` (vague, does not follow pattern)
- \`feat(auth): added new feature\` (must not have scope)
- \`fix: Fix error when generating commit messages with more than ${config.maxLength} characters.\` (too long, has period)`}

## Project Context

This is a **VS Code extension** that standardizes commit messages using Conventional Commits with an interactive wizard in Source Control.

### Main Modules
- **extension**: Main entry point
- **commitWizard**: Interactive wizard for creating commits
- **configManager**: Configuration management
- **configPanel**: Configuration panel
- **validator**: Commit message validation
- **git**: Git integration

## Special Rules

${withScope
  ? `1. When describing **configuration** changes, use the \`config\` scope
2. When describing **validation** changes, use the \`validator\` scope
3. When describing **UI/UX** changes, use the \`ui\` or \`wizard\` scope
4. When describing **Git integration** changes, use the \`git\` scope`
  : `1. Do not include scope in commit messages
2. Describe the change clearly using only the type and description`}

## Instructions for AI

- Generate **clear and objective** messages
- Respect the **${config.maxLength} character** limit on the first line
- Use the appropriate language consistently
- Choose the **most appropriate type** for the context of changes${withScope ? '\n- Choose the **most appropriate scope** for the context of changes' : '\n- **Do not include scope** in commit messages'}
- If there are multiple changes in different scopes, generate **separate commits**
`;

      // Escreve o arquivo
      fs.writeFileSync(instructionsFile, instructionsContent, 'utf-8');
    } catch (error) {
      console.error('[ConfigManager] Erro ao gerar copilot-instructions.md:', error);
      // Não lança erro para não interromper o fluxo de salvamento
    }
  }
}