This repository has been archived by the owner on Jan 6, 2021. It is now read-only.
forked from leoforfree/cz-customizable
-
Notifications
You must be signed in to change notification settings - Fork 0
/
buildCommit.js
99 lines (79 loc) · 2.77 KB
/
buildCommit.js
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
const _ = require('lodash');
const wrap = require('word-wrap');
const defaultSubjectSeparator = ': ';
const defaultMaxLineWidth = 100;
const defaultBreaklineChar = '|';
const addTicketNumber = (ticketNumber, config) => {
if (!ticketNumber) {
return '';
}
if (config.ticketNumberPrefix) {
return `${config.ticketNumberPrefix + ticketNumber.trim()} `;
}
return `${ticketNumber.trim()} `;
};
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 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,
};
// Hard limit this line
// eslint-disable-next-line max-len
const head =
addType(answers.type, config) +
addScope(answers.scope, config) +
addTicketNumber(answers.ticketNumber, config) +
addSubject(answers.subject.slice(0, config.subjectLimit));
// Wrap these lines at 100 characters
let body = wrap(answers.body, wrapOptions) || '';
body = addBreaklinesIfNeeded(body, config.breaklineChar);
const breaking = wrap(answers.breaking, wrapOptions);
const footer = wrap(answers.footer, wrapOptions);
let result = head;
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);
};