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
14/*
15 * The types and scopes accepted by both Commitlint and Commitizen are defined by the changelog
16 * configuration file - `changelog.yaml` - as they decide which section of the changelog commits
17 * with a given type and scope are placed in.
18 */
19
20let changelog;
21
22try {
23    const contents = fs.readFileSync("changelog.yaml", "utf8");
24
25    changelog = yaml.load(contents);
26} catch (err) {
27    console.log(err);
28
29    throw err;
30}
31
32/*
33 * The next couple of functions are just used to transform the changelog YAML configuration
34 * structure into one accepted by the Conventional Changelog adapter (conventional-changelog-tf-a).
35 */
36
37function getTypes(sections) {
38    return sections.map(section => {
39        return {
40            "type": section.type,
41            "section": section.hidden ? undefined : section.title,
42            "hidden": section.hidden || false,
43        };
44    })
45}
46
47function getSections(subsections) {
48    return subsections.flatMap(subsection => {
49        const scope = subsection.scope ? [ subsection.scope ] : [];
50
51        return {
52            "title": subsection.title,
53            "sections": getSections(subsection.subsections || []),
54            "scopes": scope.concat(subsection.deprecated || []),
55        };
56    })
57};
58
59const types = getTypes(changelog.sections);
60const sections = getSections(changelog.subsections);
61
62module.exports = {
63    "header": "# Change Log & Release Notes\n\nThis document contains a summary of the new features, changes, fixes and known\nissues in each release of Trusted Firmware-A.\n",
64    "preset": {
65        "name": "tf-a",
66        "commitUrlFormat": "https://review.trustedfirmware.org/plugins/gitiles/TF-A/trusted-firmware-a/+/{{hash}}",
67        "compareUrlFormat": "https://review.trustedfirmware.org/plugins/gitiles/TF-A/trusted-firmware-a/+/refs/tags/{{previousTag}}..refs/tags/{{currentTag}}",
68        "userUrlFormat": "https://github.com/{{user}}",
69
70        "types": types,
71        "sections": sections,
72    },
73    "infile": "docs/change-log.md",
74    "skip": {
75        "commit": true,
76        "tag": true
77    },
78    "bumpFiles": [
79        {
80            "filename": "package.json",
81            "type": "json"
82        },
83        {
84            "filename": "package-lock.json",
85            "type": "json"
86        },
87        {
88            "filename": "tools/conventional-changelog-tf-a/package.json",
89            "type": "json"
90        },
91        {
92            "filename": "Makefile",
93            "updater": {
94                "readVersion": function (contents) {
95                    const major = contents.match(/^VERSION_MAJOR\s*:=\s*(\d+?)$/m)[1];
96                    const minor = contents.match(/^VERSION_MINOR\s*:=\s*(\d+?)$/m)[1];
97
98                    return `${major}.${minor}.0`;
99                },
100
101                "writeVersion": function (contents, version) {
102                    const major = version.split(".")[0];
103                    const minor = version.split(".")[1];
104
105                    contents = contents.replace(/^(VERSION_MAJOR\s*:=\s*)(\d+?)$/m, `$1${major}`);
106                    contents = contents.replace(/^(VERSION_MINOR\s*:=\s*)(\d+?)$/m, `$1${minor}`);
107
108                    return contents;
109                }
110            }
111        }
112    ]
113};
114