-
Notifications
You must be signed in to change notification settings - Fork 200
Expand file tree
/
Copy pathbuildCommit.js
More file actions
76 lines (58 loc) · 2.07 KB
/
Copy pathbuildCommit.js
File metadata and controls
76 lines (58 loc) · 2.07 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
'use strict';
var wrap = require('word-wrap');
module.exports = function buildCommit(answers, config) {
var maxLineWidth = 100;
var wrapOptions = {
trim: true,
newline: '\n',
indent: '',
width: maxLineWidth
};
function addScope(scope) {
if (!scope) return ': '; //it could be type === WIP. So there is no scope
return '(' + scope.trim() + '): ';
}
function addSubject(subject) {
return subject.trim();
}
function escapeSpecialChars(result) {
var specialChars = ['\`'];
specialChars.map(function (item) {
// For some strange reason, we have to pass additional '\' slash to commitizen. Total slashes are 4.
// If user types "feat: `string`", the commit preview should show "feat: `\\string\\`".
// Don't worry. The git log will be "feat: `string`"
result = result.replace(new RegExp(item, 'g'), '\\\\`');
});
return result;
}
// Hard limit this line
var head = (answers.type + addScope(answers.scope) + addSubject(answers.subject)).slice(0, maxLineWidth);
// Wrap these lines at 100 characters
var body = wrap(answers.body, wrapOptions) || '';
body = body.split('|').join('\n');
var breaking = wrap(answers.breaking, wrapOptions);
var footer = wrap(answers.footer, wrapOptions);
var coAuthor = undefined;
if (config.hasOwnProperty('developers') && config.developers.length > 0 && answers.coAuthor.length > 0) {
coAuthor = wrap(answers.coAuthor.join('|'), wrapOptions);
coAuthor = coAuthor.split('|').map(function (e) {
return 'Co-authored-by: ' + e;
}).join('\n');
}
var result = head;
if (body) {
result += '\n\n' + body;
}
if (breaking) {
var breakingPrefix = config && config.breakingPrefix ? config.breakingPrefix : 'BREAKING CHANGE:';
result += '\n\n' + breakingPrefix + '\n' + breaking;
}
if (footer) {
var footerPrefix = config && config.footerPrefix ? config.footerPrefix : 'ISSUES CLOSED:';
result += '\n\n' + footerPrefix + ' ' + footer;
}
if (coAuthor) {
result += '\n\n' + coAuthor;
}
return escapeSpecialChars(result);
};