Chris Kay | 285c130 | 2021-11-12 15:48:44 +0000 | [diff] [blame] | 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 | |
Chris Kay | 82117d7 | 2021-12-01 16:34:55 +0000 | [diff] [blame] | 11 | const fs = require("fs"); |
| 12 | const yaml = require("js-yaml"); |
| 13 | |
Chris Kay | 285c130 | 2021-11-12 15:48:44 +0000 | [diff] [blame] | 14 | const { "trailer-exists": trailerExists } = require("@commitlint/rules").default; |
| 15 | |
Chris Kay | 025c87f | 2021-11-09 20:05:38 +0000 | [diff] [blame] | 16 | /* |
Chris Kay | 82117d7 | 2021-12-01 16:34:55 +0000 | [diff] [blame] | 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. |
Chris Kay | 025c87f | 2021-11-09 20:05:38 +0000 | [diff] [blame] | 20 | */ |
Chris Kay | 82117d7 | 2021-12-01 16:34:55 +0000 | [diff] [blame] | 21 | |
| 22 | let changelog; |
Chris Kay | 025c87f | 2021-11-09 20:05:38 +0000 | [diff] [blame] | 23 | |
Chris Kay | 82117d7 | 2021-12-01 16:34:55 +0000 | [diff] [blame] | 24 | try { |
| 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 | |
| 34 | function getTypes(sections) { |
| 35 | return sections.map(section => section.type) |
| 36 | } |
| 37 | |
| 38 | function getScopes(subsections) { |
| 39 | return subsections.flatMap(subsection => { |
| 40 | const scope = subsection.scope ? [ subsection.scope ] : []; |
| 41 | const subscopes = getScopes(subsection.subsections || []); |
Chris Kay | 025c87f | 2021-11-09 20:05:38 +0000 | [diff] [blame] | 42 | |
| 43 | return scope.concat(subscopes); |
| 44 | }) |
| 45 | }; |
| 46 | |
Chris Kay | 82117d7 | 2021-12-01 16:34:55 +0000 | [diff] [blame] | 47 | const types = getTypes(changelog.sections).sort(); /* Sort alphabetically */ |
| 48 | const scopes = getScopes(changelog.subsections).sort(); /* Sort alphabetically */ |
Chris Kay | 025c87f | 2021-11-09 20:05:38 +0000 | [diff] [blame] | 49 | |
Chris Kay | 285c130 | 2021-11-12 15:48:44 +0000 | [diff] [blame] | 50 | module.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: { |
Chris Kay | 82117d7 | 2021-12-01 16:34:55 +0000 | [diff] [blame] | 61 | "header-max-length": [1, "always", 50], /* Warning */ |
| 62 | "body-max-line-length": [1, "always", 72], /* Warning */ |
Chris Kay | 285c130 | 2021-11-12 15:48:44 +0000 | [diff] [blame] | 63 | |
| 64 | "change-id-exists": [1, "always", "Change-Id:"], /* Warning */ |
| 65 | "signed-off-by-exists": [1, "always", "Signed-off-by:"], /* Warning */ |
Chris Kay | 025c87f | 2021-11-09 20:05:38 +0000 | [diff] [blame] | 66 | |
Chris Kay | 82117d7 | 2021-12-01 16:34:55 +0000 | [diff] [blame] | 67 | "type-case": [2, "always", "lower-case" ], /* Error */ |
| 68 | "type-enum": [2, "always", types], /* Error */ |
| 69 | |
Yann Gautier | 54b08f9 | 2021-11-19 17:57:50 +0100 | [diff] [blame] | 70 | "scope-case": [2, "always", "lower-case"], /* Error */ |
Chris Kay | 025c87f | 2021-11-09 20:05:38 +0000 | [diff] [blame] | 71 | "scope-enum": [1, "always", scopes] /* Warning */ |
Chris Kay | 285c130 | 2021-11-12 15:48:44 +0000 | [diff] [blame] | 72 | }, |
| 73 | }; |