Skip to content

commitlint() ​

Lint commit messages from Node.js with the relizy config - a range of commits, a message file, or a single message.

ts
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:

OptionDescription
messageLint this message
editLint the message of the commit being created: a file path, or true for .git/COMMIT_EDITMSG
from, toThe range to lint. Default: from the merge-base of the pull/merge request in CI, else from the last tag, to HEAD
titleIn the CI of a pull/merge request, also lint its title. Default: commitlint.title
previewAdd the changelog entry of each commit to the report
format'text' (default) or 'json'
quietIn the text format, only print the commits with problems, and the summary
installWrite the commit-msg hook, force to replace an existing one
configName, config, logLevelSee 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.

ts
import { commitlint } from 'relizy'

const { errorCount } = await commitlint({ from: 'origin/main', quiet: true })

if (errorCount > 0) {
  process.exitCode = 1
}

lintCommitMessage() ​

ts
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), and ignored for a commit that is not linted (merge, fixup, release...);
  • problems: each with its rule, severity ('error' or 'warn'), message, and its fix when known;
  • suggestion: a fixed header, when some errors can be fixed safely;
  • commit: the parsed message (type, scope, subject, isBreaking, the bump it triggers, the packages it touches);
  • changelogTitle: the changelog section of the commit, unset when it does not appear in the changelog.

See LintCommitMessageOptions and CommitlintResult.

ts
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)
}

See Also ​