-
Notifications
You must be signed in to change notification settings - Fork 127
/
writer.js
99 lines (83 loc) · 2.71 KB
/
writer.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
'use strict';
var Bluebird = require('bluebird');
var DEFAULT_TYPE = 'other';
var TYPES = {
build: 'Build System / Dependencies',
ci: 'Continuous Integration',
chore: 'Chores',
docs: 'Documentation Changes',
feat: 'New Features',
fix: 'Bug Fixes',
other: 'Other Changes',
perf: 'Performance Improvements',
refactor: 'Refactors',
revert: 'Reverts',
style: 'Code Style Changes',
test: 'Tests'
};
/**
* Generate the markdown for the changelog.
* @param {String} version - the new version affiliated to this changelog
* @param {Array<Object>} commits - array of parsed commit objects
* @param {Object} options - generation options
* @param {Boolean} options.patch - whether it should be a patch changelog
* @param {Boolean} options.minor - whether it should be a minor changelog
* @param {Boolean} options.major - whether it should be a major changelog
* @param {String} options.repoUrl - repo URL that will be used when linking commits
* @returns {Promise<String>} the \n separated changelog string
*/
exports.markdown = function (version, commits, options) {
var content = [];
var date = new Date().toJSON().slice(0,10);
var heading;
if (options.major) {
heading = '##';
} else if (options.minor) {
heading = '###';
} else {
heading = '####';
}
heading += ' ' + version + ' (' + date + ')';
content.push(heading);
content.push('');
return Bluebird.resolve(commits)
.bind({ types: {} })
.each(function (commit) {
var type = TYPES[commit.type] ? commit.type : DEFAULT_TYPE;
var category = commit.category;
this.types[type] = this.types[type] || {};
this.types[type][category] = this.types[type][category] || [];
this.types[type][category].push(commit);
})
.then(function () {
return Object.keys(this.types).sort();
})
.each(function (type) {
var types = this.types;
content.push('##### ' + TYPES[type]);
content.push('');
Object.keys(this.types[type]).forEach(function (category) {
var prefix = '*';
var nested = types[type][category].length > 1;
var categoryHeading = prefix + (category ? ' **' + category + ':**' : '');
if (nested && category) {
content.push(categoryHeading);
prefix = ' *';
} else {
prefix = categoryHeading;
}
types[type][category].forEach(function (commit) {
var shorthash = commit.hash.substring(0, 8);
if (options.repoUrl) {
shorthash = '[' + shorthash + '](' + options.repoUrl + '/commit/' + commit.hash + ')';
}
content.push(prefix + ' ' + commit.subject + ' (' + shorthash + ')');
});
});
content.push('');
})
.then(function () {
content.push('');
return content.join('\n');
});
};