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