UNPKG

2.67 kBJavaScriptView Raw
1'use strict';
2
3var Bluebird = require('bluebird');
4
5var DEFAULT_TYPE = 'other';
6var TYPES = {
7 chore: 'Chores',
8 docs: 'Documentation Changes',
9 feat: 'New Features',
10 fix: 'Bug Fixes',
11 other: 'Other Changes',
12 refactor: 'Refactors',
13 style: 'Code Style Changes',
14 test: 'Tests'
15};
16
17/**
18 * Generate the markdown for the changelog.
19 * @param {String} version - the new version affiliated to this changelog
20 * @param {Array<Object>} commits - array of parsed commit objects
21 * @param {Object} options - generation options
22 * @param {Boolean} options.patch - whether it should be a patch changelog
23 * @param {Boolean} options.minor - whether it should be a minor changelog
24 * @param {Boolean} options.major - whether it should be a major changelog
25 * @param {String} options.repoUrl - repo URL that will be used when linking commits
26 * @returns {Promise<String>} the \n separated changelog string
27 */
28exports.markdown = function (version, commits, options) {
29 var content = [];
30 var now = new Date();
31 var date = now.getFullYear() + '-' + (now.getMonth() + 1) + '-' + now.getDate();
32 var heading;
33
34 if (options.major) {
35 heading = '##';
36 } else if (options.minor) {
37 heading = '###';
38 } else {
39 heading = '####';
40 }
41
42 heading += ' ' + version + ' (' + date + ')';
43
44 content.push(heading);
45 content.push('');
46
47 return Bluebird.resolve(commits)
48 .bind({ types: {} })
49 .each(function (commit) {
50 var type = TYPES[commit.type] ? commit.type : DEFAULT_TYPE;
51 var category = commit.category;
52
53 this.types[type] = this.types[type] || {};
54 this.types[type][category] = this.types[type][category] || [];
55
56 this.types[type][category].push(commit);
57 })
58 .then(function () {
59 return Object.keys(this.types).sort();
60 })
61 .each(function (type) {
62 var types = this.types;
63
64 content.push('##### ' + TYPES[type]);
65 content.push('');
66
67 Object.keys(this.types[type]).forEach(function (category) {
68 var prefix = '*';
69 var nested = types[type][category].length > 1;
70 var categoryHeading = '* **' + category + ':**';
71
72 if (nested) {
73 content.push(categoryHeading);
74 prefix = ' *';
75 } else {
76 prefix = categoryHeading;
77 }
78
79 types[type][category].forEach(function (commit) {
80 var shorthash = commit.hash.substring(0, 8);
81
82 if (options.repoUrl) {
83 shorthash = '[' + shorthash + '](' + options.repoUrl + '/commit/' + commit.hash + ')';
84 }
85
86 content.push(prefix + ' ' + commit.subject + ' (' + shorthash + ')');
87 });
88 });
89
90 content.push('');
91 })
92 .then(function () {
93 content.push('');
94 return content.join('\n');
95 });
96};