git-conventional-commits
Advanced tools
Comparing version 1.2.3 to 1.3.0
module.exports = function(config) { | ||
this.generateMarkdown = function(firstChangelogCommit, lastChangelogCommit, release, conventionalCommits, customMarkdown) { | ||
let changelogMarkdown = ''; | ||
/** | ||
* @function | ||
* @param {Object} commitLog - a list of commit candidates to be included in the changelog. | ||
* @param {Object} generatorParams - changelog specific generator parameters. | ||
* @param {string} generatorParams.releaseName - the version number of the release including an optional suffix. | ||
* @param {string} [generatorParams.customMarkdown] - optional custom markdown to be included in the changelog. | ||
* @param {boolean} generatorParams.includeInvalidCommits - if true, commits which do not match the ConCom spec will be included in the changelog. | ||
* @param {string[]} [generatorParams.commitTypes] - an optional list of all commit types to include in the changelog (e.g. ["fix", "feat", ...]). | ||
* @param {string[]} [generatorParams.commitScopes] - an optional list of all commit scopes to included in the changelog (e.g. ["deps", "dev-deps", ...]); If not set all scopes are included. | ||
* @param {RegExp} generatorParams.commitIgnoreRegex - regular expression that matches commits to be excluded from the changelog. | ||
*/ | ||
this.generateMarkdown = function ( | ||
commitLog, | ||
{ | ||
releaseName, | ||
customMarkdown, | ||
includeInvalidCommits, | ||
commitTypes, | ||
commitScopes, | ||
commitIgnoreRegex, | ||
} | ||
) { | ||
const shouldIncludeInChangesSection = (commit) => { | ||
// filter by commit messages regex | ||
if ( | ||
commitIgnoreRegex && | ||
[commit.subject, commit.body].join("\n\n").match(commitIgnoreRegex) | ||
) { | ||
return false; | ||
} | ||
// filter by commit type | ||
if ( | ||
(commit.type || !includeInvalidCommits) && // undefined commit type | ||
commitTypes && | ||
commitTypes.length && // no filter at all | ||
(commit.type === undefined || !commitTypes.includes(commit.type)) | ||
) { | ||
return false; | ||
} | ||
// filter by commit scope | ||
if ( | ||
commit.scope && | ||
commitScopes && | ||
commitScopes.length && | ||
!commitScopes.includes(commit.scope) | ||
) { | ||
return false; | ||
} | ||
return true; | ||
}; | ||
const shouldIncludeInBreakingSection = (commit) => | ||
commit.breakingChanges.length > 0; | ||
const changesSectionCommits = commitLog.filter( | ||
shouldIncludeInChangesSection | ||
); | ||
const breakingSectionCommits = commitLog.filter( | ||
shouldIncludeInBreakingSection | ||
); | ||
let changelogMarkdown = ""; | ||
// ------ generate version headline | ||
let changelogCommitRangeMarkdown = markdownCommitRange(firstChangelogCommit, lastChangelogCommit); | ||
let changelogCommitRangeMarkdown = markdownCommitRange( | ||
commitLog[0].hash, | ||
commitLog[commitLog.length -1].hash, | ||
); | ||
changelogMarkdown += `## **${release || lastChangelogCommit}**` + | ||
` <sub><sup>${yyyy_mm_dd(new Date())} (${changelogCommitRangeMarkdown})</sup></sub>\n`; | ||
changelogMarkdown += | ||
`## **${releaseName || lastChangelogCommit}**` + | ||
` <sub><sup>${yyyy_mm_dd( | ||
new Date() | ||
)} (${changelogCommitRangeMarkdown})</sup></sub>\n`; | ||
changelogMarkdown += '\n'; | ||
changelogMarkdown += "\n"; | ||
if (conventionalCommits.length > 0) { | ||
// ------ generate type specific sections | ||
const groupedChangelog = bucketAggregation(conventionalCommits, commit => commit.type); | ||
if (changesSectionCommits.length > 0 || breakingSectionCommits.length > 0) { | ||
// ------ generate changes section | ||
const groupedChangelog = bucketAggregation( | ||
changesSectionCommits, | ||
(commit) => commit.type | ||
); | ||
const commitTypes = Object.keys(groupedChangelog).sort(); | ||
commitTypes.forEach(commitType => { | ||
changelogMarkdown += generateMarkdownCommitTypeSection(commitType, groupedChangelog[commitType]); | ||
commitTypes.forEach((commitType) => { | ||
changelogMarkdown += generateMarkdownCommitTypeSection( | ||
commitType, | ||
groupedChangelog[commitType] | ||
); | ||
}); | ||
// ------ generate breaking changes section | ||
const breakingCommits = Object.values(groupedChangelog) | ||
.reduce((result, typeCommits) => result.concat(typeCommits), []) | ||
.filter(commit => commit.breakingChanges.length); | ||
// ------ generate breaking section | ||
const groupedBreakingChangelog = bucketAggregation( | ||
breakingSectionCommits, | ||
(commit) => commit.type | ||
); | ||
const breakingCommits = Object.values(groupedBreakingChangelog).reduce( | ||
(result, typeCommits) => result.concat(typeCommits), | ||
[] | ||
); | ||
if (breakingCommits.length) { | ||
changelogMarkdown += generateMarkdownBreakingChangesSection(breakingCommits); | ||
changelogMarkdown += | ||
generateMarkdownBreakingChangesSection(breakingCommits); | ||
} | ||
@@ -35,4 +116,4 @@ | ||
} else { | ||
changelogMarkdown += "*no relevant changes*\n" | ||
changelogMarkdown += "\n" | ||
changelogMarkdown += "*no relevant changes*\n"; | ||
changelogMarkdown += "\n"; | ||
} | ||
@@ -39,0 +120,0 @@ |
@@ -47,42 +47,33 @@ const Config = require("./config"); | ||
exports.handler = async function (argv) { | ||
const config = Config.load(argv.config); | ||
const commitConvention = new CommitConvention(config.convention, argv.commit); | ||
const changelogGenerator = new ChangelogGenerator(config.changelog); | ||
const config = Config.load(argv.config); | ||
const commitConvention = new CommitConvention(config.convention, argv.commit); | ||
const changelogGenerator = new ChangelogGenerator(config.changelog); | ||
let releaseName = argv.release || await commitConvention.version(); | ||
if (argv.name) { | ||
releaseName += ` - ${argv.name}`; | ||
} | ||
const commitLog = await commitConvention.commitLog(); | ||
const changelogCommits = commitLog | ||
.filter( // filter by commit messages regex | ||
commit => ![commit.subject, commit.body].join("\n\n").match(config.changelog.commitIgnoreRegex())) | ||
.filter(// filter by commit type | ||
commit => (!commit.type && config.changelog.includeInvalidCommits) // undefined commit type | ||
|| !config.changelog.commitTypes || !config.changelog.commitTypes.length // no filter at all | ||
|| (commit.type!==undefined && config.changelog.commitTypes.includes(commit.type)) // commit type in filter list | ||
) | ||
.filter( // filter by commit scope | ||
commit => !commit.scope || !config.changelog.commitScopes || !config.changelog.commitScopes.length | ||
|| config.changelog.commitScopes.includes(commit.scope)); | ||
let releaseName = argv.release || (await commitConvention.version()); | ||
if (argv.name) { | ||
releaseName += ` - ${argv.name}`; | ||
} | ||
// TODO filter revert commit pairs | ||
const commitLog = await commitConvention.commitLog(); | ||
// ------ generate markdown -------------------------------------- | ||
let firstChangelogCommit = commitLog[0].hash; | ||
let lastChangelogCommit = commitLog.slice(-1)[0].hash; | ||
let changelogMarkdown = changelogGenerator.generateMarkdown( | ||
firstChangelogCommit, lastChangelogCommit, | ||
releaseName, changelogCommits, argv.markdown); | ||
// TODO filter revert commit pairs | ||
// ------ output ------------------------------------------------- | ||
if (argv.file) { | ||
prependFileSync(argv.file, changelogMarkdown); | ||
} else { | ||
console.log(changelogMarkdown); | ||
} | ||
} | ||
// ------ generate markdown -------------------------------------- | ||
let changelogMarkdown = changelogGenerator.generateMarkdown(commitLog, { | ||
releaseName, | ||
customMarkdown: argv.markdown, | ||
includeInvalidCommits: config.changelog.includeInvalidCommits, | ||
commitTypes: config.changelog.commitTypes, | ||
commitScopes: config.changelog.commitScopes, | ||
commitIgnoreRegexPattern: config.changelog.commitIgnoreRegex(), | ||
}); | ||
// ------ output ------------------------------------------------- | ||
if (argv.file) { | ||
prependFileSync(argv.file, changelogMarkdown); | ||
} else { | ||
console.log(changelogMarkdown); | ||
} | ||
}; | ||
function prependFileSync (file, content) { | ||
@@ -89,0 +80,0 @@ let fileContent = ''; |
{ | ||
"name": "git-conventional-commits", | ||
"version": "1.2.3", | ||
"version": "1.3.0", | ||
"description": "git conventional commits util", | ||
@@ -5,0 +5,0 @@ "licence": "GPLv3", |
68035
718