UNPKG

@dev-build-deploy/commit-it

Version:
200 lines (199 loc) 7.32 kB
"use strict"; /* * SPDX-FileCopyrightText: 2023 Kevin de Jong <monkaii@hotmail.com> * SPDX-License-Identifier: MIT */ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.ConventionalCommit = void 0; const diagnose_it_1 = require("@dev-build-deploy/diagnose-it"); const commit_1 = require("./commit"); const requirements = __importStar(require("./requirements")); /** * Conventional Commit * @class ConventionalCommit * @member type Conventional Commit type * @member scope Conventional Commit scope * @member breaking Commit message has a Conventional Commit breaking change (!) * @member description Conventional Commit description * @member hash Commit hash * @member subject Commit subject * @member body Commit body * @member footer Commit footer * @member author Commit author and date * @member committer Commit committer and date * @member isValid Whether the Conventional Commit is valid * @member errors List of error messages * @member warnings List of warning messages */ class ConventionalCommit { _raw; _errors = []; _warnings = []; constructor(raw, options) { this._raw = raw; this.validate(options); } /** * Creates a new Conventional Commit object from the provided Commit. * @param commit Commit to convert to a Conventional Commit * @param options Options to use when validating the commit message * @returns Conventional Commit */ static fromCommit(commit, options) { // Convert the Commit message to Raw Conventional Commit data const rawConvCommit = createRawConventionalCommit(commit); // Create a new Conventional Commit object return new ConventionalCommit(rawConvCommit, options); } /** * Creates a new Conventional Commit object from the provided string. * @param props Hash, message and author/committer information * @param options Options to use when validating the commit message * @returns Conventional Commit */ static fromString(props, options) { return ConventionalCommit.fromCommit(commit_1.Commit.fromString(props), options); } /** * Creates a new Conventional Commit object from the provided hash. * @param props Hash and root path * @param options Options to use when validating the commit message * @returns Conventional Commit */ static fromHash(props, options) { return ConventionalCommit.fromCommit(commit_1.Commit.fromHash(props), options); } // Contributors get author() { return this._raw.commit.author; } get committer() { return this._raw.commit.committer; } // Commit get hash() { return this._raw.commit.hash; } get subject() { return this._raw.commit.subject; } get body() { return this._raw.commit.body; } get footer() { return this._raw.commit.footer; } // Conventional Commit get type() { return this._raw.type.value?.trimEnd(); } get scope() { // Removes the parenthesis from the scope if (this._raw.scope.value !== undefined) { return this._raw.scope.value.trimEnd().replace(/(\(|\))/g, ""); } return this._raw.scope.value; } get description() { return this._raw.description.value; } get breaking() { return (this._raw.breaking.value?.trimEnd() === "!" || (this.footer !== undefined && ("BREAKING CHANGE" in this.footer || "BREAKING-CHANGE" in this.footer))); } // Attributes get isFixupCommit() { return this._raw.commit.isFixupCommit; } get isMergeCommit() { return this._raw.commit.isMergeCommit; } // Raw get raw() { return this._raw.commit.raw; } // Validation get isValid() { return this._errors.length === 0; } get warnings() { return this._warnings; } get errors() { return this._errors; } toJSON() { return { hash: this.hash, author: this.author, committer: this.committer, subject: this.subject, body: this.body, footer: this.footer, type: this.type, breaking: this.breaking, description: this.description, validation: { isValid: this.isValid, errors: this.errors, warnings: this.warnings, }, attributes: { isFixup: this.isFixupCommit, isMerge: this.isMergeCommit, }, }; } // Private validation function validate(options) { let results = []; requirements.commitRules.forEach(rule => (results = [...results, ...rule.validate(this._raw, options)])); this._errors = results.filter(r => r.level === diagnose_it_1.DiagnosticsLevelEnum.Error); this._warnings = results.filter(r => r.level === diagnose_it_1.DiagnosticsLevelEnum.Warning); } } exports.ConventionalCommit = ConventionalCommit; function createRawConventionalCommit(commit) { const ConventionalCommitRegex = new RegExp(/^(?<type>[^(!:]*)(?<scope>\([^)]*\)\s*)?(?<breaking>!\s*)?(?<separator>:\s*)?(?<subject>.*)?$/); const match = ConventionalCommitRegex.exec(commit.subject.split(/\r?\n/)[0]); const conventionalCommit = { commit: commit, type: { index: 1, value: match?.groups?.type }, scope: { index: 1, value: match?.groups?.scope }, breaking: { index: 1, value: match?.groups?.breaking }, seperator: { index: 1, value: match?.groups?.separator }, description: { index: 1, value: match?.groups?.subject }, body: { index: 1, value: commit.body }, }; function intializeIndices(commit) { commit.scope.index = commit.type.index + (commit.type.value?.length ?? 0); commit.breaking.index = commit.scope.index + (commit.scope.value?.length ?? 0); commit.seperator.index = commit.breaking.index + (commit.breaking.value?.length ?? 0); commit.description.index = commit.seperator.index + (commit.seperator.value?.length ?? 0); return commit; } return intializeIndices(conventionalCommit); }