diff --git a/@commitlint/cz-commitlint/README.md b/@commitlint/cz-commitlint/README.md index fa92c1fe73..7ef9479f20 100644 --- a/@commitlint/cz-commitlint/README.md +++ b/@commitlint/cz-commitlint/README.md @@ -39,14 +39,105 @@ In package.json ```bash # Install commitlint cli and conventional config -npm install --save-dev @commitlint/config-conventional @commitlint/cli +npm install --save-dev @commitlint/config-conventional @commitlint/cli commitlint-config-gitmoji # or yarn -yarn add @commitlint/config-conventional @commitlint/cli -D +yarn add @commitlint/config-conventional @commitlint/cli commitlint-config-gitmoji -D # Simple: config with conventional -echo "module.exports = {extends: ['@commitlint/config-conventional']};" > commitlint.config.js +import { UserConfig } from "@commitlint/types"; +import gitmoji from 'commitlint-config-gitmoji' + +/** + * @type {import('@commitlint/types').UserConfig} + */ for .js file +module.exports = { + extends: ['@commitlint/config-conventional'], + prompt: { + questions: { + type: { + description: "Select the type of change that you're committing", + enum: { + feat: { + description: "A new feature", + title: "Features", + emoji: "✨", + }, + fix: { + description: "A bug fix", + title: "Bug Fixes", + emoji: "🐛", + }, + docs: { + description: "Documentation only changes", + title: "Documentation", + emoji: "📚", + }, + style: { + description: + "Changes that do not affect the meaning of the code (linters)", + title: "Styles", + emoji: "🎨", + }, + refactor: { + description: + "A code change that neither fixes a bug nor adds a feature", + title: "Code Refactoring", + emoji: "📦", + }, + perf: { + description: "A code change that improves performance", + title: "Performance Improvements", + emoji: "🚀", + }, + test: { + description: "Adding missing tests or correcting existing tests", + title: "Tests", + emoji: "🚨", + }, + build: { + description: + "Changes that affect the build system or external dependencies (yarn)", + title: "Builds", + emoji: "🏗️ ", + }, + ci: { + description: + "Changes to our CI configuration files and scripts (GitActions)", + title: "Continuous Integrations", + emoji: "⚙️ ", + }, + chore: { + description: "Other changes that don't modify src or test files", + title: "Chores", + emoji: "♻️ ", + }, + revert: { + description: "Reverts a previous commit", + title: "Reverts", + emoji: "⏪", + }, + }, + }, + }, + }, + parserPreset: { + parserOpts: gitmoji.parserPreset.parserOpts, + plugins: [gitmoji.parserPreset.plugins], + }, + ...gitmoji.rules, + ...gitmoji.plugins +} as UserConfig ``` +```json +.czrc + +{ + "path": "@commitlint/cz-commitlint", + "useGitmojis": true +} + +``` ### Try it out ```bash diff --git a/@commitlint/cz-commitlint/src/SectionHeader.ts b/@commitlint/cz-commitlint/src/SectionHeader.ts index bf43f0a2a3..948d22149d 100644 --- a/@commitlint/cz-commitlint/src/SectionHeader.ts +++ b/@commitlint/cz-commitlint/src/SectionHeader.ts @@ -27,14 +27,21 @@ export class HeaderQuestion extends Question { } export function combineCommitMessage(answers: Answers): string { - const { type = "", scope = "", subject = "" } = answers; - const prefix = `${type}${scope ? `(${scope})` : ""}`; + const { type = "", scope = "", subject = "" } = answers; - if (subject) { - return ((prefix ? prefix + ": " : "") + subject).trim(); - } else { - return prefix.trim(); - } + const questions = getQuestions(); + + const emoji = questions + .find(item => item.type === 'list' && item.name === 'type')?.choices + ?.find(choice => choice.value === type)?.emoji; + + const prefix = `${emoji?.trim()} ${type}${scope ? `(${scope})` : ""}`; + if (subject) { + return ((prefix ? prefix + ": " : "") + subject).trim(); + } + else { + return prefix.trim(); + } } export function getQuestions(): Array { @@ -81,3 +88,21 @@ export function getQuestionConfig( return questionConfig; } + +export function getEmojis() { + const headerRuleQuestionConfig = getRuleQuestionConfig("header"); + + if (!headerRuleQuestionConfig) { + return []; + } + + const emojis = headerRuleQuestionConfig?.enumList?.map(item => { + if (typeof item === 'object') { + return { value: item.value, emoji: item.emoji || '' }; + } + + return { value: item, emoji: null }; + }) + + return emojis || []; +} diff --git a/@commitlint/cz-commitlint/src/services/getRuleQuestionConfig.ts b/@commitlint/cz-commitlint/src/services/getRuleQuestionConfig.ts index 7cc85fe7fc..7ace6b337b 100644 --- a/@commitlint/cz-commitlint/src/services/getRuleQuestionConfig.ts +++ b/@commitlint/cz-commitlint/src/services/getRuleQuestionConfig.ts @@ -43,19 +43,24 @@ export default function (rulePrefix: RuleField): QuestionConfig | null { ); // TODO emoji + title enumList = enumRuleList - .sort((a, b) => enumNames.indexOf(a) - enumNames.indexOf(b)) - .map((enumName) => { - const enumDescription = enumDescriptions[enumName]?.description; - if (enumDescription) { - return { - name: `${enumName}:`.padEnd(longest + 4) + enumDescription, - value: enumName, - short: enumName, - }; - } else { - return enumName; - } - }); + .sort((a, b) => enumNames.indexOf(a) - enumNames.indexOf(b)) + .map((enumName) => { + const enumDescription = enumDescriptions[enumName]?.description; + const emoji = enumDescriptions[enumName]?.emoji ? enumDescriptions[enumName].emoji : ''; + + if (enumDescription) { + return { + name: `${emoji} ${enumName}:`.padEnd(longest + 8) + enumDescription, + value: enumName, + short: enumName, + emoji: emoji || null + }; + } + else { + return enumName; + } + } + ); } else { enumList = [...enumRuleList]; }