Skip to content

Commit

Permalink
feat(cz-git): add formatMessageCB can custom finally commit message
Browse files Browse the repository at this point in the history
add `formatMessageCB` option that is a callback function
provide use custom finally commit message

link #57

Co-authored-by: Zhengqbbb <1074059947@qq.com>
  • Loading branch information
Zhengqbbb committed Aug 19, 2022
1 parent ba4ce71 commit 754c738
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 32 deletions.
6 changes: 3 additions & 3 deletions packages/@cz-git/plugin-loader/src/index.ts
Expand Up @@ -39,8 +39,8 @@ export const loader = async (options: LoaderOptions) => {
return result ?? null
}

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

async function configExecute<T>(
Expand All @@ -51,7 +51,7 @@ async function configExecute<T>(
return null

const [name, config] = configItem as [string, Config<T>]
const fn = executable(config) ? config : async () => config
const fn = executable(config, name) ? config : async () => config
return [name, await fn()]
}

Expand Down
85 changes: 56 additions & 29 deletions packages/cz-git/src/generator/message.ts
Expand Up @@ -92,13 +92,28 @@ const addSubject = (subject?: string, colorize?: boolean) => {
return subject.trim()
}

const addFooter = (footer: string, footerPrefix = '', colorize?: boolean) => {
const addFooter = (footerSuffix: string, footerPrefix = '', colorize?: boolean) => {
if (footerSuffix === '')
return ''
if (footerPrefix === '')
return colorize ? `\n\n${style.green(footer)}` : `\n\n${footer}`
return colorize ? `\n\n${style.green(footerSuffix)}` : `\n\n${footerSuffix}`

return colorize
? `\n\n${style.green(`${footerPrefix} ${footer}`)}`
: `\n\n${footerPrefix} ${footer}`
? `\n\n${style.green(`${footerPrefix} ${footerSuffix}`)}`
: `\n\n${footerPrefix} ${footerSuffix}`
}

const formatDefaultMessage = (defaultHeader: string, body: string, breaking: string, footer: string) => {
let result = defaultHeader
if (body)
result += `\n\n${body}`

if (breaking)
result += `\n\nBREAKING CHANGE :\n${breaking}`

if (footer)
result += footer
return result
}

export const generateMessage = (
Expand All @@ -113,38 +128,50 @@ export const generateMessage = (
indent: '',
width: options.breaklineNumber,
}

// resolve custom value
const { customScope, customFooterPrefixs } = answers
answers.scope = getCustomValue(answers.scope, customScope)
answers.footerPrefix = getCustomValue(answers.footerPrefix, customFooterPrefixs) as string

// resolve single | multiple item
const { singleScope, singeIssuePrefix } = getSingleParams(answers, options)
const scope = Array.isArray(answers.scope)
const scopeSource = Array.isArray(answers.scope)
? answers.scope.join(options.scopeEnumSeparator)
: answers.scope

const type = addType(answers.type ?? '', colorize)
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(emoji, 'center', options.emojiAlign)
}${addSubject(answers.subject, colorize)
}${addEmoji(emoji, 'right', options.emojiAlign)}`
const scope = addScope(singleScope || scopeSource, colorize)
const markBreaking = addBreakchangeMark(answers.markBreaking, colorize)
const subject = addSubject(answers.subject, colorize)
const defaultHeader = `${addEmoji(emoji, 'left', options.emojiAlign)
+ type
+ scope
+ markBreaking
}: ${addEmoji(emoji, 'center', options.emojiAlign)
+ subject
+ addEmoji(emoji, 'right', options.emojiAlign)
}`
const body = wrap(answers.body ?? '', wrapOptions)
const breaking = wrap(answers.breaking ?? '', wrapOptions)
const footer = wrap(answers.footer ?? '', wrapOptions)

let result = head
if (body)
result += `\n\n${body}`

if (breaking)
result += `\n\nBREAKING CHANGE :\n${breaking}`

if (footer)
result += addFooter(footer, answers.footerPrefix || singeIssuePrefix, colorize)

return result
const footerSuffix = wrap(answers.footer ?? '', wrapOptions)
const footer = addFooter(footerSuffix, answers.footerPrefix || singeIssuePrefix, colorize)
const defaultMessage = formatDefaultMessage(defaultHeader, body, breaking, footer)

if (typeof options.formatMessageCB === 'function') {
return options.formatMessageCB({
type,
emoji,
scope,
markBreaking,
subject,
defaultHeader,
body,
breaking,
footer,
defaultMessage,
})
}
else {
return defaultMessage
}
}
1 change: 1 addition & 0 deletions packages/cz-git/src/generator/option.ts
Expand Up @@ -61,5 +61,6 @@ export const generateOptions = (config: UserConfig): CommitizenGitOptions => {
defaultBody: promptConfig.defaultBody ?? defaultConfig.defaultBody,
defaultFooterPrefix: promptConfig.defaultFooterPrefix ?? defaultConfig.defaultFooterPrefix,
defaultIssues: promptConfig.defaultIssues ?? defaultConfig.defaultIssues,
formatMessageCB: promptConfig.formatMessageCB ?? defaultConfig.formatMessageCB,
}
}
16 changes: 16 additions & 0 deletions packages/cz-git/src/shared/types/options.ts
Expand Up @@ -101,6 +101,19 @@ export interface TypesOption extends Option {
emoji?: string
}

export interface CommitMessageOptions {
type: string
scope: string
emoji: string
markBreaking: string
subject: string
defaultHeader: string
body: string
breaking: string
footer: string
defaultMessage: string
}

export interface CommitizenGitOptions {
/**
* @description: define commonly used commit message alias
Expand Down Expand Up @@ -355,6 +368,8 @@ export interface CommitizenGitOptions {
* @example: When you want to use default, just keybord <Enter> it
*/
defaultIssues?: string

formatMessageCB?: (messageMod: CommitMessageOptions) => string
}

export const defaultConfig = Object.freeze({
Expand Down Expand Up @@ -422,4 +437,5 @@ export const defaultConfig = Object.freeze({
defaultSubject: '',
defaultFooterPrefix: '',
defaultIssues: '',
formatMessageCB: undefined,
} as CommitizenGitOptions)

0 comments on commit 754c738

Please sign in to comment.