1*91f16700Schasinglulu/* 2*91f16700Schasinglulu * Copyright (c) 2021, Arm Limited. All rights reserved. 3*91f16700Schasinglulu * 4*91f16700Schasinglulu * SPDX-License-Identifier: BSD-3-Clause 5*91f16700Schasinglulu */ 6*91f16700Schasinglulu 7*91f16700Schasinglulu/* eslint-env es6 */ 8*91f16700Schasinglulu 9*91f16700Schasinglulu"use strict"; 10*91f16700Schasinglulu 11*91f16700Schasingluluconst fs = require("fs"); 12*91f16700Schasingluluconst yaml = require("js-yaml"); 13*91f16700Schasinglulu 14*91f16700Schasingluluconst { "trailer-exists": trailerExists } = require("@commitlint/rules").default; 15*91f16700Schasinglulu 16*91f16700Schasinglulu/* 17*91f16700Schasinglulu * The types and scopes accepted by both Commitlint and Commitizen are defined by the changelog 18*91f16700Schasinglulu * configuration file - `changelog.yaml` - as they decide which section of the changelog commits 19*91f16700Schasinglulu * with a given type and scope are placed in. 20*91f16700Schasinglulu */ 21*91f16700Schasinglulu 22*91f16700Schasinglululet changelog; 23*91f16700Schasinglulu 24*91f16700Schasinglulutry { 25*91f16700Schasinglulu const contents = fs.readFileSync("changelog.yaml", "utf8"); 26*91f16700Schasinglulu 27*91f16700Schasinglulu changelog = yaml.load(contents); 28*91f16700Schasinglulu} catch (err) { 29*91f16700Schasinglulu console.log(err); 30*91f16700Schasinglulu 31*91f16700Schasinglulu throw err; 32*91f16700Schasinglulu} 33*91f16700Schasinglulu 34*91f16700Schasinglulufunction getTypes(sections) { 35*91f16700Schasinglulu return sections.map(section => section.type) 36*91f16700Schasinglulu} 37*91f16700Schasinglulu 38*91f16700Schasinglulufunction getScopes(subsections) { 39*91f16700Schasinglulu return subsections.flatMap(subsection => { 40*91f16700Schasinglulu const scope = subsection.scope ? [ subsection.scope ] : []; 41*91f16700Schasinglulu const subscopes = getScopes(subsection.subsections || []); 42*91f16700Schasinglulu 43*91f16700Schasinglulu return scope.concat(subscopes); 44*91f16700Schasinglulu }) 45*91f16700Schasinglulu}; 46*91f16700Schasinglulu 47*91f16700Schasingluluconst types = getTypes(changelog.sections).sort(); /* Sort alphabetically */ 48*91f16700Schasingluluconst scopes = getScopes(changelog.subsections).sort(); /* Sort alphabetically */ 49*91f16700Schasinglulu 50*91f16700Schasinglulumodule.exports = { 51*91f16700Schasinglulu extends: ["@commitlint/config-conventional"], 52*91f16700Schasinglulu plugins: [ 53*91f16700Schasinglulu { 54*91f16700Schasinglulu rules: { 55*91f16700Schasinglulu "signed-off-by-exists": trailerExists, 56*91f16700Schasinglulu "change-id-exists": trailerExists, 57*91f16700Schasinglulu }, 58*91f16700Schasinglulu }, 59*91f16700Schasinglulu ], 60*91f16700Schasinglulu rules: { 61*91f16700Schasinglulu "header-max-length": [1, "always", 50], /* Warning */ 62*91f16700Schasinglulu "body-max-line-length": [1, "always", 72], /* Warning */ 63*91f16700Schasinglulu 64*91f16700Schasinglulu "change-id-exists": [1, "always", "Change-Id:"], /* Warning */ 65*91f16700Schasinglulu "signed-off-by-exists": [1, "always", "Signed-off-by:"], /* Warning */ 66*91f16700Schasinglulu 67*91f16700Schasinglulu "type-case": [2, "always", "lower-case" ], /* Error */ 68*91f16700Schasinglulu "type-enum": [2, "always", types], /* Error */ 69*91f16700Schasinglulu 70*91f16700Schasinglulu "scope-case": [2, "always", "lower-case"], /* Error */ 71*91f16700Schasinglulu "scope-enum": [1, "always", scopes] /* Warning */ 72*91f16700Schasinglulu }, 73*91f16700Schasinglulu}; 74