-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
143 lines (116 loc) · 3.76 KB
/
index.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
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
132
133
134
135
136
137
138
139
140
141
142
'use strict';
var parser = require('./lib/parser');
var regex = require('./lib/regex');
var through = require('through2');
var _ = require('lodash');
function assignOpts(options) {
options = _.extend({
headerPattern: /^(\w*)(?:\(([\w\$\.\-\* ]*)\))?\: (.*)$/,
headerCorrespondence: ['type', 'scope', 'subject'],
referenceActions: [
'close',
'closes',
'closed',
'fix',
'fixes',
'fixed',
'resolve',
'resolves',
'resolved'
],
issuePrefixes: ['#'],
noteKeywords: ['BREAKING CHANGE'],
fieldPattern: /^-(.*?)-$/,
revertPattern: /^Revert\s"([\s\S]*)"\s*This reverts commit (\w*)\./,
revertCorrespondence: ['header', 'hash'],
warn: function() {},
mergePattern: null,
mergeCorrespondence: null
}, options);
if (typeof options.headerPattern === 'string') {
options.headerPattern = new RegExp(options.headerPattern);
}
if (typeof options.headerCorrespondence === 'string') {
options.headerCorrespondence = options.headerCorrespondence.split(',');
}
if (typeof options.referenceActions === 'string') {
options.referenceActions = options.referenceActions.split(',');
}
if (typeof options.issuePrefixes === 'string') {
options.issuePrefixes = options.issuePrefixes.split(',');
}
if (typeof options.noteKeywords === 'string') {
options.noteKeywords = options.noteKeywords.split(',');
}
if (typeof options.fieldPattern === 'string') {
options.fieldPattern = new RegExp(options.fieldPattern);
}
if (typeof options.revertPattern === 'string') {
options.revertPattern = new RegExp(options.revertPattern);
}
if (typeof options.revertCorrespondence === 'string') {
options.revertCorrespondence = options.revertCorrespondence.split(',');
}
if (typeof options.mergePattern === 'string') {
options.mergePattern = new RegExp(options.mergePattern);
}
return options;
}
function conventionalCommitsParser(options) {
options = assignOpts(options);
var reg = regex(options);
return through.obj(function(data, enc, cb) {
var commit;
var dataString = data.toString();
var parts = dataString.split("-hash-");
var body = parts[0], footer = '\n-hash-' + parts[1];
function splitOnPattern(pattern, string) {
var match, indexes = [];
while ((match = pattern.exec(string)) !== null) {
indexes.push(match.index);
}
indexes.push(string.length);
let prevIdx = 0;
return indexes.reduce(function(acc, index) {
if (prevIdx - index === 0) return acc;
acc.push(string.substr(prevIdx, (index - prevIdx)));
prevIdx = index;
return acc;
}, [])
}
var headerPattern = new RegExp(options.headerPattern.source, "gm");
var changes = splitOnPattern(headerPattern, body);
changes = changes.map(function(change) {
return splitOnPattern(/BREAKING CHANGE/gm, change);
}).reduce(function(acc, arr) { return acc.concat(arr); });
changes = changes.map(function(changeStr) {
if (!changeStr.match(headerPattern)) {
return changeStr.replace(/(\s*BREAKING CHANGES?\s*:\s*)(.*)/g, "feat(*): $2\n$1$2");
}
return changeStr;
});
changes = changes.map(function(str) { return str + footer });
var _this = this;
try {
changes.forEach(function(change) {
commit = parser(change, options, reg);
_this.push(commit);
});
} catch (err) {
if (options.warn === true) {
cb(err);
} else {
options.warn(err.toString());
cb(null, '');
}
}
cb();
});
}
function sync(commit, options) {
options = assignOpts(options);
var reg = regex(options);
return parser(commit, options, reg);
}
module.exports = conventionalCommitsParser;
module.exports.sync = sync;