writer-opts.js 3.4 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.packageData.zentaoUrl
  60. if (url) {
  61. url = commit.type === 'Features'
  62. ? `${url}/pro/story-view-`
  63. : `${url}/pro/bug-view-`
  64. // Issue URLs.
  65. commit.subject = commit.subject.replace(/#([0-9]+)/g, (_, issue) => {
  66. issues.push(issue)
  67. return `[#${issue}](${url}${issue}.html)`
  68. })
  69. commit.subject = commit.subject.replace(/禅道bug([0-9]+)/g, (_, issue) => {
  70. issues.push(issue)
  71. return `[禅道bug${issue}](${url}${issue}.html)`
  72. })
  73. }
  74. if (context.host) {
  75. // User URLs.
  76. commit.subject = commit.subject.replace(/\B@([a-z0-9](?:-?[a-z0-9/]){0,38})/g, (_, username) => {
  77. if (username.includes('/')) {
  78. return `@${username}`
  79. }
  80. return `[@${username}](${context.host}/${username})`
  81. })
  82. }
  83. }
  84. // remove references that already appear in the subject
  85. commit.references = commit.references.filter(reference => {
  86. if (issues.indexOf(reference.issue) === -1) {
  87. return true
  88. }
  89. return false
  90. })
  91. return commit
  92. },
  93. groupBy: 'type',
  94. commitGroupsSort: 'title',
  95. commitsSort: ['scope', 'subject'],
  96. noteGroupsSort: 'title',
  97. notesSort: compareFunc
  98. }
  99. }