|
| 1 | +const emojis = { |
| 2 | + build: "🏗️", |
| 3 | + chore: "🔧", |
| 4 | + ci: "👷", |
| 5 | + docs: "📚", |
| 6 | + feat: "✨", |
| 7 | + fix: "🐛", |
| 8 | + perf: "🏎", |
| 9 | + refactor: "♻️", |
| 10 | + revert: "⏪️", |
| 11 | + style: "🎨", |
| 12 | + test: "🧪", |
| 13 | + wip: "🚧", |
| 14 | +}; |
| 15 | + |
| 16 | +function getLastCharacter(subject) { |
| 17 | + if (subject == undefined) { |
| 18 | + return false; |
| 19 | + } |
| 20 | + |
| 21 | + return [...subject].pop(); |
| 22 | +} |
| 23 | + |
| 24 | +module.exports = { |
| 25 | + extends: "@commitlint/config-conventional", |
| 26 | + plugins: [ |
| 27 | + { |
| 28 | + rules: { |
| 29 | + "header-ends-with-emoji": (parsed) => { |
| 30 | + // Get subject from parsed message |
| 31 | + const { subject } = parsed; |
| 32 | + |
| 33 | + // Get last character from subject |
| 34 | + let lastCharacter = getLastCharacter(subject); |
| 35 | + |
| 36 | + // Check if last character is an UTF-8 emoji |
| 37 | + if (!subject || !/\p{Emoji}/u.test(lastCharacter)) { |
| 38 | + return [false, "subject must end with a emoji"]; |
| 39 | + } |
| 40 | + |
| 41 | + return [true, ""]; |
| 42 | + }, |
| 43 | + |
| 44 | + "header-ends-with-allowed-emoji": (parsed) => { |
| 45 | + // Get subject from parsed message |
| 46 | + const { subject } = parsed; |
| 47 | + |
| 48 | + // Get last character from subject |
| 49 | + let lastCharacter = getLastCharacter(subject); |
| 50 | + |
| 51 | + // Check if last character matches one of the known emojis |
| 52 | + if (!subject || !Object.values(emojis).includes(lastCharacter)) { |
| 53 | + return [ |
| 54 | + false, |
| 55 | + `subject must end with one emoji of ${Object.keys(emojis) |
| 56 | + .map((emojiType) => `${emojis[emojiType]}`) |
| 57 | + .join(", ")}`, |
| 58 | + ]; |
| 59 | + } |
| 60 | + |
| 61 | + return [true, ""]; |
| 62 | + }, |
| 63 | + |
| 64 | + "header-ends-with-matching-emoji": (parsed) => { |
| 65 | + // Get type and subject from parsed message |
| 66 | + const { type, subject } = parsed; |
| 67 | + |
| 68 | + // Get last character from subject |
| 69 | + let lastCharacter = getLastCharacter(subject); |
| 70 | + |
| 71 | + // Check if last character matches the emoji of the type |
| 72 | + if (!subject || emojis[type] != lastCharacter) { |
| 73 | + // Check if type is defined |
| 74 | + if (type == undefined || type == null) { |
| 75 | + return [false, `type must not be empty to find matching emoji`]; |
| 76 | + } |
| 77 | + |
| 78 | + // Check if type is one of the known types |
| 79 | + if (emojis[type] == undefined) { |
| 80 | + return [false, `type must not be ${type}: check configuration of type-enum rule`]; |
| 81 | + } |
| 82 | + |
| 83 | + return [false, `subject must end with ${emojis[type]} for commit type ${type}`]; |
| 84 | + } |
| 85 | + |
| 86 | + return [true, ""]; |
| 87 | + }, |
| 88 | + }, |
| 89 | + }, |
| 90 | + ], |
| 91 | + rules: { |
| 92 | + "header-ends-with-emoji": [2, "always"], |
| 93 | + "header-ends-with-allowed-emoji": [2, "always"], |
| 94 | + "header-ends-with-matching-emoji": [2, "always"], |
| 95 | + "type-enum": [ |
| 96 | + 2, |
| 97 | + "always", |
| 98 | + ["build", "chore", "ci", "docs", "feat", "fix", "perf", "refactor", "revert", "style", "test", "wip"], |
| 99 | + ], |
| 100 | + }, |
| 101 | +}; |
0 commit comments