Skip to content

Commit 754c738

Browse files
committed
feat(cz-git): add formatMessageCB can custom finally commit message
add `formatMessageCB` option that is a callback function provide use custom finally commit message link #57 Co-authored-by: Zhengqbbb <1074059947@qq.com>
1 parent ba4ce71 commit 754c738

4 files changed

Lines changed: 76 additions & 32 deletions

File tree

‎packages/@cz-git/plugin-loader/src/index.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ export const loader = async (options: LoaderOptions) => {
3939
return result ?? null
4040
}
4141

42-
function executable<T>(config: Config<T>): config is ExectableConfig<T> {
43-
return typeof config === 'function'
42+
function executable<T>(config: Config<T>, name: string): config is ExectableConfig<T> {
43+
return typeof config === 'function' && !name.endsWith('CB')
4444
}
4545

4646
async function configExecute<T>(
@@ -51,7 +51,7 @@ async function configExecute<T>(
5151
return null
5252

5353
const [name, config] = configItem as [string, Config<T>]
54-
const fn = executable(config) ? config : async () => config
54+
const fn = executable(config, name) ? config : async () => config
5555
return [name, await fn()]
5656
}
5757

‎packages/cz-git/src/generator/message.ts‎

Lines changed: 56 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -92,13 +92,28 @@ const addSubject = (subject?: string, colorize?: boolean) => {
9292
return subject.trim()
9393
}
9494

95-
const addFooter = (footer: string, footerPrefix = '', colorize?: boolean) => {
95+
const addFooter = (footerSuffix: string, footerPrefix = '', colorize?: boolean) => {
96+
if (footerSuffix === '')
97+
return ''
9698
if (footerPrefix === '')
97-
return colorize ? `\n\n${style.green(footer)}` : `\n\n${footer}`
99+
return colorize ? `\n\n${style.green(footerSuffix)}` : `\n\n${footerSuffix}`
98100

99101
return colorize
100-
? `\n\n${style.green(`${footerPrefix} ${footer}`)}`
101-
: `\n\n${footerPrefix} ${footer}`
102+
? `\n\n${style.green(`${footerPrefix} ${footerSuffix}`)}`
103+
: `\n\n${footerPrefix} ${footerSuffix}`
104+
}
105+
106+
const formatDefaultMessage = (defaultHeader: string, body: string, breaking: string, footer: string) => {
107+
let result = defaultHeader
108+
if (body)
109+
result += `\n\n${body}`
110+
111+
if (breaking)
112+
result += `\n\nBREAKING CHANGE :\n${breaking}`
113+
114+
if (footer)
115+
result += footer
116+
return result
102117
}
103118

104119
export const generateMessage = (
@@ -113,38 +128,50 @@ export const generateMessage = (
113128
indent: '',
114129
width: options.breaklineNumber,
115130
}
116-
131+
// resolve custom value
117132
const { customScope, customFooterPrefixs } = answers
118133
answers.scope = getCustomValue(answers.scope, customScope)
119134
answers.footerPrefix = getCustomValue(answers.footerPrefix, customFooterPrefixs) as string
120-
135+
// resolve single | multiple item
121136
const { singleScope, singeIssuePrefix } = getSingleParams(answers, options)
122-
const scope = Array.isArray(answers.scope)
137+
const scopeSource = Array.isArray(answers.scope)
123138
? answers.scope.join(options.scopeEnumSeparator)
124139
: answers.scope
140+
141+
const type = addType(answers.type ?? '', colorize)
125142
const emoji = getEmojiCode(answers.type || '', options)
126-
const head
127-
= `${addEmoji(emoji, 'left', options.emojiAlign)
128-
+ addType(answers.type ?? '', colorize)
129-
+ addScope(singleScope || scope, colorize)
130-
+ addBreakchangeMark(answers.markBreaking, colorize)
131-
}: ${
132-
addEmoji(emoji, 'center', options.emojiAlign)
133-
}${addSubject(answers.subject, colorize)
134-
}${addEmoji(emoji, 'right', options.emojiAlign)}`
143+
const scope = addScope(singleScope || scopeSource, colorize)
144+
const markBreaking = addBreakchangeMark(answers.markBreaking, colorize)
145+
const subject = addSubject(answers.subject, colorize)
146+
const defaultHeader = `${addEmoji(emoji, 'left', options.emojiAlign)
147+
+ type
148+
+ scope
149+
+ markBreaking
150+
}: ${addEmoji(emoji, 'center', options.emojiAlign)
151+
+ subject
152+
+ addEmoji(emoji, 'right', options.emojiAlign)
153+
}`
135154
const body = wrap(answers.body ?? '', wrapOptions)
136155
const breaking = wrap(answers.breaking ?? '', wrapOptions)
137-
const footer = wrap(answers.footer ?? '', wrapOptions)
138-
139-
let result = head
140-
if (body)
141-
result += `\n\n${body}`
142-
143-
if (breaking)
144-
result += `\n\nBREAKING CHANGE :\n${breaking}`
145-
146-
if (footer)
147-
result += addFooter(footer, answers.footerPrefix || singeIssuePrefix, colorize)
148-
149-
return result
156+
const footerSuffix = wrap(answers.footer ?? '', wrapOptions)
157+
const footer = addFooter(footerSuffix, answers.footerPrefix || singeIssuePrefix, colorize)
158+
const defaultMessage = formatDefaultMessage(defaultHeader, body, breaking, footer)
159+
160+
if (typeof options.formatMessageCB === 'function') {
161+
return options.formatMessageCB({
162+
type,
163+
emoji,
164+
scope,
165+
markBreaking,
166+
subject,
167+
defaultHeader,
168+
body,
169+
breaking,
170+
footer,
171+
defaultMessage,
172+
})
173+
}
174+
else {
175+
return defaultMessage
176+
}
150177
}

‎packages/cz-git/src/generator/option.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,5 +61,6 @@ export const generateOptions = (config: UserConfig): CommitizenGitOptions => {
6161
defaultBody: promptConfig.defaultBody ?? defaultConfig.defaultBody,
6262
defaultFooterPrefix: promptConfig.defaultFooterPrefix ?? defaultConfig.defaultFooterPrefix,
6363
defaultIssues: promptConfig.defaultIssues ?? defaultConfig.defaultIssues,
64+
formatMessageCB: promptConfig.formatMessageCB ?? defaultConfig.formatMessageCB,
6465
}
6566
}

‎packages/cz-git/src/shared/types/options.ts‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,19 @@ export interface TypesOption extends Option {
101101
emoji?: string
102102
}
103103

104+
export interface CommitMessageOptions {
105+
type: string
106+
scope: string
107+
emoji: string
108+
markBreaking: string
109+
subject: string
110+
defaultHeader: string
111+
body: string
112+
breaking: string
113+
footer: string
114+
defaultMessage: string
115+
}
116+
104117
export interface CommitizenGitOptions {
105118
/**
106119
* @description: define commonly used commit message alias
@@ -355,6 +368,8 @@ export interface CommitizenGitOptions {
355368
* @example: When you want to use default, just keybord <Enter> it
356369
*/
357370
defaultIssues?: string
371+
372+
formatMessageCB?: (messageMod: CommitMessageOptions) => string
358373
}
359374

360375
export const defaultConfig = Object.freeze({
@@ -422,4 +437,5 @@ export const defaultConfig = Object.freeze({
422437
defaultSubject: '',
423438
defaultFooterPrefix: '',
424439
defaultIssues: '',
440+
formatMessageCB: undefined,
425441
} as CommitizenGitOptions)

0 commit comments

Comments
 (0)