@commitlint/format is a TypeScript library that formats commitlint reports into human-readable output with colored text and symbols. It processes lint results containing errors and warnings from commit message validation, applying appropriate visual formatting including colored signs, bold text for emphasis, and structured summary information.
npm install @commitlint/formatimport format from "@commitlint/format";
import { format, formatResult } from "@commitlint/format";For CommonJS:
const format = require("@commitlint/format");
const { format, formatResult } = require("@commitlint/format");import format from "@commitlint/format";
// Format a complete report
const report = {
results: [
{
errors: [
{
level: 2,
name: "type-empty",
message: "type may not be empty"
}
],
warnings: [
{
level: 1,
name: "subject-case",
message: "subject must be in sentence-case"
}
],
input: "fix: resolve issue with validation"
}
]
};
const formattedOutput = format(report, {
color: true,
verbose: true,
helpUrl: "https://commitlint.js.org/"
});
console.log(formattedOutput);
// Output: colored formatted text with symbols and summary@commitlint/format follows a simple two-stage formatting pipeline:
The library uses chalk for terminal colors and provides sensible defaults while allowing full customization of signs, colors, and verbosity levels.
Formats complete commitlint reports into human-readable strings with colors and symbols.
/**
* Format a commitlint report into a human-readable string
* @param report - The commitlint report containing results
* @param options - Formatting options for colors, symbols, and verbosity
* @returns Formatted string output
*/
function format(
report?: FormattableReport,
options?: FormatOptions
): string;Usage Example:
import format from "@commitlint/format";
const output = format({
results: [
{
errors: [{ level: 2, name: "type-empty", message: "type may not be empty" }],
input: "add new feature"
}
]
}, {
color: true,
verbose: true,
helpUrl: "https://commitlint.js.org/"
});Formats a single result into an array of formatted strings for custom processing.
/**
* Format a single result into an array of formatted strings
* @param result - Individual result with errors and warnings
* @param options - Formatting options for colors, symbols, and verbosity
* @returns Array of formatted string lines
*/
function formatResult(
result?: FormattableResult,
options?: FormatOptions
): string[];Usage Example:
import { formatResult } from "@commitlint/format";
const lines = formatResult({
errors: [{ level: 2, name: "type-empty", message: "type may not be empty" }],
warnings: [{ level: 1, name: "subject-case", message: "subject must be lowercase" }]
}, {
color: false,
signs: ["INFO", "WARN", "ERROR"]
});
// lines contains formatted text lines without colorsinterface FormattableReport {
results?: (FormattableResult & WithInput)[];
}
interface FormattableResult {
errors?: FormattableProblem[];
warnings?: FormattableProblem[];
}
interface FormattableProblem {
level: RuleConfigSeverity;
name: keyof QualifiedRules;
message: string;
}
// QualifiedRules represents the commitlint rule configuration
type QualifiedRules = Partial<RulesConfig<RuleConfigQuality.Qualified>>;
interface WithInput {
input?: string;
}interface FormatOptions {
/** Enable/disable colored output (default: true) */
color?: boolean;
/** Custom signs for different severity levels (default: [" ", "⚠", "✖"]) */
signs?: readonly [string, string, string];
/** Custom colors for different severity levels (default: ["white", "yellow", "red"]) */
colors?: readonly [ChalkColor, ChalkColor, ChalkColor];
/** Show verbose output including successful results (default: false) */
verbose?: boolean;
/** Help URL to display when problems are found */
helpUrl?: string;
}
type ChalkColor = ColorName | ModifierName;enum RuleConfigSeverity {
Disabled = 0,
Warning = 1,
Error = 2
}
// 0 = disabled, 1 = warning, 2 = errorThe library uses these default settings:
[" ", "⚠", "✖"] for disabled/warning/error levels["white", "yellow", "red"] for disabled/warning/error levelstrue (colored output enabled)false (only show problems, not successes)The functions are designed to handle missing or malformed data gracefully:
The formatted output includes:
Example output:
⧗ input: add new feature
✖ type may not be empty [type-empty]
⚠ subject must be lowercase [subject-case]
✖ found 1 problems, 1 warnings
ⓘ Get help: https://commitlint.js.org/