This repository has been archived by the owner on Feb 5, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
165 lines (145 loc) · 4.37 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
'use strict';
var dateFormat = require('dateformat');
var join = require('path').join;
var readFileSync = require('fs').readFileSync;
var semverValid = require('semver').valid;
var through = require('through2');
var util = require('./lib/util');
var _ = require('lodash');
function conventionalChangelogWriter(context, options) {
var savedKeyCommit;
var commits = [];
var firstRelease = true;
var neverGenerated = true;
context = _.extend({
commit: 'commits',
issue: 'issues',
date: dateFormat(new Date(), 'yyyy-mm-dd', true)
}, context);
if (!_.isBoolean(context.linkReferences) && (context.repository || context.repoUrl) && context.commit && context.issue) {
context.linkReferences = true;
}
options = _.assign({
groupBy: 'type',
commitsSort: 'header',
noteGroupsSort: 'title',
notesSort: 'text',
generateOn: function(commit) {
return semverValid(commit.version);
},
finalizeContext: function(context) {
return context;
},
debug: function() {},
reverse: false,
includeDetails: false,
ignoreReverted: true,
doFlush: true,
mainTemplate: readFileSync(join(__dirname, 'templates/template.hbs'), 'utf-8'),
headerPartial: readFileSync(join(__dirname, 'templates/header.hbs'), 'utf-8'),
commitPartial: readFileSync(join(__dirname, 'templates/commit.hbs'), 'utf-8'),
footerPartial: readFileSync(join(__dirname, 'templates/footer.hbs'), 'utf-8')
}, options);
if (!_.isFunction(options.transform) && _.isObject(options.transform) || _.isUndefined(options.transform)) {
options.transform = _.assign({
hash: function(hash) {
if (_.isString(hash)) {
return hash.substring(0, 7);
}
},
header: function(header) {
return header.substring(0, 100);
},
committerDate: function(date) {
if (!date) {
return;
}
return dateFormat(date, 'yyyy-mm-dd', true);
}
}, options.transform);
}
var generateOn = options.generateOn;
if (_.isString(generateOn)) {
generateOn = function(commit) {
return !_.isUndefined(commit[options.generateOn]);
};
} else if (!_.isFunction(generateOn)) {
generateOn = function() {
return false;
};
}
options.commitGroupsSort = util.functionify(options.commitGroupsSort);
options.commitsSort = util.functionify(options.commitsSort);
options.noteGroupsSort = util.functionify(options.noteGroupsSort);
options.notesSort = util.functionify(options.notesSort);
return through.obj(function(chunk, enc, cb) {
try {
var result;
var commit = util.processCommit(chunk, options.transform, context);
var keyCommit = commit || chunk;
// previous blocks of logs
if (options.reverse) {
if (commit) {
commits.push(commit);
}
if (generateOn(keyCommit, commits, context, options)) {
neverGenerated = false;
result = util.generate(options, commits, context, keyCommit);
if (options.includeDetails) {
this.push({
log: result,
keyCommit: keyCommit
});
} else {
this.push(result);
}
commits = [];
}
} else {
if (generateOn(keyCommit, commits, context, options)) {
neverGenerated = false;
result = util.generate(options, commits, context, savedKeyCommit);
if (!firstRelease || options.doFlush) {
if (options.includeDetails) {
this.push({
log: result,
keyCommit: savedKeyCommit
});
} else {
this.push(result);
}
}
firstRelease = false;
commits = [];
savedKeyCommit = keyCommit;
}
if (commit) {
commits.push(commit);
}
}
cb();
} catch (err) {
cb(err);
}
}, function(cb) {
if (!options.doFlush && (options.reverse || neverGenerated)) {
cb(null);
return;
}
try {
var result = util.generate(options, commits, context, savedKeyCommit);
if (options.includeDetails) {
this.push({
log: result,
keyCommit: savedKeyCommit
});
} else {
this.push(result);
}
cb();
} catch (err) {
cb(err);
}
});
}
module.exports = conventionalChangelogWriter;