diff --git a/packages/conventional-changelog-lmc-bitbucket/src/conventionalChangelog.js b/packages/conventional-changelog-lmc-bitbucket/src/conventionalChangelog.js new file mode 100644 index 0000000..7c7083c --- /dev/null +++ b/packages/conventional-changelog-lmc-bitbucket/src/conventionalChangelog.js @@ -0,0 +1,25 @@ +/* eslint-disable no-param-reassign */ +/* eslint-disable jsdoc/require-jsdoc */ +const { readFile } = require('fs').promises; +const { resolve } = require('path'); + +async function createConventionalChangelogOpts(parserOpts, writerOpts) { + return Promise.all([ + readFile(resolve(__dirname, 'templates/template.hbs'), 'utf-8'), + readFile(resolve(__dirname, 'templates/header.hbs'), 'utf-8'), + readFile(resolve(__dirname, 'templates/commit.hbs'), 'utf-8'), + readFile(resolve(__dirname, 'templates/footer.hbs'), 'utf-8'), + ]).then(([template, header, commit, footer]) => { + writerOpts.mainTemplate = template; + writerOpts.headerPartial = header; + writerOpts.commitPartial = commit; + writerOpts.footerPartial = footer; + + return { + parserOpts, + writerOpts, + }; + }); +} + +module.exports.createConventionalChangelogOpts = createConventionalChangelogOpts; diff --git a/packages/conventional-changelog-lmc-bitbucket/src/index.js b/packages/conventional-changelog-lmc-bitbucket/src/index.js index 00a7532..22a6778 100644 --- a/packages/conventional-changelog-lmc-bitbucket/src/index.js +++ b/packages/conventional-changelog-lmc-bitbucket/src/index.js @@ -1,14 +1,23 @@ -const conventionalChangelog = require(`./conventional-changelog`); -const { parserOpts, writerOpts, recommendedBumpOpts } = require('@lmc-eu/conventional-changelog-lmc'); +/* eslint-disable jsdoc/require-jsdoc */ +const { createConventionalChangelogOpts } = require(`./conventionalChangelog`); +const { + createParserOpts, + createWriterOpts, + createConventionalRecommendedBumpOpts, +} = require('@lmc-eu/conventional-changelog-lmc'); -module.exports = Promise.all([conventionalChangelog, parserOpts, recommendedBumpOpts, writerOpts]).then( - // Using same configuration as other configurations - // Did not find any documentation whether the output must be in this format - // eslint-disable-next-line no-shadow - ([conventionalChangelog, parserOpts, recommendedBumpOpts, writerOpts]) => ({ - conventionalChangelog, +async function createPreset() { + const parserOpts = createParserOpts(); + const writerOpts = createWriterOpts(); + const recommendedBumpOpts = createConventionalRecommendedBumpOpts(parserOpts); + const conventionalChangelog = await createConventionalChangelogOpts(parserOpts, writerOpts); + + return { parserOpts, - recommendedBumpOpts, writerOpts, - }), -); + recommendedBumpOpts, + conventionalChangelog, + }; +} + +module.exports = createPreset; diff --git a/packages/conventional-changelog-lmc-github/src/conventional-changelog.js b/packages/conventional-changelog-lmc-github/src/conventional-changelog.js deleted file mode 100644 index eb08d41..0000000 --- a/packages/conventional-changelog-lmc-github/src/conventional-changelog.js +++ /dev/null @@ -1,20 +0,0 @@ -const { readFile } = require('fs').promises; -const { resolve } = require('path'); -const { parserOpts, writerOpts } = require('@lmc-eu/conventional-changelog-lmc'); - -module.exports = Promise.all([ - readFile(resolve(__dirname, 'templates/template.hbs'), 'utf-8'), - readFile(resolve(__dirname, 'templates/header.hbs'), 'utf-8'), - readFile(resolve(__dirname, 'templates/commit.hbs'), 'utf-8'), - readFile(resolve(__dirname, 'templates/footer.hbs'), 'utf-8'), -]).then(([template, header, commit, footer]) => { - writerOpts.mainTemplate = template; - writerOpts.headerPartial = header; - writerOpts.commitPartial = commit; - writerOpts.footerPartial = footer; - - return { - parserOpts, - writerOpts, - }; -}); diff --git a/packages/conventional-changelog-lmc-bitbucket/src/conventional-changelog.js b/packages/conventional-changelog-lmc-github/src/conventionalChangelog.js similarity index 100% rename from packages/conventional-changelog-lmc-bitbucket/src/conventional-changelog.js rename to packages/conventional-changelog-lmc-github/src/conventionalChangelog.js diff --git a/packages/conventional-changelog-lmc/src/bump-opts.js b/packages/conventional-changelog-lmc/src/bump-opts.js deleted file mode 100644 index acdd300..0000000 --- a/packages/conventional-changelog-lmc/src/bump-opts.js +++ /dev/null @@ -1,34 +0,0 @@ -const parserOpts = require('./parser-opts'); - -module.exports = { - parserOpts, - - whatBump: (commits) => { - let level = 2; - let breakings = 0; - let features = 0; - - commits.forEach((commit) => { - if (commit.notes.length > 0) { - breakings += commit.notes.length; - level = 0; - } else if (commit.type === 'BREAKING CHANGE' || commit.type === 'BREAKING CHANGES') { - breakings += 1; - level = 0; - } else if (commit.type === `Feat`) { - features += 1; - if (level === 2) { - level = 1; - } - } - }); - - return { - level, - reason: - breakings === 1 - ? `There are ${breakings} BREAKING CHANGE and ${features} features` - : `There are ${breakings} BREAKING CHANGES and ${features} features`, - }; - }, -}; diff --git a/packages/conventional-changelog-lmc/src/conventionalRecommendedBump.js b/packages/conventional-changelog-lmc/src/conventionalRecommendedBump.js new file mode 100644 index 0000000..14fddf9 --- /dev/null +++ b/packages/conventional-changelog-lmc/src/conventionalRecommendedBump.js @@ -0,0 +1,38 @@ +/* eslint-disable jsdoc/require-jsdoc */ + +function createConventionalRecommendedBumpOpts(parserOpts) { + return { + parserOpts, + + whatBump: (commits) => { + let level = 2; + let breakings = 0; + let features = 0; + + commits.forEach((commit) => { + if (commit.notes.length > 0) { + breakings += commit.notes.length; + level = 0; + } else if (commit.type === 'BREAKING CHANGE' || commit.type === 'BREAKING CHANGES') { + breakings += 1; + level = 0; + } else if (commit.type === `Feat`) { + features += 1; + if (level === 2) { + level = 1; + } + } + }); + + return { + level, + reason: + breakings === 1 + ? `There are ${breakings} BREAKING CHANGE and ${features} features` + : `There are ${breakings} BREAKING CHANGES and ${features} features`, + }; + }, + }; +} + +module.exports.createConventionalRecommendedBumpOpts = createConventionalRecommendedBumpOpts; diff --git a/packages/conventional-changelog-lmc/src/index.js b/packages/conventional-changelog-lmc/src/index.js index bd481f1..98e7e18 100644 --- a/packages/conventional-changelog-lmc/src/index.js +++ b/packages/conventional-changelog-lmc/src/index.js @@ -1,9 +1,9 @@ -const parserOpts = require('./parser-opts'); -const writerOpts = require('./writer-opts'); -const recommendedBumpOpts = require('./bump-opts'); +const { createParserOpts } = require('./parserOpts'); +const { createWriterOpts } = require('./writerOpts'); +const { createConventionalRecommendedBumpOpts } = require('./conventionalRecommendedBump'); module.exports = { - parserOpts, - recommendedBumpOpts, - writerOpts, + createParserOpts, + createWriterOpts, + createConventionalRecommendedBumpOpts, }; diff --git a/packages/conventional-changelog-lmc/src/parser-opts.js b/packages/conventional-changelog-lmc/src/parser-opts.js deleted file mode 100644 index 87adc5a..0000000 --- a/packages/conventional-changelog-lmc/src/parser-opts.js +++ /dev/null @@ -1,8 +0,0 @@ -module.exports = { - headerPattern: /^(?:Pull request #[0-9]+: )?(?:([a-zA-Z]*-[0-9_]*)(?: ))* ?([\w ]*)(?:\((.*)\))?!?: (.*)$/, - breakingHeaderPattern: /^(?:([a-zA-Z]*-[0-9_]*)(?: ))* ?([\w ]*)(?:\((.*)\))?!: (.*)$/, - headerCorrespondence: ['body', 'type', 'scope', 'subject'], - noteKeywords: ['BREAKING CHANGE', 'BREAKING CHANGES'], - revertPattern: /^(?:Revert:)\s"?([\s\S]+?)"?\s*This reverts commit (\w*)\./i, - revertCorrespondence: ['header', 'hash'], -}; diff --git a/packages/conventional-changelog-lmc/src/parserOpts.js b/packages/conventional-changelog-lmc/src/parserOpts.js new file mode 100644 index 0000000..19f2f2f --- /dev/null +++ b/packages/conventional-changelog-lmc/src/parserOpts.js @@ -0,0 +1,14 @@ +/* eslint-disable jsdoc/require-jsdoc */ + +function createParserOpts() { + return { + headerPattern: /^(?:Pull request #[0-9]+: )?(?:([a-zA-Z]*-[0-9_]*)(?: ))* ?([\w ]*)(?:\((.*)\))?!?: (.*)$/, + breakingHeaderPattern: /^(?:([a-zA-Z]*-[0-9_]*)(?: ))* ?([\w ]*)(?:\((.*)\))?!: (.*)$/, + headerCorrespondence: ['body', 'type', 'scope', 'subject'], + noteKeywords: ['BREAKING CHANGE', 'BREAKING CHANGES'], + revertPattern: /^(?:Revert:)\s"?([\s\S]+?)"?\s*This reverts commit (\w*)\./i, + revertCorrespondence: ['header', 'hash'], + }; +} + +module.exports.createParserOpts = createParserOpts; diff --git a/packages/conventional-changelog-lmc/src/writer-opts.js b/packages/conventional-changelog-lmc/src/writer-opts.js deleted file mode 100644 index 5114aa7..0000000 --- a/packages/conventional-changelog-lmc/src/writer-opts.js +++ /dev/null @@ -1,102 +0,0 @@ -/* eslint-disable consistent-return */ -/* eslint-disable prefer-destructuring */ -/* eslint-disable no-param-reassign */ -const compareFunc = require('compare-func'); - -/** - * Formats issues using the issueURL as the prefix of the complete issue URL - * - * @param {string} issueUrl - if the issueURL is falsy, then the issue will be printed as-is. Otherwise, it will be printed as a link - * @param {string} issue - the issue reference (without the # in-front of it) - * @returns {string} - Either the issue or a Markdown-formatted link to the issue. - */ -function formatIssue(issueUrl, issue) { - if (issueUrl) { - return `[#${issue}](${issueUrl}/${issue})`; - } - - return `#${issue}`; -} - -module.exports = { - transform: (commit, context) => { - let discard = true; - const issues = []; - - commit.notes.forEach((note) => { - note.title = 'BREAKING CHANGES'; - discard = false; - }); - - const transformedCommit = commit; - - if (commit.type === 'BREAKING CHANGE' || commit.type === 'BREAKING CHANGES') { - transformedCommit.type = 'BREAKING CHANGES'; - } else if (commit.type === 'Feat') { - transformedCommit.type = 'Features'; - } else if (commit.type === 'Fix') { - transformedCommit.type = 'Bug Fixes'; - } else if (commit.type === 'Perf') { - transformedCommit.type = 'Performance Improvements'; - } else if (commit.type === 'Revert') { - transformedCommit.type = 'Reverts'; - } else if (commit.type === 'Docs') { - transformedCommit.type = 'Documentation'; - } else if (commit.type === 'Style') { - transformedCommit.type = 'Styles'; - } else if (commit.type === 'Refactor') { - transformedCommit.type = 'Code Refactoring'; - } else if (commit.type === 'Test') { - transformedCommit.type = 'Tests'; - } else if (commit.type === 'Chore') { - transformedCommit.type = 'Chores'; - } else if (commit.type === 'Deps') { - transformedCommit.type = 'Dependencies'; - } else if (discard) { - return; - } - - if (commit.scope === '*') { - transformedCommit.scope = ''; - } - - if (typeof commit.hash === 'string') { - transformedCommit.hash = commit.hash.substring(0, 7); - } - - // Remove port from host URI - if (typeof context.host === 'string') { - context.host = context.host.match(/(^https?:\/\/[a-z.-]*)/)[0]; - } - - // Take issue url from package.json - const issueUrl = context.packageData.bugs && context.packageData.bugs.url; - - if (typeof transformedCommit.subject === 'string') { - transformedCommit.subject = transformedCommit.subject.replace(/#([a-zA-Z0-9-]+)/g, (_, issue) => { - issues.push(issue); - - return formatIssue(issueUrl, issue); - }); - } - - // remove references that already appear in the subject - transformedCommit.references = commit.references - .filter((reference) => { - if (issues.indexOf(reference.issue) === -1) { - return true; - } - - return false; - }) - .map((reference) => formatIssue(issueUrl, reference.issue)) - .join(', '); - - return transformedCommit; - }, - groupBy: 'type', - commitGroupsSort: 'title', - commitsSort: ['scope', 'subject'], - noteGroupsSort: 'title', - notesSort: compareFunc, -}; diff --git a/packages/conventional-changelog-lmc/src/writerOpts.js b/packages/conventional-changelog-lmc/src/writerOpts.js new file mode 100644 index 0000000..02bb8b8 --- /dev/null +++ b/packages/conventional-changelog-lmc/src/writerOpts.js @@ -0,0 +1,107 @@ +/* eslint-disable jsdoc/require-jsdoc */ +/* eslint-disable consistent-return */ +/* eslint-disable prefer-destructuring */ +/* eslint-disable no-param-reassign */ +const compareFunc = require('compare-func'); + +/** + * Formats issues using the issueURL as the prefix of the complete issue URL + * + * @param {string} issueUrl - if the issueURL is falsy, then the issue will be printed as-is. Otherwise, it will be printed as a link + * @param {string} issue - the issue reference (without the # in-front of it) + * @returns {string} - Either the issue or a Markdown-formatted link to the issue. + */ +function formatIssue(issueUrl, issue) { + if (issueUrl) { + return `[#${issue}](${issueUrl}/${issue})`; + } + + return `#${issue}`; +} + +function getWriterOpts() { + return { + transform: (commit, context) => { + let discard = true; + const issues = []; + + commit.notes.forEach((note) => { + note.title = 'BREAKING CHANGES'; + discard = false; + }); + + const transformedCommit = commit; + + if (commit.type === 'BREAKING CHANGE' || commit.type === 'BREAKING CHANGES') { + transformedCommit.type = 'BREAKING CHANGES'; + } else if (commit.type === 'Feat') { + transformedCommit.type = 'Features'; + } else if (commit.type === 'Fix') { + transformedCommit.type = 'Bug Fixes'; + } else if (commit.type === 'Perf') { + transformedCommit.type = 'Performance Improvements'; + } else if (commit.type === 'Revert') { + transformedCommit.type = 'Reverts'; + } else if (commit.type === 'Docs') { + transformedCommit.type = 'Documentation'; + } else if (commit.type === 'Style') { + transformedCommit.type = 'Styles'; + } else if (commit.type === 'Refactor') { + transformedCommit.type = 'Code Refactoring'; + } else if (commit.type === 'Test') { + transformedCommit.type = 'Tests'; + } else if (commit.type === 'Chore') { + transformedCommit.type = 'Chores'; + } else if (commit.type === 'Deps') { + transformedCommit.type = 'Dependencies'; + } else if (discard) { + return; + } + + if (commit.scope === '*') { + transformedCommit.scope = ''; + } + + if (typeof commit.hash === 'string') { + transformedCommit.hash = commit.hash.substring(0, 7); + } + + // Remove port from host URI + if (typeof context.host === 'string') { + context.host = context.host.match(/(^https?:\/\/[a-z.-]*)/)[0]; + } + + // Take issue url from package.json + const issueUrl = context.packageData.bugs && context.packageData.bugs.url; + + if (typeof transformedCommit.subject === 'string') { + transformedCommit.subject = transformedCommit.subject.replace(/#([a-zA-Z0-9-]+)/g, (_, issue) => { + issues.push(issue); + + return formatIssue(issueUrl, issue); + }); + } + + // remove references that already appear in the subject + transformedCommit.references = commit.references + .filter((reference) => { + if (issues.indexOf(reference.issue) === -1) { + return true; + } + + return false; + }) + .map((reference) => formatIssue(issueUrl, reference.issue)) + .join(', '); + + return transformedCommit; + }, + groupBy: 'type', + commitGroupsSort: 'title', + commitsSort: ['scope', 'subject'], + noteGroupsSort: 'title', + notesSort: compareFunc, + }; +} + +module.exports.getWriterOpts = getWriterOpts;