Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: avoid config mutating with default params (fixes #930) #931

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
'use strict'
const _ = require('lodash')

const defaultConfig = {
issuePrefixes: ['#']
}

module.exports = function (config) {
config = defaultConfig(config)
const readyConfig = _.defaults(config, defaultConfig)

return {
headerPattern: /^(\w*)(?:\((.*)\))?!?: (.*)$/,
breakingHeaderPattern: /^(\w*)(?:\((.*)\))?!: (.*)$/,
Expand All @@ -13,13 +19,6 @@ module.exports = function (config) {
noteKeywords: ['BREAKING CHANGE', 'BREAKING-CHANGE'],
revertPattern: /^(?:Revert|revert:)\s"?([\s\S]+?)"?\s*This reverts commit (\w*)\./i,
revertCorrespondence: ['header', 'hash'],
issuePrefixes: config.issuePrefixes
issuePrefixes: readyConfig.issuePrefixes
}
}

// merge user set configuration with default configuration.
function defaultConfig (config) {
config = config || {}
config.issuePrefixes = config.issuePrefixes || ['#']
return config
}
65 changes: 28 additions & 37 deletions packages/conventional-changelog-conventionalcommits/writer-opts.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
const addBangNotes = require('./add-bang-notes')
const compareFunc = require('compare-func')
const Q = require('q')
const _ = require('lodash')
const readFile = Q.denodeify(require('fs').readFile)
const resolve = require('path').resolve
const releaseAsRe = /release-as:\s*\w*@?([0-9]+\.[0-9]+\.[0-9a-z]+(-[0-9a-z.]+)?)\s*/i
Expand All @@ -14,19 +15,41 @@ const owner = '{{#if this.owner}}{{~this.owner}}{{else}}{{~@root.owner}}{{/if}}'
const host = '{{~@root.host}}'
const repository = '{{#if this.repository}}{{~this.repository}}{{else}}{{~@root.repository}}{{/if}}'

const defaultConfig = {
types: [
{ type: 'feat', section: 'Features' },
{ type: 'feature', section: 'Features' },
{ type: 'fix', section: 'Bug Fixes' },
{ type: 'perf', section: 'Performance Improvements' },
{ type: 'revert', section: 'Reverts' },
{ type: 'docs', section: 'Documentation', hidden: true },
{ type: 'style', section: 'Styles', hidden: true },
{ type: 'chore', section: 'Miscellaneous Chores', hidden: true },
{ type: 'refactor', section: 'Code Refactoring', hidden: true },
{ type: 'test', section: 'Tests', hidden: true },
{ type: 'build', section: 'Build System', hidden: true },
{ type: 'ci', section: 'Continuous Integration', hidden: true }
],
issueUrlFormat: '{{host}}/{{owner}}/{{repository}}/issues/{{id}}',
commitUrlFormat: '{{host}}/{{owner}}/{{repository}}/commit/{{hash}}',
compareUrlFormat: '{{host}}/{{owner}}/{{repository}}/compare/{{previousTag}}...{{currentTag}}',
userUrlFormat: '{{host}}/{{user}}',
issuePrefixes: ['#']
}

module.exports = function (config) {
config = defaultConfig(config)
const commitUrlFormat = expandTemplate(config.commitUrlFormat, {
const readyConfig = _.defaults(config, defaultConfig)
const commitUrlFormat = expandTemplate(readyConfig.commitUrlFormat, {
host,
owner,
repository
})
const compareUrlFormat = expandTemplate(config.compareUrlFormat, {
const compareUrlFormat = expandTemplate(readyConfig.compareUrlFormat, {
host,
owner,
repository
})
const issueUrlFormat = expandTemplate(config.issueUrlFormat, {
const issueUrlFormat = expandTemplate(readyConfig.issueUrlFormat, {
host,
owner,
repository,
Expand All @@ -41,7 +64,7 @@ module.exports = function (config) {
readFile(resolve(__dirname, './templates/footer.hbs'), 'utf-8')
])
.spread((template, header, commit, footer) => {
const writerOpts = getWriterOpts(config)
const writerOpts = getWriterOpts(readyConfig)

writerOpts.mainTemplate = template
writerOpts.headerPartial = header
Expand Down Expand Up @@ -69,8 +92,6 @@ function findTypeEntry (types, commit) {
}

function getWriterOpts (config) {
config = defaultConfig(config)

return {
transform: (commit, context) => {
let discard = true
Expand Down Expand Up @@ -173,36 +194,6 @@ function getWriterOpts (config) {
}
}

// merge user set configuration with default configuration.
function defaultConfig (config) {
config = config || {}
config.types = config.types || [
{ type: 'feat', section: 'Features' },
{ type: 'feature', section: 'Features' },
{ type: 'fix', section: 'Bug Fixes' },
{ type: 'perf', section: 'Performance Improvements' },
{ type: 'revert', section: 'Reverts' },
{ type: 'docs', section: 'Documentation', hidden: true },
{ type: 'style', section: 'Styles', hidden: true },
{ type: 'chore', section: 'Miscellaneous Chores', hidden: true },
{ type: 'refactor', section: 'Code Refactoring', hidden: true },
{ type: 'test', section: 'Tests', hidden: true },
{ type: 'build', section: 'Build System', hidden: true },
{ type: 'ci', section: 'Continuous Integration', hidden: true }
]
config.issueUrlFormat = config.issueUrlFormat ||
'{{host}}/{{owner}}/{{repository}}/issues/{{id}}'
config.commitUrlFormat = config.commitUrlFormat ||
'{{host}}/{{owner}}/{{repository}}/commit/{{hash}}'
config.compareUrlFormat = config.compareUrlFormat ||
'{{host}}/{{owner}}/{{repository}}/compare/{{previousTag}}...{{currentTag}}'
config.userUrlFormat = config.userUrlFormat ||
'{{host}}/{{user}}'
config.issuePrefixes = config.issuePrefixes || ['#']

return config
}

// expand on the simple mustache-style templates supported in
// configuration (we may eventually want to use handlebars for this).
function expandTemplate (template, context) {
Expand Down