index.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. 'use strict'
  2. const parser = require('./lib/parser')
  3. const regex = require('./lib/regex')
  4. const through = require('through2')
  5. const _ = require('lodash')
  6. function assignOpts (options) {
  7. options = _.extend({
  8. headerPattern: /^(\w*)(?:\(([\w$.\-*/ ]*)\))?: (.*)$/,
  9. headerCorrespondence: ['type', 'scope', 'subject'],
  10. referenceActions: [
  11. 'close',
  12. 'closes',
  13. 'closed',
  14. 'fix',
  15. 'fixes',
  16. 'fixed',
  17. 'resolve',
  18. 'resolves',
  19. 'resolved'
  20. ],
  21. issuePrefixes: ['#'],
  22. noteKeywords: ['BREAKING CHANGE', 'BREAKING-CHANGE'],
  23. fieldPattern: /^-(.*?)-$/,
  24. revertPattern: /^Revert\s"([\s\S]*)"\s*This reverts commit (\w*)\./,
  25. revertCorrespondence: ['header', 'hash'],
  26. warn: function () {},
  27. mergePattern: null,
  28. mergeCorrespondence: null
  29. }, options)
  30. if (typeof options.headerPattern === 'string') {
  31. options.headerPattern = new RegExp(options.headerPattern)
  32. }
  33. if (typeof options.headerCorrespondence === 'string') {
  34. options.headerCorrespondence = options.headerCorrespondence.split(',')
  35. }
  36. if (typeof options.referenceActions === 'string') {
  37. options.referenceActions = options.referenceActions.split(',')
  38. }
  39. if (typeof options.issuePrefixes === 'string') {
  40. options.issuePrefixes = options.issuePrefixes.split(',')
  41. }
  42. if (typeof options.noteKeywords === 'string') {
  43. options.noteKeywords = options.noteKeywords.split(',')
  44. }
  45. if (typeof options.fieldPattern === 'string') {
  46. options.fieldPattern = new RegExp(options.fieldPattern)
  47. }
  48. if (typeof options.revertPattern === 'string') {
  49. options.revertPattern = new RegExp(options.revertPattern)
  50. }
  51. if (typeof options.revertCorrespondence === 'string') {
  52. options.revertCorrespondence = options.revertCorrespondence.split(',')
  53. }
  54. if (typeof options.mergePattern === 'string') {
  55. options.mergePattern = new RegExp(options.mergePattern)
  56. }
  57. return options
  58. }
  59. function conventionalCommitsParser (options) {
  60. options = assignOpts(options)
  61. const reg = regex(options)
  62. return through.obj(function (data, enc, cb) {
  63. let commit
  64. try {
  65. commit = parser(data.toString(), options, reg)
  66. cb(null, commit)
  67. } catch (err) {
  68. if (options.warn === true) {
  69. cb(err)
  70. } else {
  71. options.warn(err.toString())
  72. cb(null, '')
  73. }
  74. }
  75. })
  76. }
  77. function sync (commit, options) {
  78. options = assignOpts(options)
  79. const reg = regex(options)
  80. return parser(commit, options, reg)
  81. }
  82. module.exports = conventionalCommitsParser
  83. module.exports.sync = sync