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