/
message.ts
177 lines (156 loc) · 5.51 KB
/
message.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/**
* @description: generate commit message(generateMessage)
* @author: @Zhengqbbb (zhengqbbb@gmail.com)
* @license: MIT
*/
import { style } from '@cz-git/inquirer'
import { getCurrentScopes, handleStandardScopes, isSingleItem, wrap } from '../shared'
import type { Answers, CommitizenGitOptions } from '../shared'
export const getAliasMessage = (config: CommitizenGitOptions, alias?: string) => {
if (!alias || typeof config?.alias?.[alias] !== 'string') {
throw new Error(`${style.red(`>>> The alias "${alias}" is undefined`)}
${style.yellow('>>> The currently obtained alias configuration:')}
${style.cyan(JSON.stringify(config.alias, null, 2))}
`)
}
console.log(`${style.green(`>>> Using "${alias}" commit message alias:`)}`)
console.log(`${style.gray(config.alias[alias])}`)
return config.alias[alias]
}
const getSingleParams = (answers: Answers, options: CommitizenGitOptions) => {
const singleIndex = 0
const mapping = {
singleScope: '',
singeIssuePrefix: '',
}
const scopeList = handleStandardScopes(
getCurrentScopes(options.scopes, options.scopeOverrides, answers.type),
)
if (isSingleItem(options.allowCustomScopes, options.allowEmptyScopes, scopeList))
mapping.singleScope = scopeList[singleIndex].value
if (
isSingleItem(
options.allowCustomIssuePrefixs,
options.allowEmptyIssuePrefixs,
options.issuePrefixs,
)
)
mapping.singeIssuePrefix = options.issuePrefixs?.[singleIndex].value || ''
return mapping
}
const getCustomValue = (originVal?: string | string [], customVal?: string) => {
if (Array.isArray(originVal))
return originVal
return originVal !== '___CUSTOM___' ? originVal || '' : customVal || ''
}
const addType = (type: string, colorize?: boolean) => (colorize ? style.green(type) : type)
const addScope = (scope?: string, colorize?: boolean) => {
if (!scope)
return ''
scope = colorize ? style.yellow(scope) : scope
return `(${scope?.trim()})`
}
const addBreakchangeMark = (markBreaking?: string | boolean, colorize?: boolean) => {
const mark = colorize ? style.red('!') : '!'
return Boolean(markBreaking) || Boolean(process.env.break === '1') ? mark : ''
}
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) => {
if (!subject)
return ''
subject = colorize ? style.cyan(subject) : subject
return subject.trim()
}
const addFooter = (footerSuffix: string, footerPrefix = '', colorize?: boolean) => {
if (footerSuffix === '')
return ''
if (footerPrefix === '')
return colorize ? `\n\n${style.green(footerSuffix)}` : `\n\n${footerSuffix}`
return colorize
? `\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 = (
answers: Answers,
options: CommitizenGitOptions,
colorize = false,
) => {
const wrapOptions = {
breaklineChar: options.breaklineChar || '|',
trim: true,
newLine: '\n',
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 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 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 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
}
}