Skip to content

Commit a7133cc

Browse files
jbottiglierobcoe
authored andcommittedMay 14, 2019
fix: adds support for releaseCommitMessageFormat (#351)
1 parent 73b35f8 commit a7133cc

File tree

8 files changed

+83
-37
lines changed

8 files changed

+83
-37
lines changed
 

‎command.js

+3-7
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,9 @@ const yargs = require('yargs')
2626
default: defaults.infile
2727
})
2828
.option('message', {
29-
alias: 'm',
30-
describe: 'Commit message, replaces %s with new version',
31-
type: 'string',
32-
default: defaults.message
29+
alias: ['m'],
30+
describe: '[DEPRECATED] Commit message, replaces %s with new version.\nThis option will be removed in the next major version, please use --releaseCommitMessageFormat.',
31+
type: 'string'
3332
})
3433
.option('first-release', {
3534
alias: 'f',
@@ -132,6 +131,3 @@ Object.keys(spec.properties).forEach(propertyKey => {
132131
})
133132

134133
module.exports = yargs
135-
136-
// TODO: yargs should be populated with keys/descriptions from
137-
// https://github.com/conventional-changelog/conventional-changelog-config-spec

‎defaults.js

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
const spec = require('conventional-changelog-config-spec')
2+
3+
const defaults = {
4+
infile: 'CHANGELOG.md',
5+
firstRelease: false,
6+
sign: false,
7+
noVerify: false,
8+
commitAll: false,
9+
silent: false,
10+
tagPrefix: 'v',
11+
scripts: {},
12+
skip: {},
13+
dryRun: false,
14+
gitTagFallback: true,
15+
preset: 'conventionalcommits'
16+
}
17+
18+
/**
19+
* Merge in defaults provided by the spec
20+
*/
21+
Object.keys(spec.properties).forEach(propertyKey => {
22+
const property = spec.properties[propertyKey]
23+
defaults[propertyKey] = property.default
24+
})
25+
26+
module.exports = defaults

‎defaults.json

-15
This file was deleted.

‎index.js

+16
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,22 @@ const printError = require('./lib/print-error')
88
const tag = require('./lib/lifecycles/tag')
99

1010
module.exports = function standardVersion (argv) {
11+
/**
12+
* `--message` (`-m`) support will be removed in the next major version.
13+
*/
14+
const message = argv.m || argv.message
15+
if (message) {
16+
/**
17+
* The `--message` flag uses `%s` for version substitutions, we swap this
18+
* for the substitution defined in the config-spec for future-proofing upstream
19+
* handling.
20+
*/
21+
argv.releaseCommitMessageFormat = message.replace(/%s/g, '{{currentTag}}')
22+
if (!argv.silent) {
23+
console.warn('[standard-version]: --message (-m) will be removed in the next major release. Use --releaseCommitMessageFormat.')
24+
}
25+
}
26+
1127
let pkg
1228
bump.pkgFiles.forEach((filename) => {
1329
if (pkg) return

‎lib/format-commit-message.js

+1-7
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,4 @@
1-
const util = require('util')
2-
31
module.exports = function (rawMsg, newVersion) {
42
const message = String(rawMsg)
5-
const matchCount = (message.match(/%s/g) || []).length
6-
const args = Array(1 + matchCount)
7-
args[0] = message
8-
args.fill(newVersion, 1, args.length)
9-
return util.format.apply(util, args)
3+
return message.replace(/{{currentTag}}/g, newVersion)
104
}

‎lib/lifecycles/commit.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ module.exports = function (args, newVersion) {
99
if (args.skip.commit) return Promise.resolve()
1010
return runLifecycleScript(args, 'precommit')
1111
.then((message) => {
12-
if (message && message.length) args.message = message
12+
if (message && message.length) args.releaseCommitMessageFormat = message
1313
return execCommit(args, newVersion)
1414
})
1515
.then(() => {
@@ -55,6 +55,6 @@ function execCommit (args, newVersion) {
5555

5656
return runExec(args, 'git add' + toAdd)
5757
.then(() => {
58-
return runExec(args, 'git commit ' + verify + (args.sign ? '-S ' : '') + (args.commitAll ? '' : (toAdd)) + ' -m "' + formatCommitMessage(args.message, newVersion) + '"')
58+
return runExec(args, 'git commit ' + verify + (args.sign ? '-S ' : '') + (args.commitAll ? '' : (toAdd)) + ' -m "' + formatCommitMessage(args.releaseCommitMessageFormat, newVersion) + '"')
5959
})
6060
}

‎lib/lifecycles/tag.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ function execTag (newVersion, pkgPrivate, args) {
2525
tagOption = '-a '
2626
}
2727
checkpoint(args, 'tagging release %s%s', [args.tagPrefix, newVersion])
28-
return runExec(args, 'git tag ' + tagOption + args.tagPrefix + newVersion + ' -m "' + formatCommitMessage(args.message, newVersion) + '"')
28+
return runExec(args, 'git tag ' + tagOption + args.tagPrefix + newVersion + ' -m "' + formatCommitMessage(args.releaseCommitMessageFormat, newVersion) + '"')
2929
.then(() => runExec('', 'git rev-parse --abbrev-ref HEAD'))
3030
.then((currentBranch) => {
3131
let message = 'git push --follow-tags origin ' + currentBranch.trim()

‎test.js

+34-5
Original file line numberDiff line numberDiff line change
@@ -109,14 +109,14 @@ function getPackageVersion () {
109109
}
110110

111111
describe('format-commit-message', function () {
112-
it('works for no %s', function () {
112+
it('works for no {{currentTag}}', function () {
113113
formatCommitMessage('chore(release): 1.0.0', '1.0.0').should.equal('chore(release): 1.0.0')
114114
})
115-
it('works for one %s', function () {
116-
formatCommitMessage('chore(release): %s', '1.0.0').should.equal('chore(release): 1.0.0')
115+
it('works for one {{currentTag}}', function () {
116+
formatCommitMessage('chore(release): {{currentTag}}', '1.0.0').should.equal('chore(release): 1.0.0')
117117
})
118-
it('works for two %s', function () {
119-
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')
118+
it('works for two {{currentTag}}', function () {
119+
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')
120120
})
121121
})
122122

@@ -1070,6 +1070,35 @@ describe('standard-version', function () {
10701070
const content = fs.readFileSync('CHANGELOG.md', 'utf-8')
10711071
content.should.include('http://www.foo.com/1')
10721072
})
1073+
1074+
it('.versionrc : releaseCommitMessageFormat', function () {
1075+
// write configuration that overrides default issue
1076+
// URL format.
1077+
fs.writeFileSync('.versionrc', JSON.stringify({
1078+
releaseCommitMessageFormat: 'This commit represents release: {{currentTag}}'
1079+
}), 'utf-8')
1080+
commit('feat: another commit addresses issue #1')
1081+
execCli()
1082+
shell.exec('git log --oneline -n1').should.include('This commit represents release: 1.1.0')
1083+
})
1084+
1085+
it('--releaseCommitMessageFormat', function () {
1086+
commit('feat: another commit addresses issue #1')
1087+
execCli('--releaseCommitMessageFormat="{{currentTag}} is the version."')
1088+
shell.exec('git log --oneline -n1').should.include('1.1.0 is the version.')
1089+
})
1090+
1091+
it('[LEGACY] supports --message (and single %s replacement)', function () {
1092+
commit('feat: another commit addresses issue #1')
1093+
execCli('--message="V:%s"')
1094+
shell.exec('git log --oneline -n1').should.include('V:1.1.0')
1095+
})
1096+
1097+
it('[LEGACY] supports -m (and multiple %s replacements)', function () {
1098+
commit('feat: another commit addresses issue #1')
1099+
execCli('--message="V:%s is the %s."')
1100+
shell.exec('git log --oneline -n1').should.include('V:1.1.0 is the 1.1.0.')
1101+
})
10731102
})
10741103

10751104
describe('pre-major', () => {

0 commit comments

Comments
 (0)
Please sign in to comment.