All files / src commitWizard.ts

51.74% Statements 163/315
100% Branches 14/14
25% Functions 1/4
51.74% Lines 163/315

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 3161x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 24x 24x 24x 24x 24x 24x 24x 24x 24x 5x 5x 24x 24x 2x 24x 3x 3x 24x 24x 3x 3x 24x 24x 24x 1x 1x 1x 1x 1x                                                                                                                                                                                                                                                                                                                  
import * as vscode from 'vscode';
import type { CommitConfig } from './configPanel';
 
export interface CommitParts {
  type: string;
  scope?: string;
  breaking: boolean;
  description: string;
  body?: string;
  footer?: string;
}
 
interface CommitType extends vscode.QuickPickItem {
  label: string;
  description: string;
}
 
const DEFAULT_TYPES_PT: CommitType[] = [
  { label: 'feat',     description: '✨  Nova funcionalidade' },
  { label: 'fix',      description: '🐛  Correção de bug' },
  { label: 'docs',     description: '📝  Apenas documentação' },
  { label: 'style',    description: '💄  Formatação, sem mudança de lógica' },
  { label: 'refactor', description: '♻️   Refatoração sem nova feature ou fix' },
  { label: 'perf',     description: '⚡  Melhoria de performance' },
  { label: 'test',     description: '✅  Adição ou correção de testes' },
  { label: 'build',    description: '🏗️   Build system ou dependências externas' },
  { label: 'ci',       description: '👷  Configuração de CI/CD' },
  { label: 'chore',    description: '🔧  Outras tarefas sem impacto no código' },
  { label: 'revert',   description: '⏪  Reverte um commit anterior' },
];
 
const DEFAULT_TYPES_EN: CommitType[] = [
  { label: 'feat',     description: '✨  New feature' },
  { label: 'fix',      description: '🐛  Bug fix' },
  { label: 'docs',     description: '📝  Documentation only' },
  { label: 'style',    description: '💄  Formatting, no logic change' },
  { label: 'refactor', description: '♻️   Refactoring, no new feature or fix' },
  { label: 'perf',     description: '⚡  Performance improvement' },
  { label: 'test',     description: '✅  Adding or fixing tests' },
  { label: 'build',    description: '🏗️   Build system or external dependencies' },
  { label: 'ci',       description: '👷  CI/CD configuration' },
  { label: 'chore',    description: '🔧  Other tasks with no code impact' },
  { label: 'revert',   description: '⏪  Reverts a previous commit' },
];
 
interface Strings {
  typeLabel: string;
  typePlaceholder: string;
  scopeLabel: string;
  scopePrompt: string;
  scopePlaceholder: string;
  noScope: string;
  noScopeDetail: string;
  breakingLabel: string;
  breakingPlaceholder: string;
  notBreaking: string;
  notBreakingDetail: string;
  isBreaking: string;
  isBreakingDetail: string;
  descLabel: string;
  descPrompt: string;
  descPlaceholder: string;
  descRequired: string;
  descTooLong: (len: number, max: number) => string;
  descUppercase: string;
  descPeriod: string;
  bodyLabel: string;
  bodyPrompt: string;
  bodyPlaceholder: string;
  breakingFooterPrompt: string;
  breakingFooterPlaceholder: string;
  breakingFooterValidation: string;
}
 
const PT: Strings = {
  typeLabel: 'Tipo',
  typePlaceholder: 'Selecione o tipo do commit',
  scopeLabel: 'Escopo',
  scopePrompt: 'Escopo do commit (opcional)',
  scopePlaceholder: 'ex: auth, api, ui — pressione Enter para pular',
  noScope: '$(dash)  Sem escopo',
  noScopeDetail: 'Não especificar escopo',
  breakingLabel: 'Breaking Change',
  breakingPlaceholder: 'Esta mudança quebra a compatibilidade com versões anteriores?',
  notBreaking: '$(check)   Não',
  notBreakingDetail: 'Mudança compatível com a versão anterior',
  isBreaking: '$(warning) Sim',
  isBreakingDetail: 'BREAKING CHANGE — remove ou altera comportamento existente',
  descLabel: 'Descrição',
  descPrompt: 'Descrição curta e objetiva do commit (obrigatório)',
  descPlaceholder: 'ex: adiciona autenticação por OAuth',
  descRequired: 'A descrição é obrigatória',
  descTooLong: (l, m) => `Muito longo: ${l}/${m} caracteres`,
  descUppercase: 'Use letras minúsculas no início da descrição',
  descPeriod: 'Não termine a descrição com ponto final',
  bodyLabel: 'Corpo',
  bodyPrompt: 'Corpo da mensagem (opcional) — explique o quê e por quê',
  bodyPlaceholder: 'Pressione Enter para pular',
  breakingFooterPrompt: 'Descreva o que mudou e como migrar',
  breakingFooterPlaceholder: 'ex: remove o endpoint /api/v1/users, use /api/v2/users',
  breakingFooterValidation: 'O footer deve começar com "BREAKING CHANGE:"',
};
 
