Skip to content

Commit ba80a0c

Browse files
jbottiglierotommywo
authored andcommitted
feat: Adds support for header (--header) configuration based on the spec. (#364)
1 parent bc606f8 commit ba80a0c

File tree

5 files changed

+30
-13
lines changed

5 files changed

+30
-13
lines changed

command.js

+2-10
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
const spec = require('conventional-changelog-config-spec')
22
const { getConfiguration } = require('./lib/configuration')
33
const defaults = require('./defaults')
4-
const { START_OF_LAST_RELEASE_PATTERN } = require('./lib/lifecycles/changelog')
54

65
const yargs = require('yargs')
76
.usage('Usage: $0 [options]')
@@ -93,7 +92,7 @@ const yargs = require('yargs')
9392
})
9493
.option('changelogHeader', {
9594
type: 'string',
96-
describe: 'Use a custom header when generating and updating changelog.'
95+
describe: '[DEPRECATED] Use a custom header when generating and updating changelog.\nThis option will be removed in the next major version, please use --header.'
9796
})
9897
.option('preset', {
9998
type: 'string',
@@ -116,20 +115,13 @@ const yargs = require('yargs')
116115
.pkgConf('standard-version')
117116
.config(getConfiguration())
118117
.wrap(97)
119-
.check((args) => {
120-
if (args.changelogHeader && args.changelogHeader.search(START_OF_LAST_RELEASE_PATTERN) !== -1) {
121-
throw Error(`custom changelog header must not match ${START_OF_LAST_RELEASE_PATTERN}`)
122-
} else {
123-
return true
124-
}
125-
})
126118

127119
Object.keys(spec.properties).forEach(propertyKey => {
128120
const property = spec.properties[propertyKey]
129121
yargs.option(propertyKey, {
130122
type: property.type,
131123
describe: property.description,
132-
default: property.default,
124+
default: defaults[propertyKey] ? defaults[propertyKey] : property.default,
133125
group: 'Preset Configuration:'
134126
})
135127
})

defaults.js

+6
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@ Object.keys(spec.properties).forEach(propertyKey => {
2323
defaults[propertyKey] = property.default
2424
})
2525

26+
/**
27+
* Sets the default for `header` (provided by the spec) for backwards
28+
* compatibility. This should be removed in the next major version.
29+
*/
30+
defaults.header = '# Changelog\n\nAll notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.\n'
31+
2632
defaults.packageFiles = [
2733
'package.json',
2834
'bower.json',

index.js

+11
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,17 @@ module.exports = function standardVersion (argv) {
2626
}
2727
}
2828

29+
if (argv.changelogHeader) {
30+
argv.header = argv.changelogHeader
31+
if (!argv.silent) {
32+
console.warn('[standard-version]: --changelogHeader will be removed in the next major release. Use --header.')
33+
}
34+
}
35+
36+
if (argv.header && argv.header.search(changelog.START_OF_LAST_RELEASE_PATTERN) !== -1) {
37+
throw Error(`custom changelog header must not match ${changelog.START_OF_LAST_RELEASE_PATTERN}`)
38+
}
39+
2940
const args = Object.assign({}, defaults, argv)
3041
let pkg
3142
args.packageFiles.forEach((packageFile) => {

lib/lifecycles/changelog.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ module.exports = Changelog
2626
function outputChangelog (args, newVersion) {
2727
return new Promise((resolve, reject) => {
2828
createIfMissing(args)
29-
const header = args.changelogHeader || '# Changelog\n\nAll notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.\n'
29+
const header = args.header
3030

3131
let oldContent = args.dryRun ? '' : fs.readFileSync(args.infile, 'utf-8')
3232
const oldContentStart = oldContent.search(START_OF_LAST_RELEASE_PATTERN)

test.js

+10-2
Original file line numberDiff line numberDiff line change
@@ -233,15 +233,15 @@ describe('cli', function () {
233233
content.should.not.match(/legacy header format/)
234234
})
235235

236-
it('allows for a custom changelog header', function () {
236+
it('[DEPRECATED] (--changelogHeader) allows for a custom changelog header', function () {
237237
fs.writeFileSync('CHANGELOG.md', '', 'utf-8')
238238
commit('feat: first commit')
239239
execCli('--changelogHeader="# Pork Chop Log"').code.should.equal(0)
240240
const content = fs.readFileSync('CHANGELOG.md', 'utf-8')
241241
content.should.match(/# Pork Chop Log/)
242242
})
243243

244-
it('exits with error if changelog header matches last version search regex', function () {
244+
it('[DEPRECATED] (--changelogHeader) exits with error if changelog header matches last version search regex', function () {
245245
fs.writeFileSync('CHANGELOG.md', '', 'utf-8')
246246
commit('feat: first commit')
247247
execCli('--changelogHeader="## 3.0.2"').code.should.equal(1)
@@ -1262,6 +1262,14 @@ describe('standard-version', function () {
12621262
content.should.include('http://www.foo.com/ABC-1')
12631263
})
12641264

1265+
it('--header', function () {
1266+
fs.writeFileSync('CHANGELOG.md', '', 'utf-8')
1267+
commit('feat: first commit')
1268+
execCli('--header="# Welcome to our CHANGELOG.md"').code.should.equal(0)
1269+
const content = fs.readFileSync('CHANGELOG.md', 'utf-8')
1270+
content.should.match(/# Welcome to our CHANGELOG.md/)
1271+
})
1272+
12651273
it('--issuePrefixes and --issueUrlFormat', function () {
12661274
commit('feat: another commit addresses issue ABC-1')
12671275
execCli('--issuePrefixes="ABC-" --issueUrlFormat="http://www.foo.com/{{prefix}}{{id}}"')

0 commit comments

Comments
 (0)