all files / lib/ writer.js

100% Statements 42/42
100% Branches 14/14
100% Functions 7/7
100% Lines 42/42
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                                                10× 10× 10× 10×   10×       10×   10× 10×   10×             10×                                 10× 10×      
'use strict';
 
var Bluebird = require('bluebird');
 
var DEFAULT_TYPE = 'other';
var TYPES = {
  chore: 'Chores',
  docs: 'Documentation Changes',
  feat: 'New Features',
  fix: 'Bug Fixes',
  other: 'Other Changes',
  refactor: 'Refactors',
  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 now = new Date();
  var date = now.getFullYear() + '-' + (now.getMonth() + 1) + '-' + now.getDate();
  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 = '* **' + category + ':**';
 
      if (nested) {
        content.push(categoryHeading);
        prefix = '  *';
      } else {
        prefix = categoryHeading;
      }
 
      types[type][category].forEach(function (commit) {
        var hash = commit.hash.substring(0, 8);
 
        if (options.repoUrl) {
          hash = '[' + hash + '](' + options.repoUrl + '/commit/' + hash + ')';
        }
 
        content.push(prefix + ' ' + commit.subject + ' (' + hash + ')');
      });
    });
 
    content.push('');
  })
  .then(function () {
    content.push('');
    return content.join('\n');
  });
};