@wroud/conventional-commits-parser
Version:
A lightweight parser for conventional commits that supports extracting commit metadata, generating commit messages, and managing commit trailers in TypeScript.
37 lines • 1.21 kB
JavaScript
const headerRegex = /^(?<type>[\w-]+)(\((?<scope>[\w-]+)\))?(?<breaking>!)?:\s(?<description>[\S\s]*)/;
export function parseConventionalCommit(commitInfo) {
const match = headerRegex.exec(commitInfo.subject);
if (!match) {
return null;
}
const breakingChanges = [];
const { type, breaking, scope, description } = match.groups;
const rawBody = commitInfo.body?.trim() || "";
if (breaking) {
if (rawBody) {
breakingChanges.push(rawBody);
}
else {
breakingChanges.push(description);
}
}
for (const trailer of commitInfo.trailers) {
const value = trailer.value.trim();
if (trailer.token === "BREAKING CHANGE" ||
trailer.token === "BREAKING-CHANGE") {
breakingChanges.push(value);
continue;
}
}
const body = rawBody || undefined;
const uniqueBreakingChanges = Array.from(new Set(breakingChanges));
return {
commitInfo,
type: type.toLowerCase(),
scope: scope || undefined,
description,
body,
breakingChanges: uniqueBreakingChanges,
};
}
//# sourceMappingURL=parseConventionalCommit.js.map