forked from leoforfree/cz-customizable
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuildCommit.js
67 lines (51 loc) · 1.64 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
'use strict';
var wrap = require('word-wrap');
var process = require('process');
module.exports = function buildCommit(answers) {
var maxLineWidth = 100;
var systemId = process.env.COMMIT_SYSTEM_ID;
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 result = head;
if (body) {
result += '\n\n' + body;
}
if (breaking) {
result += '\n\n' + 'BREAKING CHANGE:\n' + breaking;
}
if (footer) {
result += '\n\nISSUES CLOSED: ' + footer;
}
if(systemId){
result += '\n\nSYSID: ' + systemId;
}
return escapeSpecialChars(result);
};