Skip to content

Commit

Permalink
feat: Adds support for header (--header) configuration based on the…
Browse files Browse the repository at this point in the history
… spec.
  • Loading branch information
Joe Bottigliero committed May 22, 2019
1 parent a7133cc commit 003498d
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 14 deletions.
14 changes: 3 additions & 11 deletions command.js
Expand Up @@ -5,7 +5,6 @@ const { readFileSync } = require('fs')
const configPath = findUp.sync(['.versionrc', '.versionrc.json'])
const config = configPath ? JSON.parse(readFileSync(configPath)) : {}
const spec = require('conventional-changelog-config-spec')
const { START_OF_LAST_RELEASE_PATTERN } = require('./lib/lifecycles/changelog')

const yargs = require('yargs')
.usage('Usage: $0 [options]')
Expand Down Expand Up @@ -89,12 +88,12 @@ const yargs = require('yargs')
})
.option('changelogHeader', {
type: 'string',
describe: 'Use a custom header when generating and updating changelog.'
describe: '[DEPRECATED] Use a custom header when generating and updating changelog.\nThis option will be removed in the next major version, please use --header.'
})
.option('preset', {
type: 'string',
default: defaults.preset,
describe: 'Commit message guideline preset (default: angular)'
describe: 'Commit message guideline preset'
})
.check((argv) => {
if (typeof argv.scripts !== 'object' || Array.isArray(argv.scripts)) {
Expand All @@ -112,20 +111,13 @@ const yargs = require('yargs')
.pkgConf('standard-version')
.config(config)
.wrap(97)
.check((args) => {
if (args.changelogHeader && args.changelogHeader.search(START_OF_LAST_RELEASE_PATTERN) !== -1) {
throw Error(`custom changelog header must not match ${START_OF_LAST_RELEASE_PATTERN}`)
} else {
return true
}
})

Object.keys(spec.properties).forEach(propertyKey => {
const property = spec.properties[propertyKey]
yargs.option(propertyKey, {
type: property.type,
describe: property.description,
default: property.default,
default: defaults[propertyKey] ? defaults[propertyKey] : property.default,
group: 'Preset Configuration:'
})
})
Expand Down
6 changes: 6 additions & 0 deletions defaults.js
Expand Up @@ -23,4 +23,10 @@ Object.keys(spec.properties).forEach(propertyKey => {
defaults[propertyKey] = property.default
})

/**
* Sets the default for `header` (provided by the spec) for backwards
* compatibility. This should be removed in the next major version.
*/
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'

module.exports = defaults
11 changes: 11 additions & 0 deletions index.js
Expand Up @@ -24,6 +24,17 @@ module.exports = function standardVersion (argv) {
}
}

if (argv.changelogHeader) {
argv.header = argv.changelogHeader
if (!argv.silent) {
console.warn('[standard-version]: --changelogHeader will be removed in the next major release. Use --header.')
}
}

if (argv.header && argv.header.search(changelog.START_OF_LAST_RELEASE_PATTERN) !== -1) {
throw Error(`custom changelog header must not match ${changelog.START_OF_LAST_RELEASE_PATTERN}`)
}

let pkg
bump.pkgFiles.forEach((filename) => {
if (pkg) return
Expand Down
2 changes: 1 addition & 1 deletion lib/lifecycles/changelog.js
Expand Up @@ -26,7 +26,7 @@ module.exports = Changelog
function outputChangelog (args, newVersion) {
return new Promise((resolve, reject) => {
createIfMissing(args)
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'
const header = args.header

let oldContent = args.dryRun ? '' : fs.readFileSync(args.infile, 'utf-8')
let oldContentStart = oldContent.search(START_OF_LAST_RELEASE_PATTERN)
Expand Down
12 changes: 10 additions & 2 deletions test.js
Expand Up @@ -233,15 +233,15 @@ describe('cli', function () {
content.should.not.match(/legacy header format/)
})

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

it('exits with error if changelog header matches last version search regex', function () {
it('[DEPRECATED] (--changelogHeader) exits with error if changelog header matches last version search regex', function () {
fs.writeFileSync('CHANGELOG.md', '', 'utf-8')
commit('feat: first commit')
execCli('--changelogHeader="## 3.0.2"').code.should.equal(1)
Expand Down Expand Up @@ -1088,6 +1088,14 @@ describe('standard-version', function () {
shell.exec('git log --oneline -n1').should.include('1.1.0 is the version.')
})

it('--header', function () {
fs.writeFileSync('CHANGELOG.md', '', 'utf-8')
commit('feat: first commit')
execCli('--header="# Welcome to our CHANGELOG.md"').code.should.equal(0)
let content = fs.readFileSync('CHANGELOG.md', 'utf-8')
content.should.match(/# Welcome to our CHANGELOG.md/)
})

it('[LEGACY] supports --message (and single %s replacement)', function () {
commit('feat: another commit addresses issue #1')
execCli('--message="V:%s"')
Expand Down

0 comments on commit 003498d

Please sign in to comment.