writer-opts.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. 'use strict'
  2. const compareFunc = require('compare-func')
  3. const Q = require('q')
  4. const readFile = Q.denodeify(require('fs').readFile)
  5. const resolve = require('path').resolve
  6. module.exports = Q.all([
  7. readFile(resolve(__dirname, './templates/template.hbs'), 'utf-8'),
  8. readFile(resolve(__dirname, './templates/header.hbs'), 'utf-8'),
  9. readFile(resolve(__dirname, './templates/commit.hbs'), 'utf-8'),
  10. readFile(resolve(__dirname, './templates/footer.hbs'), 'utf-8')
  11. ])
  12. .spread((template, header, commit, footer) => {
  13. const writerOpts = getWriterOpts()
  14. writerOpts.mainTemplate = template
  15. writerOpts.headerPartial = header
  16. writerOpts.commitPartial = commit
  17. writerOpts.footerPartial = footer
  18. return writerOpts
  19. })
  20. function getWriterOpts () {
  21. return {
  22. transform: (commit, context) => {
  23. let discard = true
  24. const issues = []
  25. commit.notes.forEach(note => {
  26. note.title = 'BREAKING CHANGES'
  27. discard = false
  28. })
  29. if (commit.type === 'feat') {
  30. commit.type = 'Features'
  31. } else if (commit.type === 'fix') {
  32. commit.type = 'Bug Fixes'
  33. } else if (commit.type === 'perf') {
  34. commit.type = 'Performance Improvements'
  35. } else if (commit.type === 'revert' || commit.revert) {
  36. commit.type = 'Reverts'
  37. } else if (discard) {
  38. return
  39. } else if (commit.type === 'docs') {
  40. commit.type = 'Documentation'
  41. } else if (commit.type === 'style') {
  42. commit.type = 'Styles'
  43. } else if (commit.type === 'refactor') {
  44. commit.type = 'Code Refactoring'
  45. } else if (commit.type === 'test') {
  46. commit.type = 'Tests'
  47. } else if (commit.type === 'build') {
  48. commit.type = 'Build System'
  49. } else if (commit.type === 'ci') {
  50. commit.type = 'Continuous Integration'
  51. }
  52. if (commit.scope === '*') {
  53. commit.scope = ''
  54. }
  55. if (typeof commit.hash === 'string') {
  56. commit.shortHash = commit.hash.substring(0, 7)
  57. }
  58. if (typeof commit.subject === 'string') {
  59. let url = context.repository
  60. ? `${context.host}/${context.owner}/${context.repository}`
  61. : context.repoUrl
  62. if (url) {
  63. url = `${url}/issues/`
  64. // Issue URLs.
  65. commit.subject = commit.subject.replace(/#([0-9]+)/g, (_, issue) => {
  66. issues.push(issue)
  67. return `[#${issue}](${url}${issue})`
  68. })
  69. }
  70. if (context.host) {
  71. // User URLs.
  72. commit.subject = commit.subject.replace(/\B@([a-z0-9](?:-?[a-z0-9/]){0,38})/g, (_, username) => {
  73. if (username.includes('/')) {
  74. return `@${username}`
  75. }
  76. return `[@${username}](${context.host}/${username})`
  77. })
  78. }
  79. }
  80. // remove references that already appear in the subject
  81. commit.references = commit.references.filter(reference => {
  82. if (issues.indexOf(reference.issue) === -1) {
  83. return true
  84. }
  85. return false
  86. })
  87. return commit
  88. },
  89. groupBy: 'type',
  90. commitGroupsSort: 'title',
  91. commitsSort: ['scope', 'subject'],
  92. noteGroupsSort: 'title',
  93. notesSort: compareFunc
  94. }
  95. }