From ba80a0c27029f54c751fe845560504925b45eab8 Mon Sep 17 00:00:00 2001 From: Joe Bottigliero Date: Sun, 8 Dec 2019 14:27:49 -0600 Subject: [PATCH] feat: Adds support for `header` (--header) configuration based on the spec. (#364) --- command.js | 12 ++---------- defaults.js | 6 ++++++ index.js | 11 +++++++++++ lib/lifecycles/changelog.js | 2 +- test.js | 12 ++++++++++-- 5 files changed, 30 insertions(+), 13 deletions(-) diff --git a/command.js b/command.js index 95d15825a..f84b3f9ef 100755 --- a/command.js +++ b/command.js @@ -1,7 +1,6 @@ const spec = require('conventional-changelog-config-spec') const { getConfiguration } = require('./lib/configuration') const defaults = require('./defaults') -const { START_OF_LAST_RELEASE_PATTERN } = require('./lib/lifecycles/changelog') const yargs = require('yargs') .usage('Usage: $0 [options]') @@ -93,7 +92,7 @@ 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', @@ -116,20 +115,13 @@ const yargs = require('yargs') .pkgConf('standard-version') .config(getConfiguration()) .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:' }) }) diff --git a/defaults.js b/defaults.js index fc88a5f61..dd9c684ef 100644 --- a/defaults.js +++ b/defaults.js @@ -23,6 +23,12 @@ 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' + defaults.packageFiles = [ 'package.json', 'bower.json', diff --git a/index.js b/index.js index 9ee458584..74de6e031 100755 --- a/index.js +++ b/index.js @@ -26,6 +26,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}`) + } + const args = Object.assign({}, defaults, argv) let pkg args.packageFiles.forEach((packageFile) => { diff --git a/lib/lifecycles/changelog.js b/lib/lifecycles/changelog.js index 0f3616a21..dff90ab0d 100644 --- a/lib/lifecycles/changelog.js +++ b/lib/lifecycles/changelog.js @@ -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') const oldContentStart = oldContent.search(START_OF_LAST_RELEASE_PATTERN) diff --git a/test.js b/test.js index 19120bccf..b170513c9 100644 --- a/test.js +++ b/test.js @@ -233,7 +233,7 @@ 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) @@ -241,7 +241,7 @@ describe('cli', function () { 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) @@ -1262,6 +1262,14 @@ describe('standard-version', function () { content.should.include('http://www.foo.com/ABC-1') }) + it('--header', function () { + fs.writeFileSync('CHANGELOG.md', '', 'utf-8') + commit('feat: first commit') + execCli('--header="# Welcome to our CHANGELOG.md"').code.should.equal(0) + const content = fs.readFileSync('CHANGELOG.md', 'utf-8') + content.should.match(/# Welcome to our CHANGELOG.md/) + }) + it('--issuePrefixes and --issueUrlFormat', function () { commit('feat: another commit addresses issue ABC-1') execCli('--issuePrefixes="ABC-" --issueUrlFormat="http://www.foo.com/{{prefix}}{{id}}"')