forked from leoforfree/cz-customizable
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuildCommit.js
More file actions
131 lines (106 loc) · 4.03 KB
/
Copy pathbuildCommit.js
File metadata and controls
131 lines (106 loc) · 4.03 KB
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
const _ = require('lodash');
const wrap = require('word-wrap');
const defaultSubjectSeparator = ': ';
const defaultMaxLineWidth = 100;
const defaultBreaklineChar = '|';
const getTicketNumbers = (ticketNumbers, config) => {
if (!ticketNumbers) {
return undefined;
}
const ticketPrefix = _.get(config, 'ticketNumberPrefix', '');
return `${ticketNumbers.length > 0 ? _.get(config, 'ticketNumberPositionPrefix', '') : ''}${ticketNumbers
.map(ticket => {
let ticketNumber = ticket.trim();
if (ticketPrefix !== '') {
ticketNumber = ticketNumber.replace(ticketPrefix, '');
}
return ticketPrefix + ticketNumber;
})
.join(_.get(config, 'ticketNumberSeparator', ' '))}${
ticketNumbers.length > 0 ? _.get(config, 'ticketNumberPositionSuffix', '') || '' : ''
}`;
};
const addTicketNumbersToHead = (ticketNumbers, position, config) => {
if (!ticketNumbers || config !== position) {
return '';
}
return `${position === 'inline-append' ? ' ' : ''}${ticketNumbers}${position === 'inline-prepend' ? ' ' : ''}`;
};
const addScope = (scope, config) => {
const separator = _.get(config, 'subjectSeparator', defaultSubjectSeparator);
if (!scope) return separator; // it could be type === WIP. So there is no scope
return `(${scope.trim()})${separator}`;
};
const addSubject = subject => _.trim(subject);
const addWip = (wip, config) => {
const wipPrefix = wip ? _.get(config, 'wipPrefix', 'w') : '';
return _.trim(wipPrefix);
};
const addType = (type, config) => {
const prefix = _.get(config, 'typePrefix', '');
const suffix = _.get(config, 'typeSuffix', '');
return _.trim(`${prefix}${type}${suffix}`);
};
const addBreaklinesIfNeeded = (value, breaklineChar = defaultBreaklineChar) =>
value
.split(breaklineChar)
.join('\n')
.valueOf();
const addFooter = (footer, config) => {
if (config && config.footerPrefix === '') return `\n\n${footer}`;
const footerPrefix = config && config.footerPrefix ? config.footerPrefix : 'ISSUES CLOSED:';
return `\n\n${footerPrefix} ${addBreaklinesIfNeeded(footer, config.breaklineChar)}`;
};
const escapeSpecialChars = result => {
// eslint-disable-next-line no-useless-escape
const specialChars = ['`'];
let newResult = result;
// eslint-disable-next-line array-callback-return
specialChars.map(item => {
// If user types "feat: `string`", the commit preview should show "feat: `\string\`".
// Don't worry. The git log will be "feat: `string`"
newResult = result.replace(new RegExp(item, 'g'), '\\`');
});
return newResult;
};
module.exports = (answers, config) => {
const wrapOptions = {
trim: true,
newline: '\n',
indent: '',
width: defaultMaxLineWidth,
};
const ticketNumbers = getTicketNumbers(answers.ticketNumbers, config);
const ticketNumberPosition = config && config.ticketNumberPosition ? config.ticketNumberPosition : 'inline-prepend';
// Hard limit this line
// eslint-disable-next-line max-len
const head = (
addWip(answers.wip, config) +
addType(answers.type, config) +
addScope(answers.scope, config) +
addTicketNumbersToHead(ticketNumbers, 'inline-prepend', ticketNumberPosition) +
addSubject(answers.subject) +
addTicketNumbersToHead(ticketNumbers, 'inline-append', ticketNumberPosition)
).slice(0, defaultMaxLineWidth);
// Wrap these lines at 100 characters
let body = wrap(answers.body, wrapOptions) || '';
body = addBreaklinesIfNeeded(body, config.breaklineChar);
const footerTickets = ticketNumberPosition === 'footer' ? wrap(ticketNumbers) : null;
const breaking = wrap(answers.breaking, wrapOptions);
const footer = wrap(answers.footer, wrapOptions);
let result = head;
if (footerTickets) {
result += `\n\n${footerTickets}`;
}
if (body) {
result += `\n\n${body}`;
}
if (breaking) {
const breakingPrefix = config && config.breakingPrefix ? config.breakingPrefix : 'BREAKING CHANGE:';
result += `\n\n${breakingPrefix}\n${breaking}`;
}
if (footer) {
result += addFooter(footer, config);
}
return escapeSpecialChars(result);
};