const EN: Strings = {
  typeLabel: 'Type',
  typePlaceholder: 'Select commit type',
  scopeLabel: 'Scope',
  scopePrompt: 'Commit scope (optional)',
  scopePlaceholder: 'e.g.: auth, api, ui — press Enter to skip',
  noScope: '$(dash)  No scope',
  noScopeDetail: 'Do not specify scope',
  breakingLabel: 'Breaking Change',
  breakingPlaceholder: 'Does this change break backward compatibility?',
  notBreaking: '$(check)   No',
  notBreakingDetail: 'Backward compatible change',
  isBreaking: '$(warning) Yes',
  isBreakingDetail: 'BREAKING CHANGE — removes or alters existing behavior',
  descLabel: 'Description',
  descPrompt: 'Short, objective commit description (required)',
  descPlaceholder: 'e.g.: add OAuth authentication',
  descRequired: 'Description is required',
  descTooLong: (l, m) => `Too long: ${l}/${m} characters`,
  descUppercase: 'Start description with a lowercase letter',
  descPeriod: 'Do not end description with a period',
  bodyLabel: 'Body',
  bodyPrompt: 'Message body (optional) — explain what and why',
  bodyPlaceholder: 'Press Enter to skip',
  breakingFooterPrompt: 'Describe what changed and how to migrate',
  breakingFooterPlaceholder: 'e.g.: removes /api/v1/users endpoint, use /api/v2/users',
  breakingFooterValidation: 'Footer must start with "BREAKING CHANGE:"',
};
 
/** Monta a mensagem final de commit a partir das partes */
export function buildCommitMessage(
  parts: CommitParts,
  config?: CommitConfig,
  branchName?: string
): string {
  const scope = parts.scope ? `(${parts.scope})` : '';
  const bang = parts.breaking ? '!' : '';
  let message = `${parts.type}${scope}${bang}: ${parts.description}`;
 
  if (parts.body?.trim()) {
    message += `\n\n${parts.body.trim()}`;
  }
 
  if (parts.footer?.trim()) {
    message += `\n\n${parts.footer.trim()}`;
  } else if (parts.breaking) {
    message += `\n\nBREAKING CHANGE: ${parts.description}`;
  }
 
  if (config?.includeBranch && branchName) {
    message += `\n\nBranch: ${branchName}`;
  }
 
  return message;
}
 
/**
 * Executa o wizard multi-step para criar a mensagem de commit,
 * respeitando as regras definidas no ConfigPanel.
 */
