Skip to content

Commit

Permalink
fix: adds support for releaseCommitMessageFormat (#351)
Browse files Browse the repository at this point in the history
  • Loading branch information
jbottigliero authored and bcoe committed May 14, 2019
1 parent 73b35f8 commit a7133cc
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 37 deletions.
10 changes: 3 additions & 7 deletions command.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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
26 changes: 26 additions & 0 deletions defaults.js
Original file line number Diff line number Diff line change
@@ -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
15 changes: 0 additions & 15 deletions defaults.json

This file was deleted.

16 changes: 16 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 1 addition & 7 deletions lib/format-commit-message.js
Original file line number Diff line number Diff line change
@@ -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)
}
4 changes: 2 additions & 2 deletions lib/lifecycles/commit.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(() => {
Expand Down Expand Up @@ -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) + '"')
})
}
2 changes: 1 addition & 1 deletion lib/lifecycles/tag.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
39 changes: 34 additions & 5 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
})
})

Expand Down Expand Up @@ -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', () => {
Expand Down

0 comments on commit a7133cc

Please sign in to comment.