Skip to content

Commit

Permalink
feat(cz-git): add emojiAlign custom setting emoji header location
Browse files Browse the repository at this point in the history
type: `center` | `left` | `right`

link #43
  • Loading branch information
Zhengqbbb committed Jun 29, 2022
1 parent e344fad commit ab23be2
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 10 deletions.
31 changes: 21 additions & 10 deletions packages/cz-git/src/generator/message.ts
Expand Up @@ -45,14 +45,22 @@ const addBreakchangeMark = (markBreaking?: string | boolean, colorize?: boolean)
return Boolean(markBreaking) || Boolean(process.env.break === "1") ? mark : "";
};

const addEmoji = (type: string, options: CommitizenGitOptions): string => {
if (options.useEmoji && type !== "") {
const itemSource = options.types?.concat(options.typesAppend || []) || [];
const item = itemSource.find((i) => i.value === type);
return item?.emoji ? ` ${item.emoji} ` : " ";
} else {
return " ";
const getEmojiCode = (type: string, options: CommitizenGitOptions): string => {
if (!options.useEmoji || type === "") return "";
const itemSource = options.types?.concat(options.typesAppend || []) || [];
const item = itemSource.find((i) => i.value === type);
return item?.emoji ? item.emoji : "";
};

const addEmoji = (emojiCode: string, align: string, emojiAlign?: string) => {
if (!emojiCode) return "";
switch (emojiAlign) {
case "left" || "center":
return align === emojiAlign ? emojiCode + " " : "";
case "right":
return align === emojiAlign ? " " + emojiCode : "";
}
return align === "center" ? emojiCode + " " : "";
};

const addSubject = (subject?: string, colorize?: boolean) => {
Expand Down Expand Up @@ -92,13 +100,16 @@ export const generateMessage = (
const scope = Array.isArray(answers.scope)
? answers.scope.join(options.scopeEnumSeparator)
: answers.scope;
const emoji = getEmojiCode(answers.type || "", options);
const head =
addEmoji(emoji, "left", options.emojiAlign) +
addType(answers.type ?? "", colorize) +
addScope(singleScope || scope, colorize) +
addBreakchangeMark(answers.markBreaking, colorize) +
":" +
addEmoji(answers.type ?? "", options) +
addSubject(answers.subject, colorize);
": " +
addEmoji(emoji, "center", options.emojiAlign) +
addSubject(answers.subject, colorize) +
addEmoji(emoji, "right", options.emojiAlign);
const body = wrap(answers.body ?? "", wrapOptions);
const breaking = wrap(answers.breaking ?? "", wrapOptions);
const footer = wrap(answers.footer ?? "", wrapOptions);
Expand Down
1 change: 1 addition & 0 deletions packages/cz-git/src/generator/option.ts
Expand Up @@ -26,6 +26,7 @@ export const generateOptions = (config: UserConfig): CommitizenGitOptions => {
types: promptConfig.types ?? defaultConfig.types,
typesAppend: promptConfig.typesAppend ?? defaultConfig.typesAppend,
useEmoji: Boolean(emoji === "1") || promptConfig.useEmoji || defaultConfig.useEmoji,
emojiAlign: promptConfig.emojiAlign || defaultConfig.emojiAlign,
scopes: promptConfig.scopes ?? getEnumList(config?.rules?.["scope-enum"] as any),
scopeOverrides: promptConfig.scopeOverrides ?? defaultConfig.scopeOverrides,
scopeFilters: promptConfig.scopeFilters ?? defaultConfig.scopeFilters,
Expand Down
8 changes: 8 additions & 0 deletions packages/cz-git/src/shared/types/options.ts
Expand Up @@ -135,6 +135,12 @@ export interface CommitizenGitOptions {
*/
useEmoji?: boolean;

/**
* @description: Set the location of emoji in header
* @default: "center"
*/
emojiAlign?: "left" | "center" | "right";

/**
* @description: Provides a select of prompt to select module scopes
* @note it auto import value from rule "scope-enum" with `@commitlint`
Expand Down Expand Up @@ -182,6 +188,7 @@ export interface CommitizenGitOptions {
allowEmptyScopes?: boolean;

/**
* @description: Set the location of empty option (empty) and custom option (custom) in selection range
* @default: "bottom"
*/
customScopesAlign?: "top" | "bottom" | "top-bottom" | "bottom-top";
Expand Down Expand Up @@ -367,6 +374,7 @@ export const defaultConfig = Object.freeze({
typesAppend: [],
themeColorCode: "",
useEmoji: false,
emojiAlign: "center",
scopes: [],
enableMultipleScopes: false,
scopeEnumSeparator: ",",
Expand Down

0 comments on commit ab23be2

Please sign in to comment.