export async function runCommitWizard(
  customTypes: string[],
  customScopes: string[],
  commitConfig: CommitConfig,
  branchName?: string
): Promise<string | undefined> {
  const t: Strings = commitConfig.language === 'en' ? EN : PT;

  // ── Modo simples (sem Conventional Commit) ────────────────────────────────
  if (!commitConfig.conventionalCommit) {
    const description = await vscode.window.showInputBox({
      title: `My Commit  (1 / 1) — ${t.descLabel}`,
      prompt: t.descPrompt,
      placeHolder: t.descPlaceholder,
      ignoreFocusOut: true,
      validateInput(value) {
        if (!value.trim()) { return t.descRequired; }
        if (value.trim().length > commitConfig.maxLength) {
          return t.descTooLong(value.trim().length, commitConfig.maxLength);
        }
        return null;
      },
    });
    if (description === undefined) { return undefined; }

    let message = description.trim();
    if (commitConfig.includeBranch && branchName) {
      message += `\n\nBranch: ${branchName}`;
    }
    return message;
  }

  // ── Modo Conventional Commit ──────────────────────────────────────────────
  const hasScope = commitConfig.conventionalPattern === 'with-scope';
  const totalSteps = hasScope ? 5 : 4;
  let step = 1;

  // Passo: Tipo
  const defaultTypes = commitConfig.language === 'en' ? DEFAULT_TYPES_EN : DEFAULT_TYPES_PT;
  const typeItems: CommitType[] =
    customTypes.length > 0
      ? customTypes.map(label => ({ label, description: '' }))
      : defaultTypes;

  const typeItem = await vscode.window.showQuickPick(typeItems, {
    title: `My Commit  (${step++} / ${totalSteps}) — ${t.typeLabel}`,
    placeHolder: t.typePlaceholder,
    ignoreFocusOut: true,
    matchOnDescription: true,
  });
  if (!typeItem) { return undefined; }

  // Passo: Escopo (apenas se o padrão inclui scope)
  let scope: string | undefined;
  if (hasScope) {
    if (customScopes.length > 0) {
      const NO_SCOPE = t.noScope;
      const scopeItems = [
        { label: NO_SCOPE, description: t.noScopeDetail },
        ...customScopes.map(s => ({ label: s, description: '' })),
      ];
      const scopeItem = await vscode.window.showQuickPick(scopeItems, {
        title: `My Commit  (${step++} / ${totalSteps}) — ${t.scopeLabel}`,
        placeHolder: t.scopePlaceholder,
        ignoreFocusOut: true,
      });
      if (scopeItem === undefined) { return undefined; }
      if (scopeItem.label !== NO_SCOPE) { scope = scopeItem.label; }
    } else {
      const scopeInput = await vscode.window.showInputBox({
        title: `My Commit  (${step++} / ${totalSteps}) — ${t.scopeLabel}`,
        prompt: t.scopePrompt,
        placeHolder: t.scopePlaceholder,
        ignoreFocusOut: true,
      });
      if (scopeInput === undefined) { return undefined; }
      scope = scopeInput.trim() || undefined;
    }
  }

  // Passo: Breaking change
  const breakingOptions = [
    { label: t.notBreaking, detail: t.notBreakingDetail, isBreaking: false },
    { label: t.isBreaking,  detail: t.isBreakingDetail,  isBreaking: true  },
  ];
  const breakingItem = await vscode.window.showQuickPick(breakingOptions, {
    title: `My Commit  (${step++} / ${totalSteps}) — ${t.breakingLabel}`,
    placeHolder: t.breakingPlaceholder,
    ignoreFocusOut: true,
    matchOnDetail: true,
  });
  if (breakingItem === undefined) { return undefined; }
  const breaking = breakingItem.isBreaking;

  // Passo: Descrição curta
  const description = await vscode.window.showInputBox({
    title: `My Commit  (${step++} / ${totalSteps}) — ${t.descLabel}`,
    prompt: t.descPrompt,
    placeHolder: t.descPlaceholder,
    ignoreFocusOut: true,
    validateInput(value) {
      if (!value.trim()) { return t.descRequired; }
      if (value.trim().length > commitConfig.maxLength) {
        return t.descTooLong(value.trim().length, commitConfig.maxLength);
      }
      if (/^[A-Z]/.test(value.trim())) { return t.descUppercase; }
      if (/\.$/.test(value.trim())) { return t.descPeriod; }
      return null;
    },
  });
  if (description === undefined) { return undefined; }

  // Passo: Corpo (opcional)
  const body = await vscode.window.showInputBox({
    title: `My Commit  (${step++} / ${totalSteps}) — ${t.bodyLabel}`,
    prompt: t.bodyPrompt,
    placeHolder: t.bodyPlaceholder,
    ignoreFocusOut: true,
  });
  if (body === undefined) { return undefined; }

  // Passo extra: Footer para breaking changes
  let footer: string | undefined;
  if (breaking) {
    const footerInput = await vscode.window.showInputBox({
      title: 'My Commit — BREAKING CHANGE',
      prompt: t.breakingFooterPrompt,
      placeHolder: t.breakingFooterPlaceholder,
      value: 'BREAKING CHANGE: ',
      ignoreFocusOut: true,
      validateInput(value) {
        if (!value.startsWith('BREAKING CHANGE:')) { return t.breakingFooterValidation; }
        return null;
      },
    });
    if (footerInput === undefined) { return undefined; }
    footer = footerInput.trim() || undefined;
  }

  return buildCommitMessage(
    {
      type: typeItem.label,
      scope,
      breaking,
      description: description.trim(),
      body: body.trim() || undefined,
      footer,
    },
    commitConfig,
    branchName
  );
}