|
| 1 | +import type { UserConfig } from "@commitlint/types"; |
| 2 | +import { RuleConfigSeverity } from "@commitlint/types"; |
| 3 | + |
| 4 | +const types = [ |
| 5 | + "feat", // new user-facing feature (must have (or may introduce a new) scope) |
| 6 | + "enha", // enhancement of any kind (must have a scope) |
| 7 | + "fix", // bug fix (must have a scope) |
| 8 | + "docs", // documentation enhancement (same as 'enha(docs)') |
| 9 | + "style", // code style enhancement (same as 'enha(style)') |
| 10 | + |
| 11 | + "refactor", // refactoring code enhancement (must have a scope) |
| 12 | + |
| 13 | + "perf", // performance improvement enhancement (must have a scope) |
| 14 | + "test", // testing enhancement (must have a scope) |
| 15 | + "build", // build enhancement |
| 16 | + "ci", // ci enhancement |
| 17 | + "chore", // Relating to repository maintenance |
| 18 | + "revert", // reverting a previous commit |
| 19 | + "merge", // merging branches |
| 20 | +]; |
| 21 | + |
| 22 | +const typesRequiringScope = ["feat", "enha", "fix", "refactor", "perf", "test"]; |
| 23 | + |
| 24 | +/** |
| 25 | + * Scopes are used to describe the area of the project that the commit affects. |
| 26 | + * They are used to group commits together and to provide context to the commit |
| 27 | + * message. |
| 28 | + * |
| 29 | + * The scopes are divided into two categories: areas and topics. Areas are |
| 30 | + * broader categories that describe the area of the project that the commit |
| 31 | + * affects, while topics are more specific categories that describe the |
| 32 | + * functionality or purpose of the commit. |
| 33 | + * |
| 34 | + * For example, commits that add a new feature to the frontend user interface |
| 35 | + * might look like this: |
| 36 | + * |
| 37 | + * - `feat(ui,auth): add forgot password functionality ...` |
| 38 | + * - `feat(ui,projects): Implement project url sharing ...` |
| 39 | + * |
| 40 | + * Another example might be a commit that adds a new feature to the backend API related to |
| 41 | + * user authentication: |
| 42 | + * |
| 43 | + * `feat(api,auth): use new endpoint for user authentication ...` |
| 44 | + * |
| 45 | + */ |
| 46 | +const scopes = { |
| 47 | + areas: [ |
| 48 | + /* Frontend */ |
| 49 | + "ui", // UI related changes |
| 50 | + |
| 51 | + "api", // REST API related changes |
| 52 | + "supabase", // Supabase related changes |
| 53 | + |
| 54 | + /* Others */ |
| 55 | + "core", // core changes neither specific to frontend nor backend |
| 56 | + "docs", // documentation changes |
| 57 | + "style", // code style changes |
| 58 | + "webserver", // webserver and proxying changes |
| 59 | + "version", // version bumping |
| 60 | + ], |
| 61 | + topics: [ |
| 62 | + "auth", |
| 63 | + "blog", |
| 64 | + "analytics", |
| 65 | + "automation", |
| 66 | + "faq", |
| 67 | + "projects", |
| 68 | + "roles", |
| 69 | + "settings", |
| 70 | + "inbox", |
| 71 | + ], |
| 72 | +}; |
| 73 | + |
| 74 | +const Configuration: UserConfig = { |
| 75 | + extends: ["@commitlint/config-conventional"], |
| 76 | + rules: { |
| 77 | + "type-case": [RuleConfigSeverity.Error, "always", "lower-case"], |
| 78 | + "type-empty": [RuleConfigSeverity.Error, "never"], |
| 79 | + "type-enum": [RuleConfigSeverity.Error, "always", types], |
| 80 | + "subject-case": [RuleConfigSeverity.Disabled, "always", "lower-case"], |
| 81 | + "subject-empty": [RuleConfigSeverity.Error, "never"], |
| 82 | + "subject-outer-whitespace": [RuleConfigSeverity.Error, "always"], |
| 83 | + "scope-rich": [RuleConfigSeverity.Error, "always"], |
| 84 | + "body-max-line-length": [RuleConfigSeverity.Warning, "always", 150], |
| 85 | + "header-max-length": [RuleConfigSeverity.Warning, "always", 100], |
| 86 | + }, |
| 87 | + plugins: [ |
| 88 | + { |
| 89 | + rules: { |
| 90 | + "scope-rich": (commit, when) => { |
| 91 | + const errorMessages: string[] = [ |
| 92 | + "scope is required for commit of type" + |
| 93 | + `[${typesRequiringScope.join(", ")}]`, |
| 94 | + `scope must be one of the areas [${scopes.areas.join(", ")}], ` + |
| 95 | + "optionally paired with one of the topics " + |
| 96 | + `[${scopes.topics.join(", ")}], ` + |
| 97 | + "all separated by a comma (no spaces)", |
| 98 | + ]; |
| 99 | + |
| 100 | + const scope = commit.scope ?? ""; |
| 101 | + const scopeRequired = typesRequiringScope.includes(commit.type!); |
| 102 | + |
| 103 | + if (when !== "always" || !scopeRequired) return [true, undefined]; |
| 104 | + |
| 105 | + if (scopeRequired && !scope.length) { |
| 106 | + return [false, errorMessages[0]]; |
| 107 | + } |
| 108 | + |
| 109 | + if (scope.includes(",")) { |
| 110 | + // area must be provided and valid, and if topics are provided, |
| 111 | + // they must be valid |
| 112 | + const [area, ...topics] = scope.split(","); |
| 113 | + if ( |
| 114 | + !scopes.areas.includes(area) || |
| 115 | + topics.some((topic) => !scopes.topics.includes(topic)) |
| 116 | + ) { |
| 117 | + return [false, errorMessages[1]]; |
| 118 | + } |
| 119 | + } else if (!scopes.areas.includes(scope)) { |
| 120 | + return [false, errorMessages[1]]; |
| 121 | + } |
| 122 | + |
| 123 | + return [true, undefined]; |
| 124 | + }, |
| 125 | + "subject-outer-whitespace": (commit, when) => { |
| 126 | + const errorMessages: string[] = [ |
| 127 | + "subject must not have more than one preceding whitespace character", |
| 128 | + ]; |
| 129 | + |
| 130 | + if (when !== "always" || !commit.subject) return [true, undefined]; |
| 131 | + |
| 132 | + if ([" ", "\t", "\n", "\r"].includes(commit.subject[0])) { |
| 133 | + return [false, errorMessages[0]]; |
| 134 | + } |
| 135 | + |
| 136 | + return [true, undefined]; |
| 137 | + }, |
| 138 | + }, |
| 139 | + }, |
| 140 | + ], |
| 141 | +}; |
| 142 | + |
| 143 | +export default Configuration; |
0 commit comments