Functions for generating and formatting commit messages according to Conventional Commits specification.
Core function for generating formatted commit messages from user answers and configuration.
/**
* Generate a formatted commit message from answers and configuration
* @param answers - User responses from interactive prompts
* @param options - Configuration options affecting message format
* @param colorize - Whether to apply color formatting for display
* @returns Formatted commit message string
*/
function generateMessage(
answers: Answers,
options: CommitizenGitOptions,
colorize?: boolean
): string;Usage Examples:
import { generateMessage } from "cz-git";
const answers = {
type: 'feat',
scope: 'auth',
subject: 'add OAuth2 integration',
body: 'Implements OAuth2 authentication flow|Supports Google and GitHub providers',
breaking: 'Changes authentication API interface',
footer: 'Closes #123, #456'
};
const options = {
useEmoji: true,
emojiAlign: 'center',
breaklineChar: '|',
formatMessageCB: undefined
};
// Generate the commit message
const message = generateMessage(answers, options);
// Result: "feat(auth): β¨ add OAuth2 integration\n\nImplements OAuth2 authentication flow\nSupports Google and GitHub providers\n\nBREAKING CHANGE: Changes authentication API interface\n\nCloses #123, #456"
// Generate with colorization for preview
const colorizedMessage = generateMessage(answers, options, true);Generate commit messages from predefined aliases for quick commits.
/**
* Generate commit message from a predefined alias
* @param options - Configuration options containing alias definitions
* @param alias - Alias key to use for message generation
* @returns Formatted commit message from alias
*/
function getAliasMessage(
options: CommitizenGitOptions,
alias: string
): string;Usage Examples:
import { getAliasMessage } from "cz-git";
const options = {
alias: {
fd: 'docs: fix typos',
wip: 'feat: work in progress',
bump: 'chore: bump version'
}
};
// Generate message from alias
const message = getAliasMessage(options, 'fd');
// Result: "docs: fix typos"
// Use in command line: cz_alias=fd czInterface representing the subdivided parts of a commit message for advanced formatting.
interface CommitMessageOptions {
/** Commit type (e.g., 'feat', 'fix') */
type: string;
/** Scope of the change */
scope: string;
/** Emoji code if useEmoji is enabled */
emoji: string;
/** Breaking change indicator ('!' or '') */
markBreaking: string;
/** Subject description */
subject: string;
/** Formatted header following Conventional Commits */
defaultHeader: string;
/** Body content with line breaks processed */
body: string;
/** Breaking changes description */
breaking: string;
/** Footer content (issues, references, etc.) */
footer: string;
/** Complete formatted message */
defaultMessage: string;
}Callback function interface for customizing the final message format.
/**
* Custom message formatting callback
* @param messageMod - Object containing all message parts
* @returns Custom formatted commit message
*/
type FormatMessageCallback = (messageMod: CommitMessageOptions) => string;Usage Examples:
import { definePrompt } from "cz-git";
export default definePrompt({
formatMessageCB: ({ defaultMessage, type, scope, subject }) => {
// Custom format with timestamp
const timestamp = new Date().toISOString().slice(0, 10);
return `[${timestamp}] ${type}${scope ? `(${scope})` : ''}: ${subject}`;
}
});
// Alternative: Add prefixes or modify structure
export default definePrompt({
formatMessageCB: ({ defaultHeader, body, footer }) => {
let message = `π ${defaultHeader}`;
if (body) message += `\n\nπ ${body}`;
if (footer) message += `\n\nπ ${footer}`;
return message;
}
});Internal utilities for processing different parts of the commit message.
/**
* Process and transform subject text
* @param text - Raw subject input
* @returns Cleaned subject text with trimmed whitespace
*/
function getProcessSubject(text: string): string;
/**
* Transform subject case according to configuration
* @param options - Configuration containing case preferences
* @param subject - Subject text to transform
* @returns Subject with applied case transformation
*/
function transformSubjectCase(
options: CommitizenGitOptions,
subject: string
): string;
/**
* Calculate maximum allowed subject length based on constraints
* @param type - Commit type
* @param scope - Commit scope
* @param options - Configuration with length limits
* @returns Maximum subject length considering header constraints
*/
function getMaxSubjectLength(
type: Answers['type'],
scope: Answers['scope'] | Answers['customScope'],
options: CommitizenGitOptions
): number;Function for displaying formatted commit message previews during confirmation.
/**
* Display a formatted preview of the commit message
* @param msg - Complete commit message to preview
* @param confirmColorize - Whether to apply color formatting
*/
function previewMessage(msg: string, confirmColorize?: boolean): void;Usage Examples:
import { previewMessage, generateMessage } from "cz-git";
const message = generateMessage(answers, options);
// Show preview with colors
previewMessage(message, true);
// Show plain preview
previewMessage(message, false);The message generation system automatically processes line breaks using the configured breaklineChar (default: |):
| characters to actual line breaks| characters to actual line breaksbreaklineNumber settingExamples:
// Input with line break characters
const answers = {
body: 'First line|Second line|Third line',
breaking: 'API change|Removes deprecated methods'
};
// Generated output includes actual line breaks:
// First line
// Second line
// Third line
//
// BREAKING CHANGE: API change
// Removes deprecated methodsThe message generation follows the Conventional Commits specification exactly, ensuring compatibility with automated tools and consistent formatting across projects.