UNPKG

@elsikora/commitizen-plugin-commitlint-ai

Version:
82 lines (78 loc) 3.61 kB
'use strict'; var commitMessage_entity = require('../../domain/entity/commit-message.entity.js'); var commitBody_valueObject = require('../../domain/value-object/commit-body.value-object.js'); var commitHeader_valueObject = require('../../domain/value-object/commit-header.value-object.js'); /** * Use case for manual commit message creation */ class ManualCommitUseCase { CLI_INTERFACE; constructor(cliInterface) { this.CLI_INTERFACE = cliInterface; } /** * Execute the manual commit creation process * @param {ILlmPromptContext} context - The context for the commit * @returns {Promise<CommitMessage>} Promise resolving to the commit message */ async execute(context) { // Build type options from context const typeOptions = []; if (context.typeDescriptions) { for (const [type, desc] of Object.entries(context.typeDescriptions)) { const emoji = desc.emoji ?? ""; const cleanDesc = desc.description.replace(/\.$/, ""); const emojiSuffix = emoji ? ` ${emoji}` : ""; typeOptions.push({ label: `${type}: ${cleanDesc}${emojiSuffix}`, value: type, }); } } else if (context.typeEnum) { for (const type of context.typeEnum) { typeOptions.push({ label: type, value: type }); } } const type = await this.CLI_INTERFACE.select("Select commit type:", typeOptions); // Get scope if applicable const scope = await this.CLI_INTERFACE.text("Enter scope (optional):", "", ""); // Get subject const subject = await this.CLI_INTERFACE.text("Enter commit subject:", "", "", (value) => { if (!value.trim()) { return "Subject is required"; } if (context.subject.minLength && value.length < context.subject.minLength) { return `Subject must be at least ${context.subject.minLength} characters`; } if (context.subject.maxLength && value.length > context.subject.maxLength) { return `Subject must be at most ${context.subject.maxLength} characters`; } // eslint-disable-next-line @elsikora/sonar/no-redundant-jump return; }); // Get body const body = await this.CLI_INTERFACE.text("Enter commit body (optional):", "", ""); // Get breaking change const hasBreakingChange = await this.CLI_INTERFACE.confirm("Is this a breaking change?", false); let breakingChange; if (hasBreakingChange) { breakingChange = await this.CLI_INTERFACE.text("Describe the breaking change:", "", ""); } // Create commit message const header = new commitHeader_valueObject.CommitHeader(type, subject, scope); const commitBody = new commitBody_valueObject.CommitBody(body, breakingChange); const commitMessage = new commitMessage_entity.CommitMessage(header, commitBody); // Ask for confirmation this.CLI_INTERFACE.log("\nCommit message preview:"); this.CLI_INTERFACE.log(commitMessage.toString()); const isConfirmed = await this.CLI_INTERFACE.confirm("\nUse this commit message?", true); if (!isConfirmed) { // Recursively call to edit return this.execute(context); } return commitMessage; } } exports.ManualCommitUseCase = ManualCommitUseCase; //# sourceMappingURL=manual-commit.use-case.js.map