Simple email application for Android. Original source code: https://framagit.org/dystopia-project/simple-email
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

166 lines
4.3 KiB

  1. 'use strict'
  2. var dateFormat = require('dateformat')
  3. var join = require('path').join
  4. var readFileSync = require('fs').readFileSync
  5. var semverValid = require('semver').valid
  6. var through = require('through2')
  7. var util = require('./lib/util')
  8. var _ = require('lodash')
  9. function conventionalChangelogWriter (context, options) {
  10. var savedKeyCommit
  11. var commits = []
  12. var firstRelease = true
  13. var neverGenerated = true
  14. context = _.extend({
  15. commit: 'commits',
  16. issue: 'issues',
  17. date: dateFormat(new Date(), 'yyyy-mm-dd', true)
  18. }, context)
  19. if (!_.isBoolean(context.linkReferences) && (context.repository || context.repoUrl) && context.commit && context.issue) {
  20. context.linkReferences = true
  21. }
  22. options = _.assign({
  23. groupBy: 'type',
  24. commitsSort: 'header',
  25. noteGroupsSort: 'title',
  26. notesSort: 'text',
  27. generateOn: function (commit) {
  28. return semverValid(commit.version)
  29. },
  30. finalizeContext: function (context) {
  31. return context
  32. },
  33. debug: function () {},
  34. reverse: false,
  35. includeDetails: false,
  36. ignoreReverted: true,
  37. doFlush: true,
  38. mainTemplate: readFileSync(join(__dirname, 'templates/template.hbs'), 'utf-8'),
  39. headerPartial: readFileSync(join(__dirname, 'templates/header.hbs'), 'utf-8'),
  40. commitPartial: readFileSync(join(__dirname, 'templates/commit.hbs'), 'utf-8'),
  41. footerPartial: readFileSync(join(__dirname, 'templates/footer.hbs'), 'utf-8')
  42. }, options)
  43. if ((!_.isFunction(options.transform) && _.isObject(options.transform)) || _.isUndefined(options.transform)) {
  44. options.transform = _.assign({
  45. hash: function (hash) {
  46. if (_.isString(hash)) {
  47. return hash.substring(0, 7)
  48. }
  49. },
  50. header: function (header) {
  51. return header.substring(0, 100)
  52. },
  53. committerDate: function (date) {
  54. if (!date) {
  55. return
  56. }
  57. return dateFormat(date, 'yyyy-mm-dd', true)
  58. }
  59. }, options.transform)
  60. }
  61. var generateOn = options.generateOn
  62. if (_.isString(generateOn)) {
  63. generateOn = function (commit) {
  64. return !_.isUndefined(commit[options.generateOn])
  65. }
  66. } else if (!_.isFunction(generateOn)) {
  67. generateOn = function () {
  68. return false
  69. }
  70. }
  71. options.commitGroupsSort = util.functionify(options.commitGroupsSort)
  72. options.commitsSort = util.functionify(options.commitsSort)
  73. options.noteGroupsSort = util.functionify(options.noteGroupsSort)
  74. options.notesSort = util.functionify(options.notesSort)
  75. return through.obj(function (chunk, enc, cb) {
  76. try {
  77. var result
  78. var commit = util.processCommit(chunk, options.transform, context)
  79. var keyCommit = commit || chunk
  80. // previous blocks of logs
  81. if (options.reverse) {
  82. if (commit) {
  83. commits.push(commit)
  84. }
  85. if (generateOn(keyCommit, commits, context, options)) {
  86. neverGenerated = false
  87. result = util.generate(options, commits, context, keyCommit)
  88. if (options.includeDetails) {
  89. this.push({
  90. log: result,
  91. keyCommit: keyCommit
  92. })
  93. } else {
  94. this.push(result)
  95. }
  96. commits = []
  97. }
  98. } else {
  99. if (generateOn(keyCommit, commits, context, options)) {
  100. neverGenerated = false
  101. result = util.generate(options, commits, context, savedKeyCommit)
  102. if (!firstRelease || options.doFlush) {
  103. if (options.includeDetails) {
  104. this.push({
  105. log: result,
  106. keyCommit: savedKeyCommit
  107. })
  108. } else {
  109. this.push(result)
  110. }
  111. }
  112. firstRelease = false
  113. commits = []
  114. savedKeyCommit = keyCommit
  115. }
  116. if (commit) {
  117. commits.push(commit)
  118. }
  119. }
  120. cb()
  121. } catch (err) {
  122. cb(err)
  123. }
  124. }, function (cb) {
  125. if (!options.doFlush && (options.reverse || neverGenerated)) {
  126. cb(null)
  127. return
  128. }
  129. try {
  130. var result = util.generate(options, commits, context, savedKeyCommit)
  131. if (options.includeDetails) {
  132. this.push({
  133. log: result,
  134. keyCommit: savedKeyCommit
  135. })
  136. } else {
  137. this.push(result)
  138. }
  139. cb()
  140. } catch (err) {
  141. cb(err)
  142. }
  143. })
  144. }
  145. module.exports = conventionalChangelogWriter