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.

102 lines
2.5 KiB

1 year ago
'use strict'
const parser = require('./lib/parser')
const regex = require('./lib/regex')
const through = require('through2')
const _ = require('lodash')
function assignOpts (options) {
options = _.extend({
headerPattern: /^(\w*)(?:\(([\w$.\-*/ ]*)\))?: (.*)$/,
headerCorrespondence: ['type', 'scope', 'subject'],
referenceActions: [
'close',
'closes',
'closed',
'fix',
'fixes',
'fixed',
'resolve',
'resolves',
'resolved'
],
issuePrefixes: ['#'],
noteKeywords: ['BREAKING CHANGE', 'BREAKING-CHANGE'],
fieldPattern: /^-(.*?)-$/,
revertPattern: /^Revert\s"([\s\S]*)"\s*This reverts commit (\w*)\./,
revertCorrespondence: ['header', 'hash'],
warn: function () {},
mergePattern: null,
mergeCorrespondence: null
}, options)
if (typeof options.headerPattern === 'string') {
options.headerPattern = new RegExp(options.headerPattern)
}
if (typeof options.headerCorrespondence === 'string') {
options.headerCorrespondence = options.headerCorrespondence.split(',')
}
if (typeof options.referenceActions === 'string') {
options.referenceActions = options.referenceActions.split(',')
}
if (typeof options.issuePrefixes === 'string') {
options.issuePrefixes = options.issuePrefixes.split(',')
}
if (typeof options.noteKeywords === 'string') {
options.noteKeywords = options.noteKeywords.split(',')
}
if (typeof options.fieldPattern === 'string') {
options.fieldPattern = new RegExp(options.fieldPattern)
}
if (typeof options.revertPattern === 'string') {
options.revertPattern = new RegExp(options.revertPattern)
}
if (typeof options.revertCorrespondence === 'string') {
options.revertCorrespondence = options.revertCorrespondence.split(',')
}
if (typeof options.mergePattern === 'string') {
options.mergePattern = new RegExp(options.mergePattern)
}
return options
}
function conventionalCommitsParser (options) {
options = assignOpts(options)
const reg = regex(options)
return through.obj(function (data, enc, cb) {
let commit
try {
commit = parser(data.toString(), options, reg)
cb(null, commit)
} catch (err) {
if (options.warn === true) {
cb(err)
} else {
options.warn(err.toString())
cb(null, '')
}
}
})
}
function sync (commit, options) {
options = assignOpts(options)
const reg = regex(options)
return parser(commit, options, reg)
}
module.exports = conventionalCommitsParser
module.exports.sync = sync