commitlint()
Lint commit messages from Node.js with the relizy config - a range of commits, a message file, or a single message.
function commitlint(options?: CommitlintOptions): Promise<CommitlintRunResult>commitlint() runs relizy commitlint: it lints the commits of a range, the message of the commit being created (edit) or a given message, prints the report to the standard output, and returns it. It does not exit: errorCount tells whether the messages pass. With install, it writes the commit-msg hook instead of linting.
Options
The flags of relizy commitlint, camelCased:
| Option | Description |
|---|---|
message | Lint this message |
edit | Lint the message of the commit being created: a file path, or true for .git/COMMIT_EDITMSG |
from, to | The range to lint. Default: from the merge-base of the pull/merge request in CI, else from the last tag, to HEAD |
title | In the CI of a pull/merge request, also lint its title. Default: commitlint.title |
preview | Add the changelog entry of each commit to the report |
format | 'text' (default) or 'json' |
quiet | In the text format, only print the commits with problems, and the summary |
install | Write the commit-msg hook, force to replace an existing one |
configName, config, logLevel | See the shared options |
It returns results (one CommitlintResult per linted message), errorCount, warningCount, the report (unset with install) and the installed hook (with install). See CommitlintOptions and CommitlintRunResult.
import { commitlint } from 'relizy'
const { errorCount } = await commitlint({ from: 'origin/main', quiet: true })
if (errorCount > 0) {
process.exitCode = 1
}lintCommitMessage()
function lintCommitMessage(message: string, options: LintCommitMessageOptions): Promise<CommitlintResult>Lints a single message with the rules of the config, without output. options.config is required (from loadRelizyConfig()); files (the changed files, relative to the root) decides the packages of the commit and its allowed scopes, suggest: false skips the suggested header.
The result has:
valid: no error (warnings allowed), andignoredfor a commit that is not linted (merge, fixup, release...);problems: each with itsrule,severity('error'or'warn'),message, and itsfixwhen known;suggestion: a fixedheader, when some errors can be fixed safely;commit: the parsed message (type,scope,subject,isBreaking, thebumpit triggers, thepackagesit touches);changelogTitle: the changelog section of the commit, unset when it does not appear in the changelog.
See LintCommitMessageOptions and CommitlintResult.
import { lintCommitMessage, loadRelizyConfig } from 'relizy'
const config = await loadRelizyConfig()
const result = await lintCommitMessage('feat(core): add the parser', {
config,
files: ['packages/core/src/parser.ts'],
})
if (!result.valid) {
console.log(result.problems.map(problem => problem.message), result.suggestion?.header)
}