diff --git a/command.js b/command.js index 5d9def558..0b70d492c 100755 --- a/command.js +++ b/command.js @@ -26,10 +26,9 @@ const yargs = require('yargs') default: defaults.infile }) .option('message', { - alias: 'm', - describe: 'Commit message, replaces %s with new version', - type: 'string', - default: defaults.message + alias: ['m'], + describe: '[DEPRECATED] Commit message, replaces %s with new version.\nThis option will be removed in the next major version, please use --releaseCommitMessageFormat.', + type: 'string' }) .option('first-release', { alias: 'f', @@ -132,6 +131,3 @@ Object.keys(spec.properties).forEach(propertyKey => { }) module.exports = yargs - -// TODO: yargs should be populated with keys/descriptions from -// https://github.com/conventional-changelog/conventional-changelog-config-spec diff --git a/defaults.js b/defaults.js new file mode 100644 index 000000000..202b8f4d4 --- /dev/null +++ b/defaults.js @@ -0,0 +1,26 @@ +const spec = require('conventional-changelog-config-spec') + +const defaults = { + infile: 'CHANGELOG.md', + firstRelease: false, + sign: false, + noVerify: false, + commitAll: false, + silent: false, + tagPrefix: 'v', + scripts: {}, + skip: {}, + dryRun: false, + gitTagFallback: true, + preset: 'conventionalcommits' +} + +/** + * Merge in defaults provided by the spec + */ +Object.keys(spec.properties).forEach(propertyKey => { + const property = spec.properties[propertyKey] + defaults[propertyKey] = property.default +}) + +module.exports = defaults diff --git a/defaults.json b/defaults.json deleted file mode 100644 index c5bc42198..000000000 --- a/defaults.json +++ /dev/null @@ -1,15 +0,0 @@ -{ - "infile": "CHANGELOG.md", - "message": "chore(release): %s", - "firstRelease": false, - "sign": false, - "noVerify": false, - "commitAll": false, - "silent": false, - "tagPrefix": "v", - "scripts": {}, - "skip": {}, - "dryRun": false, - "gitTagFallback": true, - "preset": "conventionalcommits" -} \ No newline at end of file diff --git a/index.js b/index.js index 956e1e808..e40339887 100755 --- a/index.js +++ b/index.js @@ -8,6 +8,22 @@ const printError = require('./lib/print-error') const tag = require('./lib/lifecycles/tag') module.exports = function standardVersion (argv) { + /** + * `--message` (`-m`) support will be removed in the next major version. + */ + const message = argv.m || argv.message + if (message) { + /** + * The `--message` flag uses `%s` for version substitutions, we swap this + * for the substitution defined in the config-spec for future-proofing upstream + * handling. + */ + argv.releaseCommitMessageFormat = message.replace(/%s/g, '{{currentTag}}') + if (!argv.silent) { + console.warn('[standard-version]: --message (-m) will be removed in the next major release. Use --releaseCommitMessageFormat.') + } + } + let pkg bump.pkgFiles.forEach((filename) => { if (pkg) return diff --git a/lib/format-commit-message.js b/lib/format-commit-message.js index b79ad2eb3..a597b5198 100644 --- a/lib/format-commit-message.js +++ b/lib/format-commit-message.js @@ -1,10 +1,4 @@ -const util = require('util') - module.exports = function (rawMsg, newVersion) { const message = String(rawMsg) - const matchCount = (message.match(/%s/g) || []).length - const args = Array(1 + matchCount) - args[0] = message - args.fill(newVersion, 1, args.length) - return util.format.apply(util, args) + return message.replace(/{{currentTag}}/g, newVersion) } diff --git a/lib/lifecycles/commit.js b/lib/lifecycles/commit.js index fe4c015ce..4ec00065a 100644 --- a/lib/lifecycles/commit.js +++ b/lib/lifecycles/commit.js @@ -9,7 +9,7 @@ module.exports = function (args, newVersion) { if (args.skip.commit) return Promise.resolve() return runLifecycleScript(args, 'precommit') .then((message) => { - if (message && message.length) args.message = message + if (message && message.length) args.releaseCommitMessageFormat = message return execCommit(args, newVersion) }) .then(() => { @@ -55,6 +55,6 @@ function execCommit (args, newVersion) { return runExec(args, 'git add' + toAdd) .then(() => { - return runExec(args, 'git commit ' + verify + (args.sign ? '-S ' : '') + (args.commitAll ? '' : (toAdd)) + ' -m "' + formatCommitMessage(args.message, newVersion) + '"') + return runExec(args, 'git commit ' + verify + (args.sign ? '-S ' : '') + (args.commitAll ? '' : (toAdd)) + ' -m "' + formatCommitMessage(args.releaseCommitMessageFormat, newVersion) + '"') }) } diff --git a/lib/lifecycles/tag.js b/lib/lifecycles/tag.js index 44c3bf1a5..f6a210829 100644 --- a/lib/lifecycles/tag.js +++ b/lib/lifecycles/tag.js @@ -25,7 +25,7 @@ function execTag (newVersion, pkgPrivate, args) { tagOption = '-a ' } checkpoint(args, 'tagging release %s%s', [args.tagPrefix, newVersion]) - return runExec(args, 'git tag ' + tagOption + args.tagPrefix + newVersion + ' -m "' + formatCommitMessage(args.message, newVersion) + '"') + return runExec(args, 'git tag ' + tagOption + args.tagPrefix + newVersion + ' -m "' + formatCommitMessage(args.releaseCommitMessageFormat, newVersion) + '"') .then(() => runExec('', 'git rev-parse --abbrev-ref HEAD')) .then((currentBranch) => { let message = 'git push --follow-tags origin ' + currentBranch.trim() diff --git a/test.js b/test.js index 370e658cc..02b3279ba 100644 --- a/test.js +++ b/test.js @@ -109,14 +109,14 @@ function getPackageVersion () { } describe('format-commit-message', function () { - it('works for no %s', function () { + it('works for no {{currentTag}}', function () { formatCommitMessage('chore(release): 1.0.0', '1.0.0').should.equal('chore(release): 1.0.0') }) - it('works for one %s', function () { - formatCommitMessage('chore(release): %s', '1.0.0').should.equal('chore(release): 1.0.0') + it('works for one {{currentTag}}', function () { + formatCommitMessage('chore(release): {{currentTag}}', '1.0.0').should.equal('chore(release): 1.0.0') }) - it('works for two %s', function () { - formatCommitMessage('chore(release): %s \n\n* CHANGELOG: https://github.com/conventional-changelog/standard-version/blob/v%s/CHANGELOG.md', '1.0.0').should.equal('chore(release): 1.0.0 \n\n* CHANGELOG: https://github.com/conventional-changelog/standard-version/blob/v1.0.0/CHANGELOG.md') + it('works for two {{currentTag}}', function () { + formatCommitMessage('chore(release): {{currentTag}} \n\n* CHANGELOG: https://github.com/conventional-changelog/standard-version/blob/v{{currentTag}}/CHANGELOG.md', '1.0.0').should.equal('chore(release): 1.0.0 \n\n* CHANGELOG: https://github.com/conventional-changelog/standard-version/blob/v1.0.0/CHANGELOG.md') }) }) @@ -1070,6 +1070,35 @@ describe('standard-version', function () { const content = fs.readFileSync('CHANGELOG.md', 'utf-8') content.should.include('http://www.foo.com/1') }) + + it('.versionrc : releaseCommitMessageFormat', function () { + // write configuration that overrides default issue + // URL format. + fs.writeFileSync('.versionrc', JSON.stringify({ + releaseCommitMessageFormat: 'This commit represents release: {{currentTag}}' + }), 'utf-8') + commit('feat: another commit addresses issue #1') + execCli() + shell.exec('git log --oneline -n1').should.include('This commit represents release: 1.1.0') + }) + + it('--releaseCommitMessageFormat', function () { + commit('feat: another commit addresses issue #1') + execCli('--releaseCommitMessageFormat="{{currentTag}} is the version."') + shell.exec('git log --oneline -n1').should.include('1.1.0 is the version.') + }) + + it('[LEGACY] supports --message (and single %s replacement)', function () { + commit('feat: another commit addresses issue #1') + execCli('--message="V:%s"') + shell.exec('git log --oneline -n1').should.include('V:1.1.0') + }) + + it('[LEGACY] supports -m (and multiple %s replacements)', function () { + commit('feat: another commit addresses issue #1') + execCli('--message="V:%s is the %s."') + shell.exec('git log --oneline -n1').should.include('V:1.1.0 is the 1.1.0.') + }) }) describe('pre-major', () => {