-
Notifications
You must be signed in to change notification settings - Fork 10
/
changelog.js
219 lines (179 loc) · 6.28 KB
/
changelog.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/*
* grunt-changelog
* https://github.com/ericmatthys/grunt-changelog
*
* Copyright (c) 2013 Eric Matthys
* Licensed under the MIT license.
*/
'use strict';
module.exports = function (grunt) {
var _ = require('underscore');
var Handlebars = require('handlebars');
var moment = require('moment');
var semver = require('semver');
grunt.registerMultiTask('changelog', 'Generate a changelog based on commit messages.', function (after, before) {
// Merge task-specific and/or target-specific options with these defaults.
var options = this.options({
featureRegex: /^(.*)closes #\d+:?(.*)$/gim,
fixRegex: /^(.*)fixes #\d+:?(.*)$/gim,
dest: 'changelog.txt',
template: '{{> features}}{{> fixes}}',
after: after,
before: before
});
// Extend partials separately so only one custom partial can be specified
// without having to provide every single partial.
var partials = _.extend({
features: 'NEW:\n\n{{#if features}}{{#each features}}{{> feature}}{{/each}}{{else}}{{> empty}}{{/if}}\n',
feature: ' - {{{this}}}\n',
fixes: 'FIXES:\n\n{{#if fixes}}{{#each fixes}}{{> fix}}{{/each}}{{else}}{{> empty}}{{/if}}',
fix: ' - {{{this}}}\n',
empty: ' (none)\n'
}, options.partials);
var isDateRange;
// Determine if a date or a commit sha / tag was provided for the after
// option. This will determine what kind of range we need to use.
if (options.after) {
if (!semver.valid(options.after)) {
after = moment(options.after);
isDateRange = after.isValid();
}
// Fallback to the provided after value if it is not a valid date. This
// likely means that a commit sha or tag is being used.
if (!isDateRange)
after = options.after;
} else {
// If no after option is provided, default to using the last week.
after = moment().subtract('days', 7);
isDateRange = true;
}
if (isDateRange) {
before = options.before ? moment(options.before) : moment();
} else {
before = options.before ? options.before : 'HEAD';
}
// Compile and register our templates and partials.
var template = Handlebars.compile(options.template);
for (var key in partials) {
Handlebars.registerPartial(key, Handlebars.compile(partials[key]));
}
grunt.verbose.writeflags(options, 'Options');
// Loop through each match and build the array of changes that will be
// passed to the template.
function getChanges(log, regex) {
var changes = [];
var match;
while ((match = regex.exec(log))) {
var change = '';
for (var i = 1, len = match.length; i < len; i++) {
change += match[i];
}
changes.push(change.trim());
}
return changes;
}
// Generate the changelog using the templates defined in options.
function getChangelog(log) {
var data = {
date: moment().format('YYYY-MM-DD'),
features: getChanges(log, options.featureRegex),
fixes: getChanges(log, options.fixRegex)
};
return template(data);
}
// Write the changelog to the destination file.
function writeChangelog(changelog) {
var fileContents = null;
var firstLineFile = null;
var firstLineFileHeader = null;
var regex = null;
if (options.insertType && grunt.file.exists(options.dest)) {
fileContents = grunt.file.read(options.dest);
firstLineFile = fileContents.split('\n')[0];
grunt.log.debug('firstLineFile = ' + firstLineFile);
switch (options.insertType) {
case 'prepend':
changelog = changelog + '\n' + fileContents;
break;
case 'append':
changelog = fileContents + '\n' + changelog;
break;
default:
grunt.fatal('"' + options.insertType + '" is not a valid insertType. Please use "append" or "prepend".');
return false;
}
}
if (options.fileHeader) {
firstLineFileHeader = options.fileHeader.split('\n')[0];
grunt.log.debug('firstLineFileHeader = ' + firstLineFileHeader);
if (options.insertType === 'prepend') {
if (firstLineFile !== firstLineFileHeader) {
changelog = options.fileHeader + '\n\n' + changelog;
} else {
regex = new RegExp(options.fileHeader+'\n\n','m');
changelog = options.fileHeader + '\n\n' + changelog.replace(regex, '');
}
// insertType === 'append' || undefined
} else {
if (firstLineFile !== firstLineFileHeader) {
changelog = options.fileHeader + '\n\n' + changelog;
}
}
}
grunt.file.write(options.dest, changelog);
// Log the results.
grunt.log.ok(changelog);
grunt.log.writeln();
grunt.log.writeln('Changelog created at '+ options.dest.toString().cyan + '.');
}
// If a log is passed in as an option, don't run the git log command
// and just use the explicit log instead.
if (options.log) {
// Check to make sure that the log exists before going any further.
if (!grunt.file.exists(options.log)) {
grunt.fatal('This log file does not exist.');
return false;
}
var result = grunt.file.read(options.log);
writeChangelog(getChangelog(result));
return;
}
var done = this.async();
// Build our options for the git log command.
// Default: Only print the commit message without paging.
var args = [
'--no-pager',
'log'
];
if (options.logArguments) {
args.push.apply(args, options.logArguments);
} else {
args.push(
'--pretty=format:%s',
'--no-merges'
);
}
if (isDateRange) {
args.push('--after="' + after.format() + '"');
args.push('--before="' + before.format() + '"');
} else {
args.splice(2, 0, after + '..' + before);
}
grunt.verbose.writeln('git ' + args.join(' '));
// Run the git log command and parse the result.
grunt.util.spawn(
{
cmd: 'git',
args: args
},
function (error, result) {
if (error) {
grunt.log.error(error);
return done(false);
}
writeChangelog(getChangelog(result));
done();
}
);
});
};