-
Notifications
You must be signed in to change notification settings - Fork 200
Expand file tree
/
Copy pathbuildCommit.js
More file actions
106 lines (86 loc) · 2.93 KB
/
Copy pathbuildCommit.js
File metadata and controls
106 lines (86 loc) · 2.93 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
const _ = require('lodash');
const wrap = require('word-wrap');
const defaultSubjectSeparator = ': ';
const defaultMaxLineWidth = 100;
const defaultBreaklineChar = '|';
const defaultBreakingPrefix = 'BREAKING CHANGE:';
const defaultFooterPrefix = 'ISSUES CLOSED:';
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 addBreaklinesIfNeededAndWrap = (value = '', config) => {
const breaklineChar = _.get(config, 'breaklineChar', defaultBreaklineChar);
const wrapOptions = {
trim: true,
newline: '\n',
indent: '',
width: defaultMaxLineWidth,
};
return (
value
.split(breaklineChar)
// Wrap each line at 100 characters
.map(p => wrap(p, wrapOptions))
.join('\n')
.valueOf()
);
};
const addBreaking = (breaking, config) => {
const breakingPrefix = _.get(config, 'breakingPrefix', defaultBreakingPrefix);
return `\n\n${breakingPrefix}\n${breaking}`;
};
const addFooter = (footer, config) => {
if (config && config.footerPrefix === '') return `\n\n${addBreaklinesIfNeededAndWrap(footer, config)}`;
const footerPrefix = _.get(config, 'footerPrefix', defaultFooterPrefix);
return `\n\n${footerPrefix} ${addBreaklinesIfNeededAndWrap(footer, config)}`;
};
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) => {
// 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, defaultMaxLineWidth);
let result = head;
if (answers.body) {
result += `\n\n${addBreaklinesIfNeededAndWrap(answers.body, config)}`;
}
if (answers.breaking) {
result += addBreaking(answers.breaking, config);
}
if (answers.footer) {
result += addFooter(answers.footer, config);
}
return escapeSpecialChars(result);
};