parser.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. 'use strict'
  2. const _ = require('lodash')
  3. const CATCH_ALL = /()(.+)/gi
  4. const SCISSOR = '# ------------------------ >8 ------------------------'
  5. function trimOffNewlines (input) {
  6. const result = input.match(/[^\r\n]/)
  7. if (!result) {
  8. return ''
  9. }
  10. const firstIndex = result.index
  11. let lastIndex = input.length - 1
  12. while (input[lastIndex] === '\r' || input[lastIndex] === '\n') {
  13. lastIndex--
  14. }
  15. return input.substring(firstIndex, lastIndex + 1)
  16. }
  17. function append (src, line) {
  18. if (src) {
  19. src += '\n' + line
  20. } else {
  21. src = line
  22. }
  23. return src
  24. }
  25. function getCommentFilter (char) {
  26. return function (line) {
  27. return line.charAt(0) !== char
  28. }
  29. }
  30. function truncateToScissor (lines) {
  31. const scissorIndex = lines.indexOf(SCISSOR)
  32. if (scissorIndex === -1) {
  33. return lines
  34. }
  35. return lines.slice(0, scissorIndex)
  36. }
  37. function getReferences (input, regex) {
  38. const references = []
  39. let referenceSentences
  40. let referenceMatch
  41. const reApplicable = input.match(regex.references) !== null
  42. ? regex.references
  43. : CATCH_ALL
  44. while ((referenceSentences = reApplicable.exec(input))) {
  45. const action = referenceSentences[1] || null
  46. const sentence = referenceSentences[2]
  47. while ((referenceMatch = regex.referenceParts.exec(sentence))) {
  48. let owner = null
  49. let repository = referenceMatch[1] || ''
  50. const ownerRepo = repository.split('/')
  51. if (ownerRepo.length > 1) {
  52. owner = ownerRepo.shift()
  53. repository = ownerRepo.join('/')
  54. }
  55. const reference = {
  56. action: action,
  57. owner: owner,
  58. repository: repository || null,
  59. issue: referenceMatch[3],
  60. raw: referenceMatch[0],
  61. prefix: referenceMatch[2]
  62. }
  63. references.push(reference)
  64. }
  65. }
  66. return references
  67. }
  68. function passTrough () {
  69. return true
  70. }
  71. function parser (raw, options, regex) {
  72. if (!raw || !raw.trim()) {
  73. throw new TypeError('Expected a raw commit')
  74. }
  75. if (_.isEmpty(options)) {
  76. throw new TypeError('Expected options')
  77. }
  78. if (_.isEmpty(regex)) {
  79. throw new TypeError('Expected regex')
  80. }
  81. let currentProcessedField
  82. let mentionsMatch
  83. const otherFields = {}
  84. const commentFilter = typeof options.commentChar === 'string'
  85. ? getCommentFilter(options.commentChar)
  86. : passTrough
  87. const gpgFilter = line => !line.match(/^\s*gpg:/)
  88. const rawLines = trimOffNewlines(raw).split(/\r?\n/)
  89. const lines = truncateToScissor(rawLines).filter(commentFilter).filter(gpgFilter)
  90. let continueNote = false
  91. let isBody = true
  92. const headerCorrespondence = _.map(options.headerCorrespondence, function (part) {
  93. return part.trim()
  94. })
  95. const revertCorrespondence = _.map(options.revertCorrespondence, function (field) {
  96. return field.trim()
  97. })
  98. const mergeCorrespondence = _.map(options.mergeCorrespondence, function (field) {
  99. return field.trim()
  100. })
  101. let body = null
  102. let footer = null
  103. let header = null
  104. const mentions = []
  105. let merge = null
  106. const notes = []
  107. const references = []
  108. let revert = null
  109. if (lines.length === 0) {
  110. return {
  111. body: body,
  112. footer: footer,
  113. header: header,
  114. mentions: mentions,
  115. merge: merge,
  116. notes: notes,
  117. references: references,
  118. revert: revert,
  119. scope: null,
  120. subject: null,
  121. type: null
  122. }
  123. }
  124. // msg parts
  125. merge = lines.shift()
  126. const mergeParts = {}
  127. const headerParts = {}
  128. body = ''
  129. footer = ''
  130. const mergeMatch = merge.match(options.mergePattern)
  131. if (mergeMatch && options.mergePattern) {
  132. merge = mergeMatch[0]
  133. header = lines.shift()
  134. while (header !== undefined && !header.trim()) {
  135. header = lines.shift()
  136. }
  137. if (!header) {
  138. header = ''
  139. }
  140. _.forEach(mergeCorrespondence, function (partName, index) {
  141. const partValue = mergeMatch[index + 1] || null
  142. mergeParts[partName] = partValue
  143. })
  144. } else {
  145. header = merge
  146. merge = null
  147. _.forEach(mergeCorrespondence, function (partName) {
  148. mergeParts[partName] = null
  149. })
  150. }
  151. const headerMatch = header.match(options.headerPattern)
  152. if (headerMatch) {
  153. _.forEach(headerCorrespondence, function (partName, index) {
  154. const partValue = headerMatch[index + 1] || null
  155. headerParts[partName] = partValue
  156. })
  157. } else {
  158. _.forEach(headerCorrespondence, function (partName) {
  159. headerParts[partName] = null
  160. })
  161. }
  162. Array.prototype.push.apply(references, getReferences(header, {
  163. references: regex.references,
  164. referenceParts: regex.referenceParts
  165. }))
  166. // body or footer
  167. _.forEach(lines, function (line) {
  168. if (options.fieldPattern) {
  169. const fieldMatch = options.fieldPattern.exec(line)
  170. if (fieldMatch) {
  171. currentProcessedField = fieldMatch[1]
  172. return
  173. }
  174. if (currentProcessedField) {
  175. otherFields[currentProcessedField] = append(otherFields[currentProcessedField], line)
  176. return
  177. }
  178. }
  179. let referenceMatched
  180. // this is a new important note
  181. const notesMatch = line.match(regex.notes)
  182. if (notesMatch) {
  183. continueNote = true
  184. isBody = false
  185. footer = append(footer, line)
  186. const note = {
  187. title: notesMatch[1],
  188. text: notesMatch[2]
  189. }
  190. notes.push(note)
  191. return
  192. }
  193. const lineReferences = getReferences(line, {
  194. references: regex.references,
  195. referenceParts: regex.referenceParts
  196. })
  197. if (lineReferences.length > 0) {
  198. isBody = false
  199. referenceMatched = true
  200. continueNote = false
  201. }
  202. Array.prototype.push.apply(references, lineReferences)
  203. if (referenceMatched) {
  204. footer = append(footer, line)
  205. return
  206. }
  207. if (continueNote) {
  208. notes[notes.length - 1].text = append(notes[notes.length - 1].text, line)
  209. footer = append(footer, line)
  210. return
  211. }
  212. if (isBody) {
  213. body = append(body, line)
  214. } else {
  215. footer = append(footer, line)
  216. }
  217. })
  218. if (options.breakingHeaderPattern && notes.length === 0) {
  219. const breakingHeader = header.match(options.breakingHeaderPattern)
  220. if (breakingHeader) {
  221. const noteText = breakingHeader[3] // the description of the change.
  222. notes.push({
  223. title: 'BREAKING CHANGE',
  224. text: noteText
  225. })
  226. }
  227. }
  228. while ((mentionsMatch = regex.mentions.exec(raw))) {
  229. mentions.push(mentionsMatch[1])
  230. }
  231. // does this commit revert any other commit?
  232. const revertMatch = raw.match(options.revertPattern)
  233. if (revertMatch) {
  234. revert = {}
  235. _.forEach(revertCorrespondence, function (partName, index) {
  236. const partValue = revertMatch[index + 1] || null
  237. revert[partName] = partValue
  238. })
  239. } else {
  240. revert = null
  241. }
  242. _.map(notes, function (note) {
  243. note.text = trimOffNewlines(note.text)
  244. return note
  245. })
  246. const msg = _.merge(headerParts, mergeParts, {
  247. merge: merge,
  248. header: header,
  249. body: body ? trimOffNewlines(body) : null,
  250. footer: footer ? trimOffNewlines(footer) : null,
  251. notes: notes,
  252. references: references,
  253. mentions: mentions,
  254. revert: revert
  255. }, otherFields)
  256. return msg
  257. }
  258. module.exports = parser